Incerteza
Incerteza

Reputation: 34884

Using () for calling a function with no parameters

What's a convention for using or ommitting parentheses when calling a function which accepts no parameters?

function1(aaa) |> function2() |> function3() |> function4(bbb)

# or

function1(aaa) |> function2 |> function3 |> function4(bbb)

Upvotes: 4

Views: 1931

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

I believe, there is no convention on it yet.

Pro

  • Elixir compiler warns about calling functions without parentheses outside of chains.
  • There is a risk to override a method call without parentheses with a local variable.

_

defmodule A do
  def a, do: "¡Yay!"
  def b1, do: IO.puts a
  def b2 do
    a = ":("
    # 100 LOCs
    IO.puts a
  end
end

iex> A.b1
"¡Yay!"
iex> A.b2
":("

Contra

  • Credo code analysis tool would report zero-arity function calls with parentheses by default.
  • Elixir code itself does not seem to use parentheses.
  • Aesthetics.

Conclusion

In my humble opinion, the rule of thumb “use parentheses when calling functions” has very little advantages, and global suppressing of credo’s guard does not worth it. I personally do not use them, unless it improves the readability. Hope it helps.

Upvotes: 4

Related Questions