Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you omit the parenthesis from attributes with no params?

I noticed when I have [Serializable] instead of [Serializable()], the code still compiles.

Is there a rule for this that you can omit the parentheses? Is it a good practice? It seems more readable to me unless I am missing something.

like image 642
Joan Venge Avatar asked Mar 15 '10 15:03

Joan Venge


3 Answers

There's no particular rule regarding best practices that I've seen anywehere on the subect.

Generally, I find that people who hand-type their attributes omit the empty parens, but code generation almost always explicitly includes them.

like image 192
Randolpho Avatar answered Oct 19 '22 21:10

Randolpho


You can miss off the () when the attribute constructor you want to use has no parameters, and you don't want to specify any optional parameters (property values).

See here for more details

like image 26
thecoop Avatar answered Oct 19 '22 21:10

thecoop


Yes omitting the attribute is legal from a language standpoint. I don't know of any best practices surrounding it but in general I find developers omit the parens if they're not necessary.

like image 25
JaredPar Avatar answered Oct 19 '22 19:10

JaredPar