5

I'm sometimes torn between two naming conventions defining the order of words that make up a function name. The first one is choosing the words in the same order we would natural use in a sentence, for example:

getFilters(...)
getPairedFilters(...)
getUniquePairedFilters(...)

This reads rather naturally but it's not immediately obvious that all of those functions return filters. Only the last part of the function name indicates that. If the name is long, that's not as convenient as the following:

getFilters(...)
getFiltersPaired(...)
getFiltersPairedUnique(...)

This, however, does not read as naturally but the names immediately suggest that they all return filters. Is there some sort of a consensus what name ordering is "better"?

5
  • 2
    As the numbers multiply, my vote would be for getFilters().paired().unique() etc...
    – user949300
    Commented Jan 7, 2021 at 9:11
  • That assumes that getFiltersPairedUnique can be implemented doing three function calls.
    – gnasher729
    Commented Jan 7, 2021 at 17:37
  • I mean what they return is in the signature right? like getFilters(...): Filter[] and getUniquePairedFilters(...): Filter[] you know it returns filters because that's what the return type is. Commented Aug 25, 2022 at 14:52
  • 2
    it's not immediately obvious that all of those functions return filters Really? Seems very obvious to me.
    – John Wu
    Commented Dec 22, 2022 at 8:15
  • I disagree with the presumptive binary choice. The entire method signature and assignment influences naming. Naming patterns like the filters examples is often parameters hiding in the name. Often, when signaling the return type it is not a morphed object type, rather a collection/set. Good (receiving) variable names can help take care of that. Polymorphic naming in context is a significant hint about underlying object and code design.
    – radarbob
    Commented Dec 23, 2022 at 22:56

2 Answers 2

1

Number 1 is correct. English is universal.

According to 2, a function called isTimeSet() should be named isSetTime() which is completely wrong.

Also functions named getWeekDefinition() and getMonthDefinition() whould turn into getDefinitionWeek() and getDefinitionMonth() respectively, which is wrong.

By using number 1, thankfully, we all know that getWeekDefinition() gives us a Definition object and not a Week object.

2
  • "should be named isSetTime()" -- I did not make that conclusion from #2 since the word Filters == noun and Paired == adjective. whereas Set == verb. #2 changes the order of nouns and adjectives. Commented Dec 22, 2022 at 20:36
  • 1
    @MarcelWilson Set, in isTimeSet() is the past participle of the verb to set, used as an adjective, not unlike Paired is the past participle of the verb to pair, used as an adjective. The real verb in ' isTimeSet()` is "is" (to be). Commented Dec 23, 2022 at 8:41
-3

Generally the only people reading function names are programmers. Given that most code factories are under severe deadlines the problem becomes which is the fastest way to exclude possibilities so that the coder can quickly find what they're looking for. So the best solution would be your second (not "get" so ignore, not "filters" so ignore, select appropriate remainder).

2
  • 3
    Let me just say that I completely disagree. People read a short name like getUniquePairedFilters completely and now directly what it means, because it is correct English.
    – gnasher729
    Commented Aug 23, 2022 at 22:00
  • 1
    Yeah, time is limited so let's add some puzzles. Makes perfect sense. Commented Aug 24, 2022 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.