Reuse of identifier names in subscopes leads to obscuration or shadowing. The identifiers in the current scope render those defined elsewhere inaccessible. While Although the Java Language Specification clearly resolves any syntactic ambiguity arising from obscuring or shadowing, such ambiguity burdens code maintainers, especially when code requires access to both the original named entity and the inaccessible one. The problem is aggravated exacerbated when the reused name is defined in a different package.
Wiki Markup |
---|
According to [§6.3.2, "Obscured Declarations,"|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.2] of the _Java Language Specification_ \[[JLS 2005|AA. References#JLS 05]\], |
A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of 6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package.
...
No identifier should obscure or shadow another identifier in a containing scope. For instance, a local variable should not reuse the name of a class field or method , or the class name or package name. Similarly, an inner class name should not reuse the name of an outer class or package.
Both overriding and shadowing differ from hiding, in which an accessible member (typically non-privatenonprivate) that should have been inherited by a subclass is replaced by a locally declared subclass member that assumes the same name but has a different, incompatible method signature.
...
This noncompliant code example reuses the name of the val
instance field in the scope of an instance method. This The resulting behavior can be classified as shadowing.
...
This compliant solution eliminates shadowing by changing the name of the variable defined in the method scope.
Code Block | ||
---|---|---|
| ||
class MyVector { private int val = 1; private void doLogic() { int newValue; //... } } |
...
This example is noncompliant because the variable i
defined in the scope of the second for
loop block , shadows the definition of i
, which is defined in the scope of the doLogic()
method.
...
Name reuse makes code more difficult to read and maintain. This , which can result in security weaknesses.
...
C Secure Coding Standard: "DCL01-C. Do not reuse variable names in subscopes"
C++ Secure Coding Standard: "DCL01-CPP. Do not reuse variable names in subscopes"
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="52fe6baabab99c07-c47ff57c-45724a23-a4258893-dcd8dbfd4ac2586963281ac4"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. References#JLS 05]] | [§6.3.2, "Obscured Declarations" | http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.2] | ]]></ac:plain-text-body></ac:structured-macro> |
| |||||
| |||||
| |||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="88fc5b305787a265-fc7c0acd-47ac4571-853f9b8a-56910a4f17d07f7fa75810ab"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. References#Bloch 05]] | Puzzle 67: All Strung Out | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="53958358bab0019a-02568157-4dff4f47-9f4dad96-1ee7d62112090f0fcafd75c1"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. References#Bloch 08]] | Item 16: Prefer interfaces to abstract classes | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7e4acf015843b9ec-bb3b7cb4-47f247ea-9d84a534-39926da92d25df0257f49bae"><ac:plain-text-body><![CDATA[ | [[Kabanov 2009 | AA. References#Kabanov 09]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ef4aabadba48bd53-a59568d3-4a594b3d-afd19767-276a58a7a76b06a0851ac6cc"><ac:plain-text-body><![CDATA[ | [[Conventions 2009 | AA. References#Conventions 09]] | 6.3 Placement | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c2453eab4e395027-5a33c60e-4afe4056-8f73972b-4eeab868b43723a4f3e7ff31"><ac:plain-text-body><![CDATA[ | [[FindBugs 2008 | AA. References#FindBugs 08]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...