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.

Typically, when you hit Enter in a smart editor, it copies the indentation of the last line by inserting the same amount of spaces, so if you don't delete them, the code looks like this:

sub sum {
    my @numbers = @_;
    
    my $sum = 0;
    foreach (@_) {
         $sum += $_;
    }
    
    return $sum;
}

(Yes, the example is silly; that sub could be written in two or three lines. :))

To be honest, these lines make my cursor feel dizzy, so I tend to clear them out. (I admit, that this behavior generates commit noise + inconsistency.) Plus, the whitespace are unnecessary bytes. Well, probably none of these does matter, at least in most obvious ways. So I wonder if there is any better (i.e. less subjective and less over-pedantic) reason why the lines should be blanked (by s/^\s+$//).

Note: And please try not to point me to the style guide. Imagine I'm the one writing the style guide: should I inherit rule "delete the extra spaces" or just bravely fight my intuition and leave those peaceful \t's and 's alone?. Or I just want to improve my "own" guide, which I can use where "official" guide is silent or missing.

share|improve this question
9  
If you're not wanting to adhere to a style guide and you're writing your own, it's irrelevant. Do as you please, it's opinion at that point. –  Aaron McIver Dec 15 '11 at 15:39
1  
This seems to be a special case of programmers.stackexchange.com/questions/121555/…. –  delnan Dec 15 '11 at 15:40
    
@Aaron Good point. But I remain undecided and would like to "please" in the most constructive way I can find. That's why I asked here .-) –  Alois Mahdal Dec 15 '11 at 16:10
1  
Just committed a Whitespace "hello world" in our codebase... If someone just deletes it, I'm going to revert with a "please don't delete my code" message... Thanks for the "work holy war / nothing better to do" inspiration... –  Yannis Rizos Dec 15 '11 at 16:20
2  
Are you asking: "Should I delete lines with only whitespace" or are you asking. "Should I delete the extra white space on a line?" –  Loki Astari Dec 15 '11 at 16:24

3 Answers 3

In general, whitespace formatting (and style guides) serve the developers by making code easier to read. If you're the only developer, then the style that makes code easier for YOU to read is the right one. Any rules put in place are highly personal and subjective - there's no definitive right or wrong answer.

share|improve this answer
1  
Maybe I'm the only developer now (I am), but I try to keep in mind that real code (by which I mean everything that's not just a single-use or experimental script) should never be written without consideration of other future readers. –  Alois Mahdal Dec 15 '11 at 18:03
1  
@Alois, that's true. If you're planning on having others read your code, you should give some consideration to following the coding styles that are used by the language that you're coding in. –  JW8 Dec 15 '11 at 18:11

OK, most of the answers and comments are addressing to whether or not you should remove the blank lines... but I see from the question and the regex that what you are really referring to is truncating lines which contain ONLY whitespace to just blank lines.

For example (\t tab and \n newline characters added for clarity):

foo(){\n
\t  int blah\n
\t  \n
\t  blah = 0;\n
\t  \n
\t  return blah;\n
}\n

would be stripped to:

foo(){\n
\t  int blah\n
\n
\t  blah = 0;\n
\n
\t  return blah;\n
}\n

I personally leave in those extra tabs. Let's say I am on line 2 and decide to go down to line 3 and add some code. The cursor will already be in the right place. While hitting enter on line 2 would create a new line with the appropriate indent, I may not be at the end of the line when I want to go down to the next one and add something, so I would prefer to down arrow rather than [End][Enter].

But maybe that's just my habit.

share|improve this answer
3  
That's a side affect of your editor. There are editors out there that correctly handle the situation. –  Loki Astari Dec 15 '11 at 22:50

Because empty heredoc terminators make multi-line quotes pretty (I blame Schwern for this):

\t  my $value = $dbh->selectrow_array( << '', {}, $index );
\t  \t  SELECT value
\t  \t  FROM table
\t  \t  WHERE key = ?

\t  print $value;
share|improve this answer

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.