Click here to Skip to main content
Click here to Skip to main content

Tagged as

Intelligent comparison syntax

, 2 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
When you compare a variable (a lvalue) to a constant (not a lvalue), always write the lvalue on the right hand side of the comparison.Bad example:if ( n == 5 ){ // do something}Good example:if ( 5 == n ){ // do something}This will help with your debugging in case...
When you compare a variable (a lvalue) to a constant (not a lvalue), always write the lvalue on the right hand side of the comparison.
 
Bad example:
if ( n == 5 )
{
   // do something
}
 
Good example:
if ( 5 == n )
{
   // do something
}
 
This will help with your debugging in case of a typo: if you mistype '==' as '=', in the first case, your code will compile just fine, resulting in a program with some hard to catch error which might take you hours to find.
 
In the second case however your compiler will immediately complain, since the syntax is incorrect and you will find and fix the error within seconds.
 
(C) notice: Thanks to Ralf Babel for this invaluable tip he published in his book, "Das Amiga Guru Buch" (originally available in german only, but to my knowledge there is a much newer english version as well)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Stefan_Lang
Software Developer (Senior)
Switzerland Switzerland
Graduated at TU Darmstadt in Math & CS, with a heavy focus on CAD/CAM
 
Programming and designing applications in C++ in the areas AI, real-time programming, client-server applications and CAD/CAM since 1985.
 
Personal interests: AI, computer graphics, games, reading

Comments and Discussions

 
GeneralMy vote of 1 PinmemberYoldas Askan15-Sep-14 8:04 
GeneralMy vote of 5 PinmvpMichael Haephrati מיכאל האפרתי20-Feb-13 7:06 
GeneralRe: My vote of 5 PinmemberStefan_Lang20-Feb-13 23:05 
GeneralRe: My vote of 5 PinmvpMichael Haephrati20-Feb-13 23:17 
Questionvote of 1 - What is the problem? PinmemberStefan632-Mar-10 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 2 Mar 2010
Article Copyright 2010 by Stefan_Lang
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid