question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: Maintainability rule - Remove unnecessary parenthesis for object initializers.

See original GitHub issue
Product 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:open
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ZizhengTaicommented, Dec 4, 2021

I know this was from five years ago. Has anything been implemented for this? I searched through the documentation but didn’t find anything.

0reactions
Przemyslaw-Wcommented, Apr 27, 2015

Syntactically, it is argument list, yes. Semantically, applied attribute is kinda declarative - it is not “live” code, which is invoked in runtime.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found