Why you should care
It can be confusing to have a class member with the same name (case differences aside) as its enclosing class, especialy when considering the common practice of naming a class instance for the class itself.
Best practice dictates that any field or member with the same name as the enclosing class be renamed to be more descriptive of the particular aspect of the class it represents or holds.
How we detect
CAST Highlight counts one occurrence each time a field has the same name than its class or struct.
Bad CodeÂ
public class Foo { private var foo : String // NonCompliant public func getFoo() -> String { return foo } } struct SuperHero { var superhero : String // NonCompliant var power: String func whoIsIt() { print("Name: " + self.nom + ", Power: " + self.power) } } var foo = Foo() foo.getFoo() // what does this return?
Good CodeÂ
public class Foo { private var name : String public func getName() -> String { return name } } var foo = Foo(); foo.getName()
References
https://kotlinlang.org/docs/coding-conventions.html
https://riptutorial.com/kotlin/example/30735/elvis-operator—–
https://github.com/Kotlin/kotlin-style-guide/issues/18
https://riptutorial.com/kotlin/example/30735/elvis-operator—–
https://github.com/Kotlin/kotlin-style-guide/issues/18
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.