"get_N" vs "N" in the name of a function

In what cases do you name a function as “get_N”, and in what cases as simply “N”?




In almost all cases I would use noun-less name. Simply to make code feel more declarative in that manner. Caller should not care what callee need to do to return requested value.


The most important factor is consistency. If you have a few with- and a few without the prefix in a module, it’s getting harder to memorize function names. The question “would that function be prefixed or not?” becomes very soon annoying during development.

# bad

The second is clarity. The name should be clear about what is does. In your example, the variant without prefix is totally clear so no need for the prefix. In my opinion ‘user’ would be less clear than ‘get_user’ as it might as well create a user in db, return a new user struct etc…

If you have functions with- and functions without prefixes in a module, consider splitting the functions in two modules as probably the prefixed functions can be placed in an interface module.

Personal flavor
I use the get_n variants for functions which are targeted at a connection/server (e.g. Genserver or Database) For example:

get_n - one item
list_n - multiple items
query_n - compose and return a query

However, if all the functions they are grouped in an ‘noun’ module I use User.get and User.list.


I really like this way of thinking about it: skip get for pure functions, use it when hitting the DB, external service or there are any side effects are involved.

Also the convention in the standard library seems to be for get* to return something or nil, and for fetch to return an ok/error tuple.

1 Like

IMO it’s best if you standardize it across a project, document it in an internal README or a project Wiki and just be done with it. At least people will have a common definition and a convention that way and no confusion will happen [after people get properly onboarded which can sometimes take months, granted].

Stuff like User.get_by or Orders.by_user etc. should be self-explanatory but they are not always.


For example?

Noun-less means that “get_user()” would become “get()”, because a noun is “user”.

“get” what? And what if there’re multiple things a module can “get”?

In modules where there are multiple ways to retrieve something (get_x, get_x!, fetch_x, fetch_x!) I use get_.
If not, I use the noun only.

Note that typically in elixir the convention is: get means “nil on absence”, fetch means “ok/error tuple”, and fetch! means “crash on absence”.


And what if there’re multiple things a module can “get”…

Make it two modules? Lol

Yeah, I mean verb-less, I always confuse these two words.


The story of nouns en verbs helped me finally remember it every time. It also is a fun story to read.