Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update S1200: Rule should ignore 'nameof()' references #2123

Closed
saciervo opened this issue Nov 26, 2018 · 2 comments
Closed

Update S1200: Rule should ignore 'nameof()' references #2123

saciervo opened this issue Nov 26, 2018 · 2 comments
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@saciervo
Copy link

saciervo commented Nov 26, 2018

Description

Using the nameof() keyword operator counts against the threshold of Rule S1200.

While it is technically true that a type used as a nameof() parameter will be coupled to the class you are writing, I think it is not a problematic dependency because it is only about the name of the type and not about behavior or responsibility.

Repro steps

Create a class and write some code containing several nameof()'s that point to several other classes and namespaces than the one you are writing.

Expected behavior

The analyzer should not demand to split the class into smaller and more specialized ones.

Actual behavior

The analyzer demands to split the class into smaller and more specialized ones.

Known workarounds

None

Related information

  • SonarC# Version 7.8.0.7320
  • Visual Studio Version 15.9.2
@Evangelink Evangelink self-assigned this Dec 3, 2018
@Evangelink Evangelink added this to the Support milestone Dec 3, 2018
@Evangelink
Copy link
Contributor

Hi @saciervo,

Thank you for the feedback. You are right!
I still think it would be worth somehow to capture the kind of link/dependency created through nameof.

Out of curiosity, would you mind sharing some use cases where you would need to reference a class or method name without having a behavioral dependency on it?

@Evangelink Evangelink modified the milestones: Support, Rules Dec 3, 2018
@Evangelink Evangelink removed their assignment Dec 3, 2018
@Evangelink Evangelink added Area: Rules Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Dec 3, 2018
@Evangelink Evangelink changed the title S1200: Consider ignoring 'nameof()' references Update S1200: Rule should ignore 'nameof()' references Dec 3, 2018
@saciervo
Copy link
Author

Hi @Evangelink

Sorry for the late reply. The problem occured in a method that generates an SQL command for multiple tables. Usually we use EntityFramework for database access, but in this special case we needed raw SQL. Instead of hard coding the table names, my co-worker used nameof() with the entities. The method is not that large and has even less complexity (your typical StringBuilder scenario), so she was baffled by this message from the analyzer and asked me why that is.

It's not a particularly strong case, but I just couldn't see what the analyzer had to complain about this method at first sight. It was only at second glance that I suspected what might be the reason. Then I thought I should create an issue for this, because for me and her, it was just not obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

2 participants