Reuse of identifier names in subscopes leads to obscuration or shadowing. That is, the The identifiers in the current scope render those defined elsewhere inaccessible. While the JLS 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 when the reused name is defined in a different package.
Wiki Markup |
---|
According to the Java Language Specification \[[JLS 2005|AA. Bibliography#JLS 05]\], [Section 6.[§6.3.2|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.2], "Obscured Declarations" of the _Java Language Specification_ \[[JLS 2005|AA. Bibliography#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§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.
...
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.
...
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
Wiki Markup |
---|
\[[JLS 2005|AA. Bibliography#JLS 05]\] [Section 6§6.3.2|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.2] "Obscured Declarations", [Section 6 [§6.3.1|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.1] "Shadowing Declarations", [Section 7§7.5.2|http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5.2] "Type-Import-On_Demand Declaration", [Section 14§14.4.3|http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.4.3] "Shadowing of Names by Local Variables" |
Wiki Markup |
---|
\[[Bloch 2005|AA. Bibliography#Bloch 05]\] Puzzle 67: All Strung Out \[[Bloch 2008|AA. Bibliography#Bloch 08]\] Item 16: Prefer interfaces to abstract classes \[[Kabanov 2009|AA. Bibliography#Kabanov 09]\] \[[Conventions 2009|AA. Bibliography#Conventions 09]\] 6.3 Placement \[[FindBugs 2008|AA. Bibliography#FindBugs 08]\] |
...