Tuesday, August 13, 2013

Code Complete Chapter 31 Layout and Style Notes

Notes from reading:
Code Complete: A Practical Handbook of Software Construction, Second Edition by Steve McConnell Link: Amazon 
I have been reading this try to improve my skills I figured I could jot down some notes which might help it sink in my brain and who know you might find interesting.  
 
Interesting
for ( int i = 0; i < MAX_LINES; i++ )
{
          ReadLine( i );
          ProcessLine( i );
}

for ( int i = 0; i < MAX_LINES; i++ )
   { 
          ReadLine( i );
          ProcessLine( i );
   }
McConnell makes the point that the above coding styles don't really illustrate the logic of the code.  His point is basically that the statements really don't need to be indented.  He believe that the following is really more appropriate. As the below better shows the concept better of what level the statement are at. 

for ( int i = 0; i < MAX_LINES; i++ )
   { 
    ReadLine( i );
    ProcessLine( i );
   }

I admit I have been using the first example quite often in C++ and C# as it seems to be the Visual Studio default.  He also mentions that the more standard Java (pure block) method is probably a slightly better layout.  I think he makes a decent case that this is a little better style as it would require less indenting to show the levels.
for ( int i = 0; i < MAX_LINES; i++ ) { 
    ReadLine( i );
    ProcessLine( i );
}


Nice to see in writing that the 80 character limit is not something that anyone should be worried about on modern technology.

grossRate[census[groupId].gender,census[groupId].ageGroup] should be
grossRate[ census[ groupId ].gender, census[ groupId ].ageGroup] which I will admit is probably more readable but I think it only works on a new project.  Trying to do that on an existing code base would probably just mean I wouldn't be able to search for anything properly. 

"A class should support one and only one purpose" I have read this a lot and it seems to make sense but sadly it seems like no one likes to do it. 
submit to reddit

No comments:

Post a Comment