Coding Styles

One thing that is really important when coding is to eliminate errors from the start. Lots of errors happen because of a typo one being if(a=1) in a C sort of language. This is a assignment not a comparison (==) so how can I avoid this error?
By putting the 1 before the == like
if(1==a)
if you forget one = (if(1=a)) this will moan at you at compile time.
In short
Never use (a==1) always use (1==a)

a=1 will compile and always return true
1=a will not compile

Thx to J. Spolsky

No comments: