False Positive on squid:S1845

259 views
Skip to first unread message

sraul....@gmail.com

unread,
Feb 10, 2017, 9:34:22 AM2/10/17
to SonarQube
Hello.

This rule says: Methods and field names should not be the same or differ only by capitalization

However, this rule ignores the particular case of Builder pattern.
In a Builder class, is very common to have methods with same field name, to provide a Fluent API, such as:

public static class MyClassBuilder {
 
   
private String name;

   
public MyClassBuilder name(String value) {
       
this.name = value;
       
return this;
   
}


   
public MyClass build() {
       
return new MyClass(name);
   
}
}

Of course, we could argue that we could use a different name for the method, such as withName() but I don't think we should need to change the code in those cases where the field in only used for the build procedure, should not cause any confusion at all.

What do you think? Could it exclude Builder classes? Maybe using the class name suffix "Builder" would be sufficient.


Brian Sperlongano

unread,
Feb 10, 2017, 3:38:28 PM2/10/17
to SonarQube, sraul....@gmail.com
Hello,

The builder pattern is one of the practices encouraged in Effective Java by Joshua Bloch.  I agree that this should be allowed for builders but should be disallowed in other cases.  Perhaps SQ could ignore methods that return this?  The hard part here seems to be in coming up with a rule that clearly establishes the cases under which this is permitted.

-Brian

sraul....@gmail.com

unread,
Feb 10, 2017, 3:51:42 PM2/10/17
to SonarQube
Another option would be to simply allow to configure an exclude pattern for class names. This would give more flexibility to configure for particular cases.

Nicolas Peru

unread,
Feb 22, 2017, 11:20:13 AM2/22/17
to sraul....@gmail.com, SonarQube
Hi, 

Thanks for the feedback. I tend to agree with your analysis hence I created a ticket to tackle the issue : https://jira.sonarsource.com/browse/SONARJAVA-2134

Cheers, 

Le ven. 10 févr. 2017 à 21:51, <sraul....@gmail.com> a écrit :
Another option would be to simply allow to configure an exclude pattern for class names. This would give more flexibility to configure for particular cases.

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/7fd7f9f9-fa9e-4f38-bb1e-555c35b1c5d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Nicolas PERU | SonarSource
Senior Developer
http://sonarsource.com
Reply all
Reply to author
Forward
0 new messages