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?

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement