Are trailing commas in Perl a bad practice? [close

2019-03-11 14:26发布

问题:

Today I was in a Webex meeting showing my screen with some Perl code I wrote. My boss suddenly told me while everyone else was looking and hearing that I had to remove trailing commas from my hash and array structures because it is a bad practice. I said I didn't think that was a bad practice in Perl, but he insisted and made me delete those commas just to show my script running in the meeting.

I still think it's not a bad practice in Perl, but I can be wrong. I actually find them pretty convenient and a good practice because they prevent me from adding new elements and forgetting to add the corresponding comma in the process.

But, I'd really like to know if it's a good or bad practice and be able to show it my boss (if he's wrong) with good arguments and even good sources for my arguments.

So, is it a bad practice to leave trailing commas?

This is an example:

my $hash_ref = {
    key1    => 'a',
    key2    => 'b',
    key3    => 'c',
};

my $array_ref = [
    1,
    2,
    3,
];

回答1:

It's a great practice to have the trailing comma. Larry added it because of a common error he saw programmers make. When they added elements to a list (or whatever their language called it), they forgot the separator character. Perl allows the trailing comma on purpose. It's not a quirk or side effect of something else.

What is bad practice, however, is distracting a meeting full of people with something your boss could have corrected later. Unless the meeting was specifically a code review, your boss wasted a bunch of time. I've always wished that to join a video conference, you had to enter your per-minute compensation so a counter would show on everyone's screen to show how much money was being wasted. Spending a couple hundred dollars watching you remove commas on a working program would tamp down that nonsense.



回答2:

So the PBP page referred to by Miller argues for making it easier to reorder the list by cutting and pasting lines; the mod_perl coding style document linked by Borodin argues for avoiding a momentary syntax error when you add stuff.

Much more significant than either, in my opinion, is that if you always have a trailing comma and you add a line, the diff only shows the line you added and the existing lines remain unchanged. This makes blame-finding better, and makes diffs more readable.

All three are good reasons for always using trailing commas, and there are in my opinion no good reasons not to do so.



回答3:

The Apache mod_perl coding style document says this

Whenever you create a list or an array, always add a comma after the last item. The reason for doing this is that it's highly probable that new items will be appended to the end of the list in the future. If the comma is missing and this isn't noticed, there will be an error.

What your manager may have been thinking of is that doing the same thing in C is non-standard and non-portable, however there is no excuse for his extraordinary behaviour.



回答4:

It is indeed a good practice and also mentioned in the famous PBP.

There is actually a Policy for perlcritic which always gets me: https://metacpan.org/pod/Perl::Critic::Policy::CodeLayout::RequireTrailingCommas



回答5:

I favor leading commas, though I know its rather unpopular and seems to irritate the dyslexic. I also haven't been able to find a perltidy option for it. It fixes the line-change-diff problem as well (except for the first line, but that's not usually the one being changed in my experience), and I adore the way the commas line up in neat columns. It also works in languages that are white-space agnostic but don't like trailing commas on lists quite neatly. I think I learned this pattern while working with javascript...

my $hash_ref =
  { key1    => 'a'
  , key2    => 'b'
  , key3    => 'c'
  };

my $array_ref = 
  [ 1
  , 2
  , 3
  ];


标签: arrays perl hash