PBP: 031 Ambiguous Names

Some words are easily misinterpreted, and the PBP suggests avoiding them for names.  I don’t agree with one of the things it suggests you always avoid, though…

The book contains a list of potentially confusing names, some of which are more likely to confuse things than others.  It eventually summarizes, “Any homograph can potentially cause difficulties if it has a distinct, non-programming-related sense that is relevant to your particular problem domain.”  This is true, and I agree with it.

Most of the suggestion boils down to, “Use your brain and try not to pick ambiguous words.” which I can live with.

But… it also says, “set” and “last” are both always ambiguous and should probably be avoided.

I can’t agree with this.  If you’re using the split names for accessors – rather than a single mutator function that is both get and set – you’ll have a “get_whatever” and a “set_whatever”.  That verb is at the beginning and clearly an accessor.  If you wind up having to set a set, find a different way to refer to the set, or duplicate it, “set_data_set”, which uses “set” in two ways and may actually still be clear.

While I almost never use separate setter/getter functions – feh!  Context tells me what it’s doing, I don’t need four extra bytes for clarity!  This is Perl, not Java! – they are pretty much an industry standard and to avoid using “set” because a mathematical set somewhere might get confused seems a little over the top.

 

 

One Response to “PBP: 031 Ambiguous Names”

  1. Aaron Priven says:

    You didn’t talk about “last.” I do think “previous” and “final” are good words and generally less ambiguous than “last.”

Leave a Reply to Aaron Priven