2

I am using IntelliJ PMD plugin, and it gives me LOD violation, on the if(keys[i].equals(key)). Keys is an object in the same class within the function.

public Object get(Object key) {
    int n,i;
    for(i=0,n=0;i<keys.length;i++) {
        if(n >= nelems)
            break ;
        if ( keys[i] == null )
            continue;
        if(keys[i].equals(key))
            return values[i] ;
        n++ ;
    }
    return null;
}

You can find the whole code here : https://raw.githubusercontent.com/Sable/abc/master/benchmarks/Jigsaw/src/classes/org/w3c/util/ArrayDictionary.java

Linus Fernandes
  • 498
  • 5
  • 30
onurtore
  • 195
  • 1
  • 11
  • Does the warning go away if you do `key.equals(keys[i])`? Are you sure the warning isn't for `return values[i]`, where you'd be potentially exposing inner data (for use outside the class) rather than having the object perform actions on that data? – Vince Oct 17 '18 at 13:08
  • @VinceEmigh no it still there after I changed it. And I am sure that warning isn't for return values[i]. Even with the current case, I am not sending any message using keys[i].equals(key) outside of the function. So i do not understand why there is n law of demeter violation. – onurtore Nov 09 '18 at 22:56

1 Answers1

0

If you don't wish to have this flagged as an error, you can wrap the the indexed field keys[i] as new String(keys[i]) and call the equals function on this new object. This ugly hack will defeat the rule.

However, a better solution , in my opinion, is to filter base classes such as String, Integer, Long etc from this rule. Secondly, immutable classes should also be exempt from this rule. An annotation @Immutable can be used to achieve this. This would have to be implemented by the PMD team.

Another option would be to define the rule by class boundaries rather than object boundaries. If a method returns an object of the same class or a primitive class, that object is not to be counted. If a primitive type, it must be immune from application of the rule.

This method chaining piece of the rule becomes tiresome otherwise.

Linus Fernandes
  • 498
  • 5
  • 30