Why underline on function parameter

Hi all
I am trying to learn phoenix and bought the book programming phoenix.
I copied the code from book that looks as follow:

defmodule Rumbl.UserController do

  use Rumbl.Web, :controller

  def index(conn, _params) do
    users = Repo.all(Rumbl.User)
    render conn, "index.html", users: users
  end

end

Why the author give the parameter name _params with underline? Any reason?
Thanks

http://elixir-lang.org/docs/master/elixir/naming-conventions.html#underscore-_foo-

1 Like

That’s how you explicitly let Elixir know that you don’t care about the value in that variable. If you don’t prepend an underscore, the compiler will bark at you, since you have a parameter that you’re not using.

1 Like

Also the underscored variable will always match, using pattern matching.

So in this example the index function is called regardless what the second parameter acutally is.
You will prob notice later, that an underscored variable or just an underscore can act as an default case using switch/case structures or as a “default” function if there are 2 or more functions with the same name.

Its not good practise but, you can technically use an underscored variable. If you want something that cannot be read from you could use only an underscore.

1 Like

The guy who wrote Programming Elixir 1.2 said he uses underscored variables instead of underscores for documentation. It just makes it a bit more clear on what is being used and what is not.

1 Like

Thanks everyone.
Phoenix is awesome :heart:

1 Like