What is the common preference/recommendation in the community about using parentheses for function calls in Phoenix templates?

After a long period without doing anything related to Elixir/Phoenix I came back and just upgraded a former project to Phoenix 1.6. Many things have changed but I’m starting to get used to things again at my own pace. :turtle: ^^

Now I’m using heex templates and even added the HEEx HTML Formatter as plugin to mix format.

I see that this formatter plugin will add parentheses to any function call inside heex templates.

For example

<%= render “edit_profile.html”, some_func(assigns) %>

will become

<%= render(“edit_profile.html”, some_func(assigns)) %>

Personnally I find the first synthax prettier and less cumbersome.

And also when I used generators like phx.gen.auth they adopt the first style as well.

So my actual question is:

Is it just a matter of preference or is there a synthax that is encouraged or recommended inside templates?

Thanks. ^^

1 Like

I think it’s a matter of preference. But seeing as you asked, the correct preference is to use parentheses for all function calls and optionally, leave them off for macros :wink:

2 Likes

Why optionally leave them off for macros? That would requiring knowing whether something is a function or a macro, and if you’re not aware of the distinction (as with most beginners), it would look like a random mix (no pun intended), leading to the impression that there is no style.

Usually the macros, which are setup to not have parenthesis added, are not even perceived as function- or rather macro calls in the first place: defmodule, def, schema, all the phoenix router macros, …

2 Likes

Agreed. My personal standard (and I think this may be something of an unspoken convention?) is:

  • Macros that modify the environment = no parens. Examples like those you stated.
  • Macros that are “pure” in the sense that they act like functions, but are macros to allow for additional power/expressiveness = parens. Examples include much of the Ecto query api.
3 Likes

Fair enough for that kind of macro… but what about macros that “look like” functions? Maybe the dividing line could be, rather than function vs. macro, “what looks like a function” vs. everything else.

1 Like

Further to what @LostKobrakai and @zachallaun said, sometimes people create DSLs with macros and it is nicer on the eye to leave off the parenthesis.

1 Like