2

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 = @_;
   &nbsp
    my $sum = 0;
    foreach (@_) {
         $sum += $_;
    }
   &nbsp
    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.

Alois Mahdal
  • 146
  • 6
  • 10
    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 http://programmers.stackexchange.com/questions/121555/why-is-trailing-whitespace-a-big-deal. –  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
  • @AaronMcIver ...or you actually answered the question "no"? – Alois Mahdal Dec 15 '11 at 16:14
  • @AloisMahdal Yes the answer is no, since you state; _So I wonder if there is any better (i.e. less subjective and less over-pedantic) reason_ – Aaron McIver Dec 15 '11 at 16:17
  • 1
    Just committed a [Whitespace](http://compsoc.dur.ac.uk/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 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?" – Martin York Dec 15 '11 at 16:24

3 Answers3

5

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.

JW8
  • 2,669
  • 3
  • 21
  • 22
  • 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
2

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.

Tevo D
  • 342
  • 1
  • 2
  • 4
2

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;
Nic Wolff
  • 21
  • 1