Previous Page
Next Page

2.5. Keys and Indices

Separate complex keys or indices from their surrounding brackets.

When accessing elements of nested data structures (hashes of hashes of arrays of whatever), it's easy to produce a long, complex, and visually dense expression, such as:

    $candidates[$i] = $incumbent{$candidates[$i]{get_region( )}};

That's especially true when one or more of the indices are themselves indexed variables. Squashing everything together without any spacing doesn't help the readability of such expressions. In particular, it can be difficult to detect whether a given pair of brackets is part of the inner or outer index.

Unless an index is a simple constant or scalar variable, it's much clearer to put spaces between the indexing expression and its surrounding brackets:

    $candidates[$i] = $incumbent{ $candidates[$i]{ get_region( ) } };

Note that the determining factors here are both the complexity and the overall length of the index. Occasionally, "spacing-out" an index makes sense even if that index is just a single constant or scalar. For example, if that simple index is unusually long, it's better written as:

    print $incumbent{ $largest_gerrymandered_constituency };

rather than:

    print $incumbent{$largest_gerrymandered_constituency};

    Previous Page
    Next Page