rspec/rules/S3218/rule.adoc

63 lines
1.1 KiB
Plaintext
Raw Normal View History

It's possible to name the members of an inner class the same as the ``++static++`` 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.
2020-06-30 12:48:39 +02:00
== 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
2020-06-30 12:48:39 +02:00
----
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://wiki.sei.cmu.edu/confluence/x/5DVGBQ[CERT, DCL51-J.] - Do not shadow or obscure identifiers in subscopes
2020-06-30 12:48:39 +02:00