rspec/rules/S3218/rule.adoc

62 lines
1.1 KiB
Plaintext
Raw Normal View History

2020-06-30 12:48:39 +02:00
It's possible to name the members of an inner class the same as the <code>static</code> members of its enclosing class - possible, but a bad idea. That's because maintainers may be confused about which members are being used where. Instead the inner class' members should be renamed and all the references updated.
== Noncompliant Code Example
----
class Outer
{
public static int A;
public class Inner
{
public int A; //Noncompliant
public int MyProp
{
get { return A; } // Returns inner A. Was that intended?
}
}
}
----
After a rename
----
class Outer
{
public static int A;
public class Inner
{
public int B;
public int MyProp
{
get { return A; } // Still compiles and runs but functionality has changed
}
}
}
----
== Compliant Solution
----
class Outer
{
public static int A;
public class Inner
{
public int InnerA;
public int MyProp
{
get { return InnerA; }
}
}
}
----
== See
* https://www.securecoding.cert.org/confluence/x/2ADEAw[CERT, DCL51-J.] - Do not shadow or obscure identifiers in subscopes