Confusing overloading of methods¶
ID: java/confusing-method-signatureKind: problemSecurity severity: Severity: recommendationPrecision: highTags: - quality - maintainability - readability - namingQuery suites: - java-security-and-quality.qls
Click to see the query in the CodeQL repository
Overloaded method declarations that have the same number of parameters may be confusing if none of the corresponding pairs of parameter types is substantially different. A pair of parameter types A and B is substantially different if A cannot be cast to B and B cannot be cast to A. If the parameter types are not substantially different then the programmer may assume that the method with parameter type A is called when in fact the method with parameter type B is called.
Recommendation¶
It is generally best to avoid declaring overloaded methods with the same number of parameters, unless at least one of the corresponding parameter pairs is substantially different.
Example¶
Declaring overloaded methodsprocess(Objectobj) andprocess(Strings) is confusing because the parameter types are not substantially different. It is clearer to declare methods with different names:processObject(Objectobj) andprocessString(Strings).
In contrast, declaring overloaded methodsprocess(Objectobj,Strings) andprocess(Strings,inti) is not as confusing because the second parameters of each method are substantially different.
References¶
J. Bloch,Effective Java (second edition), Item 41. Addison-Wesley, 2008.
Java Language Specification:15.12 Method Invocation Expressions.