General Guidelines for Programming – Part 1
General guidelines for programming:
These are the awesome general guidelines for programming gathered from ‘Code Complete’ Book by Steve McConnel.
Make type conversions obvious. Make sure that someone reading your code will be aware of it when a conversion between different data types occurs. In C++ you could say y = x + (float) i. This practice also helps to ensure that the conversion is the one you want to occur— different compilers do different conversions, so you’re taking your chances otherwise.
Abbreviate consistently Always use the same abbreviation. For example, use Num everywhere or No everywhere, but don’t use both. Similarly, don’t abbreviate a word in some names and not in others. For instance, don’t use the full word Number in some places and the abbreviation Num in others. Create names that you can pronounce Use xPos rather than xPstn and needsComp rather than ndsCmptg. Apply the telephone test—if you can’t read your code to some- one over the phone, rename your variables to be more distinctive.
One common programming problem is misuse of global variables. If you give all global variable names a g_ prefix, for example, a programmer seeing the variable g_RunningTotal will know it’s a global variable.
Use positive boolean variable names Negative names like notFound, notdone, and notSuccessful are difficult to read.
Use appropriate name and it should convey the purpose of the variable. For better results, replace ‘status’ with a name like ‘error’ or ‘statusOK’, and replace ‘sourceFile’ with ‘sourceFileAvailable’ or ‘sourceFileFound’, or whatever the variable represents.
Use final or const when possible, you can prevent the variable from being assigned a value after it’s initialized. Ideally, declare and define each variable close to where it’s first used. C++, Java supports such a declaration.
These are the points that I have gathered from the Book ‘Code Complete’ written by Steve McConnell. Every Software Engineer Should read this book at least once. It contains lots of guides with real-time usage which can make you a better programmer.
You can buy the book using the below link:
General Guidelines Series