Archive for December, 2008

Why Avoid The “else” Construct ?

Till now I have seen a fair amount of code, the good, the bad and o yes! the ugly and the really really ugly. Ok, so you are a programmer, what makes you think a chunk of code is "good" or "bad" ? - I haven't had the opportunity to discuss this with fellow programmers in detail, so I think the best way to air this would be to document this.

First thing that comes to my mind about good code is READABILITY ... it's imperative the code should be readable and understandable. This however, is automatically achieved when you follow the most optimum way of execution and is even prettier in OOP. Structured code provokes ugliness. And I have noticed that the code gets uglier when there are more and more if-elseif-else construct nesting. I have seen programmers abusing this construct a lot ! You are better off using a SWITCH-CASE construct (which should also be used if and only if necessary).

I know you must be thinking: So what's up with the "else" construct ? and what does this guy have against it ? - Well, in all honesty, I don't have anything against it. It's just that the "else" construct refrains me to write beatiful code :P - Ask yourself, why would you need a lot of else statements anyways ? (do keep in mind that for menu choices you always have the good 'ol switch-case). This may not hold true when you are executing via the structured approach. When using OOP, it just doesn't make much sense. Of course, a simple "if-else" is ok as its simple and readable too. But when you are dwelling into if-elseif { if - else }-else { if - else } it becomes more complex and consequently turns into a nightmare. Imagine yourself trying to debug a 100 lines of code out which at least 40 are consumed by the nested "if" construct. Hehe, I know ! - It scares me too :)

So what does it mean when you have used nested "if-elseif-else" structure a one too many times ? I answer this based on my own code more than a year ago. It means:

1. You are not breaking down problems into smaller chunks, that's for sure! - consequently you have become that greedy Ostrich who swallowed the whole freshly baked potato! so obviously the aftermath is the same. You jump, cry and get frustrated when you need to refactor the code or even worst, debug it.

2. Poor design ! - Yes ! poor design ! I don't care if you agree with me here or not, but you don't have a robust and scalable design before you put your cowboy hat on and went riding that keyboard. You did not think twice before coding. The rationale goes like this: An "if-else" construct denotes a change in variables and you address that change by changing the execution flow. Making the right decisions when a change occurrs. In an OO way, that variable change denotes a "state" change. So you do what you should according to that particular state. Similar to your reactions based on your emotional state,

Happy => Smile || Laugh

, Angry => Frown || Break something || Punch someone

, Sad => Cry || Whiskey++

In OO you can actually encapsulate execution as behaviors according to the change in state. As a result, you design is robust and scalable. All you need to do is invoke the right behavior for the right state. For that you'd probably need a simple IF statement.

3. Obviously either you haven't heard or you aren't interested in the philosophy behind "return home early" - a term coined by Felix from debuggable.com - READ IT as it addresses programming psychology you might make use of.

Doh ! I've run out of time. I'm going to try to put some code slides up. You look at the code and decide for youself.

The Grunts And Groans Of Working From Home

Before starting with me grunts and groans here’s a little note:

To all those who don't have experience of “working from home” try to turn off your judgments while you read. It's a classic example of “you don't know how it's like”. And those who fancy this way of doing business well guess what? you still don’t get to be a slacker.

First off, being human, we have to socialize in some way. For most of us, just having 100+ friends on facebook or any other social network is appealing, but not the real thing. Socializing becomes a challenge when working from home amidst many others. This is the biggest challenge what I have experienced so far, and overcame it too (this part would require a separate post :P). Thank god I don't work alone now. But still, one could rot if one does not step out of the house so very often. To counter this, surely you’d socialize on all those social networks out there, but to get the real thing, here is what I had found useful:

  • - Spend some good 30 minutes outside (and this is for the worst of the introverts out there, it should be an hour or so – no offence). Take a stroll in a park / peaceful place or join a gym. Some fresh air won’t kill ya! But staying inside surrounded by 4 walls will definitely do some damage to your brain.
  • - Try to network up with people who are in the same line of business as you are. And meet up!
  • - Keep an eye out for different events happening out there that are even mildly related to your industry. Try to show up on those. Trust me you’ll feel great when you meet the alike. And probably the best way to do that would be to join a local community.  Finally! You might be able to put all those social networking apps to the right use :P – I’m sure you’d be able to find your local user group through these networks.

Apart of the social issue, here comes another. People don’t tend to take you seriously when you tell them you work from home. This can be frustrating at times. People are still getting used to the idea and some just reject it. This is especially true for countries like Pakistan and I’m sure India’s in the list too. Like many other countries, we both have a family oriented environment and mostly I find people “branding” themselves based on the companies they work in a one too many times. So after having a couple of these experiences, the idea came like a lightning bolt! Yup ! get yourself a business card!, just shrink the “what you do part” in a single understandable phrase. For instance, I use “Software Engineer” that works for me. The funny part is, I’ve experienced that people don’t really understand the “software” part :P they just calm down when they hear “engineer”, which is actually really cool as I’m always giggling when witnessing this. Ah! On the other hand, this always makes me wonder when the hell we are going to get out of this entire stereo-typical behavioral pattern.
The third evil is keeping loose work timings. This can be cancerous to your work! – seriously! Some people may not realize this until after they find themselves in the middle of a crisis. And I had to find this the hard way … doh ! You have to separate your work timings. Sure sure, you must be thinking I didn’t choose to work from home just so that I work strict timings. Unfortunately, to keep professionalism, discipline is important. Can’t say this for sure about designers and the more creative kind but I can say this certainly for programmers. Although, you might be one of those people who are most creative during specific times of day, then you should time yourself according to that. You see this is where working from home comes in handy. You can choose your own timings.
A few things can help you achieve discipline. You can start off by creating a separate email, IM and phone for your work. It’d be easier to discipline your work this way. Although here I would have to admit that I haven’t been able to implement that yet, but keeping in view my plans, I might not need to. Trust me, it’s really important to keep your private and work contacts separate. The next thing you’d know is that your private email is bombarded by client requests and your private phone is ringing so very often with clients on other end and your folks giving you strange looks.
In essence, the important thing is that you should find time and reflect on other important aspects of your life during the day. I think I know now why there is an 8 hour per day work limit. It helps strike a balance between your private and work life. Honestly, when you are working from home you can cross that work limit very easily without even knowing what you did till later. But there are always exceptions. People who are passionate about something won’t stop at nothing till they achieve it. If you’re not that obsessive about something, then you should strike a balance with discipline.
I’m sure my experiences are quite similar with other people. Let me know if there are any differences you find. That would surely help me understand this better :)