Section 2.14. Elses


2.14. Elses

Don't cuddle an else.

A "cuddled" else looks like this:

     } else { 

An uncuddled else looks like this:

      }     else { 

Cuddling saves an additional line per alternative, but ultimately it works against the readability of code in other ways, especially when that code is formatted using K&R bracketing. A cuddled else keyword is no longer in vertical alignment with its controlling if, nor with its own closing bracket. This misalignment makes it harder to visually match up the various components of an if-else construct.

More importantly, the whole point of an else is to distinguish an alternate course of action. But cuddling the else makes that distinction less distinct. For a start, it removes the near-empty line provided by the closing brace of the preceding if, which reduces the visual gap between the if and else blocks. Squashing the two blocks together in that way undermines the paragraphing inside the two blocks (see the previous guideline, "Chunking"), especially if the contents of the blocks are themselves properly paragraphed with empty lines between chunks.

Cuddling also moves the else from the leftmost position on its line, which means that the keyword is harder to locate when you are scanning down the code. On the other hand, an uncuddled else improves both the vertical separation of your code and the identifiability of the keyword:

      if ($sigil eq '$') {         if ($subsigil eq '?') {             $sym_table{ substr($var_name,2) } = delete $sym_table{$var_name};             $internal_count++;             $has_internal{$var_name}++;         }         else {             ${$var_ref} = q{$sym_table{$var_name}};             $external_count++;             $has_external{$var_name}++;         }     }     elsif ($sigil eq '@' && $subsigil eq '?') {         @{ $sym_table{$var_name} }             = grep {defined $_} @{$sym_table{$var_name}};     }     elsif ($sigil eq '%' && $subsigil eq '?') {         delete $sym_table{$var_name}{$EMPTY_STR};     }     else {         ${$var_ref} = q{$sym_table{$var_name}};     } 

In contrast, a cuddled else or elsif reduces readability by obscuring both the chunking of the blocks and the visibility of the keywords:

     if ($sigil eq '$') {         if ($subsigil eq '?') {             $sym_table{ substr($var_name,2) } = delete $sym_table{$var_name};             $internal_count++;             $has_internal{$var_name}++;         } else {             ${$var_ref} = q{$sym_table{$var_name}};             $external_count++;             $has_external{$var_name}++;         }     } elsif ($sigil eq '@' && $subsigil eq '?') {         @{$sym_table{$var_name}}             = grep {defined $_} @{$sym_table{$var_name}};     } elsif ($sigil eq '%' && $subsigil eq '?') {         delete $sym_table{$var_name}{$EMPTY_STR};     } else {         ${$var_ref} = q{$sym_table{$var_name}};     }



Perl Best Practices
Perl Best Practices
ISBN: 0596001738
EAN: 2147483647
Year: 2004
Pages: 350
Authors: Damian Conway

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net