How do you use blank lines in your code ?

Posted by Matthieu M. on Programmers See other posts from Programmers or by Matthieu M.
Published on 2010-11-06T13:50:25Z Indexed on 2010/12/29 17:59 UTC
Read the original article Hit count: 451

Filed under:
|

There has been a few remarks about white space already in discussion about curly braces placements.

I myself tend to sprinkle my code with blank lines in an attempt to segregate things that go together in "logical" groups and hopefully make it easier for the next person to come by to read the code I just produced.

In fact, I would say I structure my code like I write: I make paragraphs, no longer than a few lines (definitely shorter than 10), and try to make each paragraph self-contained.

For example:

  • in a class, I will group methods that go together, while separating them by a blank line from the next group.
  • if I need to write a comment I'll usually put a blank line before the comment
  • in a method, I make one paragraph per step of the process

All in all, I rarely have more than 4/5 lines clustered together, meaning a very sparse code.

I don't consider all this white space a waste because I actually use it to structure the code (as I use the indentation in fact), and therefore I feel it worth the screen estate it takes.

For example:

for (int i = 0; i < 10; ++i)
{
    if (i % 3 == 0) continue;

    array[i] += 2;
}

I consider than the two statements have clear distinct purposes and thus deserve to be separated to make it obvious.

So, how do you actually use (or not) blank lines in code ?

© Programmers or respective owner

Related posts about holy-war

Related posts about code-formatting