Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was wondering what is the convention for skipping lines? Do we group similar things together, skip no lines or just group everything together except if statements or similar constructs. If there is no convention for this, then tell me how you do it and at the end ill determine if i want to use any. Currently i skip no lines.

Languages - C, C++, C#, PHP and Java

share|improve this question

migrated from stackoverflow.com Feb 1 '11 at 14:27

This question came from our site for professional and enthusiast programmers.

2  
You need to mark questions that you have asked as answered if someone has answered them for you. –  Joe Feb 1 '11 at 14:08
1  
@Brandon_R : Accepting rate means, accepting the answers which are provided to your questions. –  Abimaran Kugathasan Feb 1 '11 at 14:35
    
Have you looked at other people's code? How much? What projects? –  S.Lott Feb 1 '11 at 16:34
    
Imaging reading any book full of detailed instructions in short hand jargon that didn't separate any sentences with blank lines, pretty un-readable I would imagine. –  Jarrod Roberson May 4 '12 at 16:40
    
It depends on language and personal preference. I generally do 1 line between functions/methods/properties, 2 between classes, 2 between imports and code. Anything smaller than that gets whitespace only if it logically makes sense to group lines together for better readability. –  Evan Plaice May 4 '12 at 17:53
add comment

7 Answers

up vote 7 down vote accepted

I usually leave single empty lines between large blocks, Examples include methods, large loops, and large if statements. (I know, I know, those should be refactored out...)

I also put empty lines before and after multi-line-statements (in many cases at least). But before a closing bracket, I never write an empty line.

function foobar(foo, bar) {
   doSomething();

   foo.callSomethingWithManyParams({
      foo: "foo",
      bar: "bar",
      foobar: "barfoo"
   });

   if (foo.bar>bar.foo) {
     foo.foo();
     bar.bar();
     foo.foobar(bar);
     bar.foobar(foo);
   }
}

function barfoo(bar, foo) {
   foobar(foo,bar);
}
share|improve this answer
    
Thanks, helped alot. –  Brandon_R Feb 1 '11 at 17:00
add comment

There is no convention, but blank lines improve readability. Without them it's like reading a book without paragraphs and there's no good reason to leave them out.

share|improve this answer
1  
+1 for "No rules beyond readability". The only reason for whitespace AT ALL (except obviously in languages where it carries syntactic meaning) is for readability. So do whatever is appropriate. –  Dan Ray Feb 1 '11 at 16:10
2  
+1 for the analogy to paragraphs -- without realizing, that's exactly how I use blank lines. If the instruction is part of a new thought, it become a new paragraph, if it's part of a string of instructions, I group lines together. –  NickC Feb 25 '12 at 21:40
add comment

If your employer has a source code style guide, use that religiously. Otherwise, aim for legibility. I use white space (double newline) on a fairly ad-hoc basis to separate logical chunks of code within method bodies, and always between methods and functions. I try never to have any but the most closely related classes in the same compilation unit, but use at least a double newline if I'm forced to combine classes in a single compilation unit.

share|improve this answer
add comment

In my experience the conventions vary depending on company or project. I think the only real convention out there is to use a double newline between classes, functions and methods.

Personally I prefer to group stuff by using a double newline as a delimiter. Same as in normal writing (paragraph) really.

share|improve this answer
add comment

Like the others, I try to group statements by logical concept. For example, code that initializes and works with a particular variable or object, or a set of statements that defines a loop.

I personally find that the code tends to be more readable if there's a line break every 10-12 lines at most, but I don't hold myself to any hard rule. If applicable, I'll have a comment as a header for each section.

I have a few personal quirks that I don't see too often in other people's code. I like to have two line breaks between functions or classes, to further accentuate their distinctness. Another is no blank lines in functions of 3 statements or less... these small functions look too "spread out" to me if I add any blank lines to them.

share|improve this answer
add comment

I'll throw in a blank link between functions/methods as well as between variable definitions (if applicable) and code. That's it though. I've come to really dislike the "blank line everywhere" approach that a lot of people are taking nowadays. It doesn't help readability in so many of the cases I've seen. On the contrary, since I can't keep as much code on the screen, I have to scroll around more which is a distraction.

share|improve this answer
    
+1 - "Everything in moderation", to quote Jeff Atwood. –  jmort253 Feb 2 '11 at 15:22
add comment

If company don't restrict you into any coding styles, then I usually follow this style

f1()
{
    //related variable initializtion in a group
    //one blank between related 

    //code chunk related to below for loop
    //one blank line before chunk and for loop

    for()
    {

    }

    //one blank line between loop blocks        
    while()
    {

    }
}


//two blank lines between two methods
f2()
{
    f1()

    //one blank line between methods calling
    f1()
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.