General Guidelines for Good Programming – Part 2
Hi Friends, If you have missed Part 1 of General Guidelines for Good Programming, make sure you read that first.
Nested Loop:
If you have several nested loops, assign longer names to the loop variables to improve readability. Java Example of Good Loop Names in a Nested Loop
for ( teamIndex = 0; teamIndex < teamCount; teamIndex++ ) { for ( eventIndex = 0; eventIndex < eventCount[ teamIndex ]; eventIndex++ ) { score[ teamIndex ][ eventIndex ] = 0; } }
Diagrams:
Draw a Diagram Diagrams are another powerful heuristic tool. A picture is worth 1000 words—kind of. Picture can represent the problem at a higher level of abstraction.
Routine Parameters:
Don’t use routine parameters as working variables It’s dangerous to use the parameters passed to a routine as working variables. Use local variables instead. For example, in the following Java fragment, the variable inputVal is improperly used to store intermediate results of a computation
int Sample( int inputVal ) { inputVal = inputVal * CurrentMultiplier( inputVal ); inputVal = inputVal + CurrentAdder( inputVal );// ... At this point, inputVal is no longer contains a input value longer }
Reuse Code:
The single biggest way to improve both the quality of your code and your productivity is to reuse good code. Check whether the code is available in library or any algorithms are available. Make sure you are not doing more work than necessary.
How to Optimize?
It’s usually a waste of effort to work on efficiency at the level of individual routines. The big optimizations come from refining the high-level design, not the individual routines. You generally use micro-optimizations only when the high-level design turns out not to support the system’s performance goals, and you won’t know that until the whole program is done. Don’t waste time scraping for incremental improvements until you know they’re needed.
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.