aethereus

aethereus

Why are there different naming styles?

An irrelevant question, there are function names like Integer.is_odd, and there are function names like Enum.empty?, why the two different naming styles?

Marked As Solved

christhekeele

christhekeele

Because not having a way to recognize the special-case guard-enabled functions would be more confusing to new-comers.

Guards are used much, much more often than they are created—they are possibly typed out more than any other function or macro, if and def included. This is especially true of the is_ type-check guards since they are the only way to switch on type information, in or outside function heads. So you will rapidly become fluent with this special casing.

We want new-comers to learn fast and well the fact that you can’t throw just any predicate function into a guard. Accepting the cognitive burden of remembering which functions are allowed in guards is far more important than the burden of remembering to use a different naming style when writing them, and if they were named identically it would be much harder to memorize which is which for the first time.

Additionally, this naming difference helps visibility and discoverability: we want these special cases to stand out in documentation, we want people like you to notice the discrepancy and take note of it like you have here, instead of frustratedly filing bug reports about how only some predicates seem to work in guards. :smile: So I think it has proved an effective decision.

Also Liked

michalmuskala

michalmuskala

As a side note, this convention is explained in the Naming Conventions doc page.

11
Post #5
mjadczak

mjadczak

Traditionally, Erlang uses is_xxx for functions which return a boolean value. In particular, functions which can be used in guards, of which many are indeed is_xxx functions, were taken wholesale into Elixir from Erlang. Currently, in Elixir, the is_xxx format serves as an indicator that a function / method can be used inside a guard. Since Integer.is_odd/1 is a macro which can be placed into a guard, it also follows this convention. Most other Elixir functions which return a boolean value, such as Enum.empty?/1 cannot be placed in guards, and follow the normal Elixir xxx? naming convention.

aethereus

aethereus

But non-boolean guard-friendly functions such as div and rem do not have such indicator, why should boolean functions be a special case? For fresh Elixir programmers, remembering different naming styles can be a burden, so why not use xxx? style exclusively?

Last Post!

michalmuskala

michalmuskala

As a side note, this convention is explained in the Naming Conventions doc page.

11
Post #5

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement