Are the usage of parentheses in Typespecs optional?

Viewed 78
2 Answers

Yes, the usage of parentheses in typespecs (or in any zero-arity function) is a styling preference: the parser will know how to interpret functions with or without parentheses, but sometimes the readability is improved with them, sometimes without. The docs for mix format include some explanations of how formatting is applied and customized if you use that tool. Including mix format as part of your code commits and validation checks can really help in achieving consistent formatting and cutting down on formatting noise in the code diffs.

Another relevant tool in this discussion is the credo package: it helps to keep your code consistently formatted, and you can learn a lot about the different things that are evaluated by looking over its configuration file .credo.exs. Relevant to your question I think, there is the Credo.Check.Readability.ParenthesesOnZeroArityDefs option.

Yes.

As tempting as it is to leave the one word answer, I feel I need to say more to avoid down votes. Note that hexdocs probably better represents the state of the nation, and the getting starting pages are not updated to standards that often. So I would expect that the preference is to use parentheses for the types. The first option is therefore your best choice, the last is your second best, and I hope most people won't deliberately choose the middle two. Consistency is such a nice thing.

Related