Monday, September 8, 2008

Coolest Programming Convention Ever!!!

Really don’t know how many of you actually heard of this...

This coding convention is really cool:

It goes like this: For equivalence statements, keep the constant on the left side.

Eg. a == 3; //Not Good

3 == a; //Gr8!

How it helps???? It will help you in situations where you "mistakingly" used a "=" inplace of"==".

a = 3 compiles to true

While, 3 = a generates a compile time error!!!

Wow!! Isn’t it!


  1. While at first glance this might seem like a good idea, it really isn't for a couple of reasons.

    1. In my opinion, it makes the code harder to read. Code reads from left to right so while "if x is equal to 3" parses easily, "if 3 is equal to x" always makes me think "how can the value of 3 change?".

    2. Every C & C++ compiler I've run across in the last 20 years has had the ability to flag an unintentional = as a warning. You are compiling with warnings cranked up to max and paying attention to those warnings, right?

    What's worse is that this technique seems to be creeping into languages where a statement like "if (x = 3)" already triggers a syntax error such as C# and Python.

  2. @fgb

    Agree with you on first point. It really is an unconventional convention... so shocks at first!

    OK Now Point no.2: There are languages other than C & C++ man. Think of scripting languages: Javascript or PHP for instance where you cant really compile a code to see that warning... and u'll never know what happened!!!

    If x=3 is a syntax error, the convention doesnt applies.. u'll anyhow track the mistaking "="!!!