Who defines "Compliant" and "Non-Compliant" rules?

461 views
Skip to first unread message

jae...@wayoftheleaf.net

unread,
Feb 22, 2018, 5:18:24 PM2/22/18
to SonarQube
So,

I really have no idea where to ask this.  StackOverflow is well, stack overflow, and GitHub is for actual issues.  But I have wider ranges of curiosities and interests.  I don't just think about the how, or what, I often times question the root origin of WHY. 

Take for example the IComparable<T> interface in C#.  It is a simpye designed interface allowing any object to write a comparison method for future evaluations. 
The concept of an interface is such that the consuming code does not need to know or care about the implementation of the process only that, the calling convention follows a standard.

IComparable<T> has a single method:  CompareTo. 
Yet, SonarLint/Qube seem to suggest that by just implementing the IComparable<T> interface this is somehow non-compliant code.
RSPEC-1210
Who made the final word on that?  No, seriously, I want to know?  Where is it defined ANYWHERE that IComparable<T> implementation should also include the Equals() override, and the static operator overrides for ==, !=, <, > <=, and >=?  Not to mention the RSPEC-3875 rule which complains about overriding the == operand to begin with.  Catch-22 there. 

IComparable<T> is an interface that "Compares" the implementing object to an object of type T.  There is no rule or requirement that says the "CompareTo" operation must abide by the same rules as the == operator or Equals(object) method.  There is no rule that the generic T type parameter must be the same time as the class implementing IComparable<T>.  I can have class Foo implement IComparable<Bar> and that is not non-compliant.  That is what the generics and interfaces are for.

To be frank, actually, there is a rule:  It's called IComparable<T> does NOT have a member called Equals(object).  Which means, compliant implementation of IComparable<T> has nothing to do with the Equals() method, or the operator overrides.

I get the rules that say: this variable is unused.  This assignment to this variable is unnecessary.  But these "Compliant" and "Non-Compliant' determinations are arbitrary, and 100% opinion based.  So, who, where, what is the group that manufactures these arbitrary "Compliance" determinations? 

Regards
Jaeden "Sifo Dyas" al'Raec Ruiner

Scott B.

unread,
Feb 23, 2018, 6:22:27 AM2/23/18
to SonarQube
Hi.

First of all, I'm not from SonarSource.

I think your tone is too agressive. There's no need for that.

So let's think about what you're saying. Suppose I have an instance of a class that implements IComparable, but don't override Equals or operators. I can get the following results:

a.CompareTo(b) returns 0 // equality
a == b returns false
a.Equals(b) returns false

It's strange, right?

Also, I understand the rule RSPEC-1210 is based on CA1035 from Microsoft:

Types that define a custom sort order implement the IComparable interface. The CompareTo method returns an integer value that indicates the correct sort order for two instances of the type. This rule identifies types that set a sort order; this implies that the ordinary meaning of equality, inequality, less than, and greater than do not apply. When you provide an implementation of IComparable, you must usually also override Equals so that it returns values that are consistent with CompareTo. If you override Equals and are coding in a language that supports operator overloads, you should also provide operators that are consistent with Equals.

Of course, there is some exceptions, but you can mark these as "won't fix" in the SonarQube UI if you want.

Colin Mueller

unread,
Feb 23, 2018, 8:40:15 AM2/23/18
to SonarQube
To extend on what Scott is saying (also not from SonarSource), you can always exclude rules from your Quality Profile that you don't agree with, aren't helpful to you, etc.

Sometimes it's also helpful to browse their Rules Repository project on JIRA to see why a rule originated (https://jira.sonarsource.com/browse/RSPEC-1210), which in this case points to this documentation: https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

I also agree with Scott that your tone is overly aggressive.

Colin

G. Ann Campbell

unread,
Feb 23, 2018, 9:09:04 AM2/23/18
to SonarQube
Hi all,

I think @Scott and @Colin handled the meat of your specific rule question (thanks guys!) so I'm going to skip that and just chime in to say that you have found the right place to come with this type of question. In fact, any time you feel the need to initiate a discussion (as opposed to the transactional question/answer nature of interactions on StackOverflow) you should bring it here.

And discussions about the 'whys' behind rules - and suggestions for new rules - are welcome in this friendly community.


:-)
Ann

Amaury Leve

unread,
Feb 23, 2018, 11:44:48 AM2/23/18
to G. Ann Campbell, SonarQube
Hi all,

@Scott, @Colin Thanks for your feedback!

@Jaeden To be fair when we have implemented these rules we spent quite a lot of time reading blogs and threads from Microsoft and well known developers (Jon Skeet and others) because we were really surprised by how much those different rules were linked.

For example if you have a look at the following rules from FXCop you'll see how crazy it is (but at the same time it does make sense):
  • CA2218 states that you should override GetHashCode when overriding Equals
  • CA2226 states that operators should have their symmetrical overload
  • CA2225: Operator overloads should have named alternate (methods)
  • CA2224: Override Equals() when overload ==
  • CA1036: When implementing IComparable<T> overload ==, !=, <, > and override Equals
  • CA1013: Overload == when overloading +, -
And if you want to see more WTF I suggest you to read articles about IEquatable<T> and IEqualityComparer<T>.

Cheers,
Amaury

--
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/5a0f37b7-b4dd-4c85-bea1-ad48114b224f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Amaury Levé | SonarSource

Software Developer - .Net Team

http://sonarsource.com


Are you using SonarLint in your IDE? 
Reply all
Reply to author
Forward
0 new messages