Tuesday, August 10, 2010

if This, Then else That

If Else statements are the major part of Coding. These are the roads that split and divert the streams of logic. The point that I want to tackle here is about how phrasing the “If Else” statements can influence the mindset of writer and reader of the code. The claim that I would make is that “If” statements are better be phrased in positive term. And the operations in “If” block, are better be handling the main, a general, a positive flow of logic. And the rest is for “Else” block.

Thus, developer is concentrating first on positives and only then on negatives. Goal first, diligence second. Main flow first, edge cases second. Wall first, coloring it second.



If (something)


   do this




  do that



By concentrating first on the positive aspect helps person to get into the subject of the statements. “do this” communicates the context, a pathway towards some goal, and “do that” communicates the edge case, a complimentary addition to “do this”. There can not be This without That. This and That go together. What’s more important? This or That? Both? What’s the prioritization?This first, That second, isn’t it?


rubenhak said...

Hmmm.... nice approach. If (i were you) Then { i would accumulate some more patters/ideas like this to improve code readability in the IDE and suggest it to Microsoft/Visual Assist teams. } Else { I would wait somebody to suggest it to Microsoft/Visual Assist teams. } :)

securebox said...

First define what means "positive". I think positiveness is too subjective to be universally defined.

I would recommend developing this idea more and come up with concrete recommendations on how one should place the "cases" of if statement.

Try to be more concrete.

Diana said...
This comment has been removed by the author.
Diana said...

Nice posting, thanks for sharing. Interesting that there is somebody who thinks that much globally :)

Securebox, seems that there is an answer inside the post for your question. Concrete recommendation will be to put the main goal in if statement.


int Divide(int x, int y)
if(y != 0)
return x/y;
throw new Exception("Hello World, I'm not your goal!");

Btw, if else statement is really the most important statements in our lives. This is how we build the tree of our lives, even thought it's not binary :))

securebox said...

Let me rephrase my concern to make it more clear.

Your example is trivial. There is a "positive" case (y != 0) and a "negative" case (else block).

What I'm saying is what if the "cases" of a conditional statement are not as trivial as your example? How should the programmer behave in that case?
The author suggests that they should first put the positive case but I'm arguing that "positivity" is not perceived universally by everyone. One thing that is positive for me doesn't have to be positive for you.

So, unless there are concrete recommendations, this approach can end up with the same chaotic state as we currently see in source codes (assumingly the article suggests a way to avoid such chaos).

Post a Comment