Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter naming convention

I have a mysql function that takes a string as input and returns only the alphanumeric characters.

101-FFKS 99S-5 would output 101FFKS99S5 for instance.

It was called alphanum which isn't very descriptive.

I was considering something along the lines of alphanum_filter or punctuation_filter.

What is the agreed convention? The thing you're filtering out, or the thing you're left with?

A few google searches didn't yield anything helpful.

like image 860
Trav Easton Avatar asked Aug 29 '17 12:08

Trav Easton


1 Answers

I don't think there's any agreed convention in programming.

Note that there are three, not two options, as to the thing to put near "filter":

  • what you're filtering out,
  • what you're left with
  • and the combination of both, that is the substance which gets put into the filter.

Indeed I think that in general when in English you see a word near "filter" you think that that word is what will be put into the filter.
Note that while "air filter", "water filter", "oil filter" and "fuel filter" might seem to refer to what is left after the filtering, I strongly believe that there's an implicit "unfiltered" before all of them.

However there also nouns that really include "what you're left with", such as "high-pass filter" or "red filter", and ones that include "what you're filtering out", such as "spam filter".

So it will probably be equivocal in any case.
Personally at first glance I'd think that the word would be what is being filtered out, so I'd be more comfortable with punctuation_filter, but it's probably subjective.
So it would be better to find a less ambiguous name (although in some cases it's ok to use something ambiguous and let each programmer understand what it does by looking at it's source; what's much more important is to be consistent, you sure don't want a punctuation_filter that filters out punctuation and a num_filter that lets only numbers through in the same code).

An idea for a clearer name might be LeaveOnlyAlphanums.
Yes, it's an assertion rather than a noun, but the only unambiguous noun phrases you could use are probably FilterThatLeavesOnlyAlphanums, or ThingThatLeavesOnlyAlphanums.

An additional idea is to use FilterOut instead of simply Filter. That's for sure unambiguous, although it's an assertion as well.


If you're looking for something with some sort of authority in the field of programming, I found an explicit mention that filter is ambiguous in The Art of Readable Code.

like image 126
gbr Avatar answered Sep 19 '22 02:09

gbr