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
  • 10
  • 5
    I see your point, but [`Map.get` in Java returns null](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object-). – lennon310 Jan 23 '21 at 01:11
  • @lennon310 thanks...I misremembered. added a better example instead. – Danack Jan 23 '21 at 02: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
  • 74,672
  • 10
  • 115
  • 187
  • "to express your intent and thoughts accurately" - that's fine for senior programmers, but is less good advice for junior programmers (who don't have exposure to many idioms) or for people who want to communicate with people outside of my own team. – Danack Jan 24 '21 at 19:31
  • 1
    I asked a related question https://softwareengineering.stackexchange.com/questions/419122/does-oop-overemphasize-the-importance-of-noun-and-thus-put-action-verb-in-the-le which probably antagonize OO experts so it was closed and got 2 delete votes. But it was not my intention. I did quite some research and thoughts to write that long question. – Qiulang 邱朗 Mar 13 '21 at 01:40
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.

Greg Burghardt
  • 34,276
  • 8
  • 63
  • 114
  • 3
    Just as an example: in Scala, `Map.get` returns an `Option[A]` and there's a `Map.getOrElse` which takes an alternative default value as its second argument that will be returned if the key cannot be found. Neither of those will ever throw an exception (unless something goes horribly wrong) nor return `null`. Likewise, `Iterable.find` returns an `Option[A]` as well. – Jörg W Mittag Jan 23 '21 at 02:58