A field should not duplicate the name of its containing class

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

5362

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.