PBP: 017 Non-Terminal Expressions

This Best Practice is one that I’ve seen people argue horribly about, and I don’t get it.  The suggestion is to avoid doing a big calculation, with sequences of complex operations, in the middle of  a statement.  Take that complexity, stick it in a variable, and use the variable.

I like this.  In most cases, the more variables that are used, the better.  This usually makes code more readable, as the variable name should tell you the meaning of the calculation, not just the steps that were needed to calculate it.  Even if it’s not a calculation but an extraction from a complex data structure, give it a name!

$thing->{color}->{$level}->[3]->{$relevance};

What is that?  Assign it to a variable called “my $color_choice” and you’ll know exactly what it is.  (And exactly how much you want to strangle whoever built that data structure.  Terrible example names on my part, sorry.)

Leave a Reply