3

I was trying to explain something to a junior programmer the other day, and as part of it I had to explain that:

  • a method with 'find' as the verb in it, will either find an item and return it, or return null.
  • a method with 'get' as the verb in it, will either return an item, or throw an exception if it is unavailable.

Those two things are standard in PHP and other programming languages.

This made me realise that there are quite a few 'standard behaviours' associated with certain verbs, that would be difficult for people to learn about.

Is there a list any where of standard verbs to use, with the standard behaviour they should have, that would avoid confusion in assumptions?

Another example is that a junior programmer might use the verb 'make' in a method named makeHttpRequest to indicate that a HTTP request will be performed. But for me 'make' is ambiguous, and often means 'create'. So for this one, execute or dispatch would probably be more standard.

Danack
  • 345
  • 1
  • 2
  • 11

2 Answers2

2

Programming languages are means to express our ideas. If there would be only one canonical way to express them, all the original subtleties of our thoughts would get lost in translation.

There are of course a few broadly accepted conventions such as getXxx, setXxx, isXxx, canXxx, but it already stops at makeXxx which has to compete with createXxx and buildXxx. Usually such conventions belong to the typical idioms of the language and you can find them in every tutorial.

But if you’re asking, you’re probably already out of that idiomatic space. Therefore, use your liberty and the richness of the language to express your intent and thoughts accurately (e.g. find vs search).

Christophe
  • 81,699
0

There is no canonical list of verbs for this. At best you could identify a naming convention that is specific to a programming language, framework or library. Failing that, decide as a team what the idiom should be.

Naming things is notoriously difficult and subjective. The convention of find returning null and get throwing an exception is no different.