org.sonar.l10n.java.rules.squid.HiddenFieldCheck.html Maven / Gradle / Ivy
Shadowing fields with a local variable or with a method parameter is a bad practice that reduces code readability: It makes it confusing to know whether the field or the variable is being used.
Noncompliant Code Example
class Foo {
public int myField;
public void doSomething() {
int myField = 0;
...
}
public void doSomethingElse(int myField) {
...
}
}
Exceptions
Constructors and setters are exceptions; it is common practice to name arguments for the fields the values will be assigned to. Static methods are also ignored.
class Foo {
public int myField;
public Foo(int myField) {
this.myField = myField;
}
public static Foo build(int myField) {
...
}
public void setMyField(int myField){
this.myField = myField;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy