Skip to content

Conditions Suck

December 6, 2011

Well obviously you cannot write a condition-free code and what’s wrong about if else and switch case statements anyway?

The problem with them is that they tend to make methods and functions grow tumors. What is the easiest way to solve the fallowing problem — you are required for a change to implement a new functionality of some kind. It has to fallow some flow that is already implemented. You go to the method that does similar kind of thing you need and see this:

if (condition)
{
   //Some 50 lines of code.
}
else if (anotherCondition)
{
   //Another 50 lines of similar code. Hard to tell a difference at first look.
}

Time is pushing, you need to implement it by the afternoon and today doesn’t seem to be the most productive day either. Copy-paste, change some things and you’re good to go. Unit tests pass, testers are not complaining, client is happily using the new functionality.

The thing is, if it is done wrong, most probably, while implementing some small change, it will force you to keep the existing bad style. Eventually, if code smells, it will start to suffocate you as the stench grows.

On the other hand, if it is implemented right, you most probably will maintain good style as well.

Whenever I have to implement a new feature and see this conditional crap, I wanna puke. For me, this instantly indicates design flaw and needs to be refactored. If I have a spare minute to make the code base better, I just look for conditions and remove them with strategy or template.

Advertisements
5 Comments leave one →
  1. December 6, 2011 18:44

    Interesting… I am new to programming/scripting (learning bash scripting and some python) and was wondering if you could provide an example of pseudo code showing how the conditional statement can be cleaned up with “strategy or [a] template”?

    Is it a method that helps reduce redundant/duplicated code?

    • December 6, 2011 22:07

      > if you could provide an example of pseudo code showing how the conditional statement can be cleaned up with “strategy or [a] template”?
      absolutely! I will create a new post about it (should be here in 24 hours)

      >Is it a method that helps reduce redundant/duplicated code?
      Yes, but not only, it would allow easier maintainability and readability (as I will show)

    • December 7, 2011 21:18

      Take a look at https://mikeongis.wordpress.com/2011/12/07/conditions-to-strategy/
      Sorry if it is a bit messy, but I hope it would clear some things up.
      Also, I am unable to give you advice in Python or Bash as I am quite newb with them myself…

Trackbacks

  1. Conditions to Strategy « MikeOnGIS
  2. The Pragmatic Programmer – Review « MikeOnGIS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: