views:

661

answers:

9

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?

+14  A: 

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.

Thomas Owens
+4  A: 

It's not a rule, it's one of Larry Wall's style preferences. Style preferences are about what help you and the others who will maintain your code visually absorb information quickly and accurately.

I agree with Larry in this case, and find the space before the semicolon ugly and disruptive to my reading process, but others such as yourself may find the exact opposite. I would, of course, prefer that you use the sort of style I like, but there aren't any laws on the books about it.

Yet.

chaos
+8  A: 

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.

brian d foy
When celebrated Perl authors refer to the conditional operator (http://perldoc.perl.org/perlop.html#Conditional-Operator) as "the ternary operator", it makes a baby camel cry.
chaos
Sure. ?: is *a* ternary operator, but what you're doing like calling `+` "the binary operator" or `!` "the unary operator".
chaos
chaos's favorite topic once again! I'll get some popcorn.
innaM
@brian d foy: It was wrong to call it that in C, too. And every time someone prominent makes this error in public, more people think it's correct. @Manni: If not for people like you I wouldn't have to bother with it.
chaos
me? the last time I called this thing the ternary operator you corrected me and I haven't used that name since.
innaM
@Manni: Oh, okay. I didn't know; you sounded determined to keep calling it that in the thread. I declare you one of the good guys, then.
chaos
+2  A: 

Well, it's style, not a rule. Style rules are by definition fairly arbitrary. As for why you shouldn't put spaces before semicolons, it's simply because that's The Way It's Done. Not just with Perl, but with C and all the other curlies-and-semicolons languages going back to C and newer C-influenced ones like C++, C#, Objective C, Javascript, Java, PHP, etc.

friedo
+2  A: 

Because people don't expect it . It looks strange when you do it .

Aric TenEyck
The important thing then is who are these people who will be seeing the code.
ysth
+4  A: 

Like others have said, this is a matter of style, not a hard and fast rule. For instance, I don't like four spaces for indentation. I am a real tab for block level indentation/spaces for lining things up sort of programmer, so I ignore that section of perlstyle.

I also require a reason for style. If you cannot clearly state why you prefer a given style then the rule is pointless. In this case the reason is fairly easy to see. Whitespace that is not required is used to call attention to something or make something easier to read. So, does a semicolon deserve extra attention? Every expression (barring control structures) will end with a semicolon, and most expressions fit on one line. So calling attention to the expected case seems to be a waste of a programmers time and attention. This is why most programmers indent a line that is a continuation of an expression to call attention to the fact that it didn't end on one line:

open my $fh, "<", $file
    or die "could not open '$file': $!";

Now, the second reason we use whitespace is make something easier to read. Is

foo("bar") ;

easier to read than

foo("bar");

I would make the claim that is harder to read because it is calling my attention to the semicolon, and I, for the most part, don't care about semicolons if the file is formatted correctly. Sure Perl cares, and if I am missing one it will tell me about it.

Chas. Owens
A: 

I really don't like it. But it's 100% a personal decision and group convention you must make.

Santi
+2  A: 

Feel free to put a space. The important thing is that you be consistent; consistency allows you to more readily spot errors.

There's one interesting coding style that places , and ; at the beginning of the following line (after indentation). While that's not to my taste, it works so long as it is consistent.

Update: an example of that coding style (which I do not advocate):

; sub capture (&;*)
   { my $code = shift
   ; my $fh   = shift || select
   ; local $output     
   ; no strict 'refs'
   ; if ( my $to = tied *$fh )
      { my $tc = ref $to
      ; bless $to, __PACKAGE__
      ; &{$code}()
      ; bless $to, $tc
      }
      else
      { tie *$fh , __PACKAGE__
      ; &{$code}()
      ; untie *$fh
      }
   ; \$output
   }

A defense can be found here: http://perl.4pro.net/pcs.html.

ysth
Aaaaarghhhhhhh! Commas and semicolons at the beginning of the line mean style war!!! ;-)
Sinan Ünür
Wow. That block just made my eyes bleed. I'm too instilled with the standard convention of semi-colons at the end of the line. I'd probably just curl into a ball and cry for a while if I inherited code like this, lol.
Eric
A: 

Code style is just a set of rules to make reading and maintaining code easier. There is no real bad style, but some are more accepted than others. And they are of course the source for some "religious battles" (to call curly braces style ;-) ).

To make a real life comparison, we have trafic lights with red, yellow/orange and green. Despite the psychological effects of the colors, it is not wrong to use Purple, Brown and Pink, but just because we are all used to the colors there are less trafic accidents.

Gamecat