Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it bad to put a space before a semicolon?

The perlstyle pod states

No space before the semicolon

and I can see no reason for that. I know that in English there should not be any space before characters made of 2 parts ( like '?',';','!' ), but I don't see why this should be a rule when writing Perl code.

I confess I personally use spaces before semicolons. My reason is that it makes the statement stands up a bit more clearer. I know it's not a very strong reason, but at least it's a reason.

print "Something\n with : some ; chars"; # good
print "Something\n with : some ; chars" ; # bad??

What's the reason for the second being bad?

like image 315
jeje Avatar asked Aug 12 '09 14:08

jeje


People also ask

Do you put a space before a semicolon?

Modern style guides recommend no space before them and one space after. They also typically recommend placing semicolons outside ending quotation marks, although this was not always the case.

Do you put a space before and after a semicolon?

Place one space after a comma, a semicolon, and other forms of punctuation falling within a sentence.

Should there be a space before a colon?

But first please note the following: the colon is never preceded by a white space; it is always followed by a single white space in normal use, and it is never, never, never followed by a hyphen or a dash — in spite of what you might have been taught in school.

Is there a space between the semicolon and word?

Semicolons are found between words in a sentence; the semicolon is not a terminal punctuation mark found at the end of a sentence. It immediately follows the letter before it, with no space in between. There is a space after the semicolon, before the beginning of the word that follows.


2 Answers

From the first paragraph of the Description section:

Each programmer will, of course, have his or her own preferences in regards to formatting, but there are some general guidelines that will make your programs easier to read, understand, and maintain.

And from the third paragraph of the Description section:

Regarding aesthetics of code lay out, about the only thing Larry cares strongly about is that the closing curly bracket of a multi-line BLOCK should line up with the keyword that started the construct. Beyond that, he has other preferences that aren't so strong:

It's just a convention among Perl programmers for style. If you don't like it, you can choose to ignore it. I would compare it to Sun's Java Style guidelines or the suggestions for indenting in the K&R C book. Some environments have their own guidelines. These just happen to be the suggestions for Perl.

As Jon Skeet said in a deleted answer to this question:

If you're happy to be inconsistent with what some other people like, then just write in the most readable form for you. If you're likely to be sharing your code with others - and particularly if they'll be contributing code too - then it's worth trying to agree some consistent style.

like image 149
Thomas Owens Avatar answered Nov 15 '22 20:11

Thomas Owens


This is only my opinion, but I also realize that people read code in different ways so "bad' is relative. If you are the only person who ever looks at your code, you can do whatever you like. However, having looked at a lot of Perl code, I've only seen a couple of people put spaces before statement separators.

When you are doing something that is very different from what the rest of the world is doing, the difference stands out to other people because their brain don't see it in the same way it. Conversely, doing things differently makes it harder for you to read other people's code for the same reason: you don't see the visual patterns you expect.

My standard is to avoid visual clutter, and that I should see islands of context. Anything that stands out draws attention, (as you say you want), but I don't need to draw attention to my statement separators because I usually only have one statement per line. Anything that I don't really need to see should fade into the visual background. I don't like semi-colons to stand out. To me, a semicolon is a minor issue, and I want to reduce the number of things my eyes see as distinct groups.

There are times where the punctuation is important, and I want those to stand out, and in that case the semicolon needs to get out of the way. I often do this with the conditional operator, for instance:

my $foo = $boolean ?
            $some_long_value
                  :
            $some_other_value
                  ;

If you are a new coder, typing that damned statement separator might be a big pain in your life, but your pains will change over time. Later on, the style fad you choose to mitigate one pain becomes the pain. You'll get used to the syntax eventually. The better question might be, why don't they already stand out? If you're using a good programmer font that has heavier and bigger punctuation faces, you might have an easier time seeing them.

Even if you decide to do that in your code, I find it odd that people do it in their writing. I never really noticed it before Stackoverflow, but a lot of programmers here put spaces before most punctuation.

like image 22
brian d foy Avatar answered Nov 15 '22 18:11

brian d foy