-
Left hand comparison: Better way to write your IF condition
In my daily routine, I use C# as the main language to code. Those from C or C++ world may know what I am talking about.
Try this in your C# codebool flag1 = true;if(flag1 = false){Console.WriteLine(flag1);}Technically this should give you error (Tested in VS 2010, Framework 4.0) but it does not. Instead of comparing flag1 to false , code will assign false value to flag1. This will always be true and visual studio compiler will never give you error. Note: This is only true for Boolean values.This is called right hand comparison. Instead I try to follow Left-hand comparison
(http://en.wikipedia.org/wiki/Coding_conventions#Left-hand_comparisons) in which you always put Value on the left hand side of the variable to compare. i.e.if ( false = flag1) then …This code will always give you compile time error so there is no scope of accidental assignment error.