# Adding a Release test to check for shadowed vars defined at the level of a 
class

Imagine we make a class like this:

Object subclass: #MyClass
        instanceVariableNames: 'Array i'
        classVariableNames: 'Object'
        package: 'MyPackage'
        

We can ask the class for all the variables it defines with #definedVariables. 
Thus is is easy to filter
for the shadowing variables:

```Smalltalk
MyClass definedVariables select: [ :variable | variable isShadowing  ] 

"{#Array => InstanceVariableSlot. #Object->nil}"
```

With this, we now can easily scan the whole system for problematic classes:

```Smalltalk
Smalltalk globals allBehaviors select: [ :class | 
                class definedVariables anySatisfy: [:var | var isShadowing]].
```

And interestingly, this returns results! Of course, our class MyClass as 
defined above, but in addition we do have 6 classes
where class variables shadow globals. We should fix them at some point.

For now, we can add a release test that ensures that we do not add more cases:

```Smalltalk


testClassesShadow
        |  classes validExceptions remaining |

        classes := Smalltalk globals allBehaviors select: [ :class | 
                class definedVariables anySatisfy: [:var | var isShadowing]].

        validExceptions := #(). 
        
        remaining := classes asOrderedCollection reject: [ :each  | 
validExceptions includes: each name].
        "6 left that we need to fix"
        self assert: remaining size <= 6.
```

What we need next is a Code Critique rule. This for once warns developers 
early, but in addition it will
make it much easier to fix the 6 problem cases seen above.

Reply via email to