Feb 062013

While comparing a variable with a constant (literal) in C/C++ language, which of the two ways of comparison should we use ?

    if( x == 2 ) ...

    if( ptr == NULL ) ...


    if( 2 == x ) ...

    if( NULL == ptr ) ...

In the first comparison we are writing variable on the Left side of == and literal (constant) on right side. whereas, in the second form we are writing the literal (2, NULL, etc.) on the left side of ==.
Continue reading »

Aug 292012

The Open Close Principle in Programming says

“Software entities like Classes, Functions, Modules should be open for extension, but closed for modification.”

It encourages programmers to write code in such a way that new functionality can be added without changing the existing code.

i.e If we are writing a class, then we may later, extend the class and create more base classes to it (which will enhance the functionality further), But, we should never be forced to change the code inside the class, just because some more features need to be added. Same goes for functions.
Continue reading »