Rule proposal: Maintainability rule - Remove unnecessary parenthesis for object initializers.
See original GitHub issueProduct p = new Product() { Id= 10, Name = "Cup" };
should be
Product p = new Product { Id= 10, Name = "Cup" };
Originally posted by ericslattery http://stylecop.codeplex.com/workitem/6793
follow up discussion:
andyr wrote Feb 4, 2011 at 9:47 PM Jason, please review and comment. Then I’ll fix/add.
andyr wrote Mar 15, 2011 at 5:32 PM From Jason, “Love it”
Tratcher wrote Jul 19, 2012 at 7:04 PM Dislike. If we must choose one or the other I’d rather require the parens than prohibit them.
Keeping the parens is more consistent with normal constructor requirements. E.g. If I already had new Products(); adding {…} should not force me to remove the parens.
Also, the parens will always be required for any constructors with parameters, so it is inconsistent to prohibit them on the parameter-less constructor.
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Why are C# 3.0 object initializer constructor parentheses ...
It seems that the C# 3.0 object initializer syntax allows one to exclude the open/close pair of parentheses in the constructor when there...
Read more >Java static code analysis: Unused "private" fields should be ...
If a private field is declared but not used in the program, it can be considered dead code and should therefore be removed....
Read more >Object and Collection Initializers - C# Programming Guide
The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax).
Read more >Unused local variables should be removed
If a local variable is declared but not used, it is dead code and should be removed. Doing so will improve maintainability because...
Read more >C++ Core Guidelines
The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I know this was from five years ago. Has anything been implemented for this? I searched through the documentation but didn’t find anything.
Syntactically, it is argument list, yes. Semantically, applied attribute is kinda declarative - it is not “live” code, which is invoked in runtime.