Документация
HTML CSS PHP PERL другое

Section 2.8.  Commas

 
Previous
Table of Contents
Next

2.8. Commas

Place a comma after every value in a multiline list.

Just as semicolons act as separators in a block of statements, commas act as separators in a list of values. That means that exactly the same arguments apply in favour of treating them as terminators instead.

Adding an extra trailing comma (which is perfectly legal in any Perl list) also makes it much easier to reorder the elements of the list. For example, it's much easier to convert:


    my @dwarves = (
        'Happy',
        'Sleepy',
        'Dopey',
        'Sneezy',
        'Grumpy',
        'Bashful',
        'Doc',
    );

to:


    my @dwarves = (
        'Bashful',
        'Doc',
        'Dopey',
        'Grumpy',
        'Happy',
        'Sleepy',
        'Sneezy',
    );

You can manually cut and paste lines or even feed the list contents through sort.

Without that trailing comma after 'Doc', reordering the list would introduce a bug:

    my @dwarves = (
        'Bashful',
        'Doc'
        'Dopey',
        'Grumpy',
        'Happy',
        'Sleepy',
        'Sneezy',
    );

Of course, that's a trivial mistake to find and fix, but why not adopt a coding style that eliminates the very possibility of such problems?

    Previous
    Table of Contents
    Next
    © 2000- NIV