Curious why so many Elixir devs omit `get_` in function prototypes?

I’m not religious about this at all, but it seems like an easy quality of life improvement to always prefix functions which get things, with the word get. e.g. get_name instead of name. It’s an easy way to distinguish between variables and functions. e.g. name = get_name().

Anybody with historical know-how, know how/why this pattern developed? So curious!

1 Like

The easier way to distinguish between variable names and function calls is the fact that there are arguments to the function call… get_name/0 is not really something that would make sense… Usually its a get_name/1/name/1.

Though even more idiomatic is usually to just access the field of the struct.

get_* usually implies that the result can be “empty” (in which case usually nil is returned), while fetch_* implies that it can be empty and then we use :ok/:error to distinguish, while fetch_*! would raise in case of an “empty” result.

Those idioms might be another reason why get_ is omitted for “unfallible” access.

6 Likes

Likely because put_name does not make much sense in an immutable language i.e. you cannot modify the internal state of whatever module the function is called on.

1 Like

Sure you can. It can go in the database, a GenServer’s state, etc.

1 Like

You might not be able to mutate state but you can create a new version of the state which includes the changes, which is generally how you work with data.

1 Like

Obviously. Wasn’t talking about that though. :slight_smile: