24 July 2017

Shadows

We've all seen code where the variable names shadow something, that is the local variables have the same name as a class member or keyword. Python and Ruby both allow this, but you shouldn't do it.

Shadows create confusion for the reader. One recent example was a bit of code where several functions arguments shadowed the members of the class. It required the reader to slow down and think about which value was actually being used. Worse, in conversation it had to be discussed more than once which value was being used. Even the original author had trouble keeping track. Ultimately we renamed the function parameters and discovered we could eliminate most of the duplication and just use the class member.

In all, the shadows probably doubled the time it took us to complete the work just because of the confusion they caused.