The “constructor that doesn’t construct” blooper of the day

DeadFish2

Two people sent me this code blooper the other day, again, class and parameter names have been changed to protect the identity of the offender(s).

public SomeObject(
boolean lowSetting,
boolean highSetting,
boolean currentSetting,
Color lowColor,
Color highColor,
Color currentColor,
Color otherColor)
{
   super();
   lowSetting     = this.lowSetting;
   highSetting    = this.highSetting;
   currentSetting= this.currentSetting;
   lowColor        = this.lowColor;
   highColor       = this.highColor;
   currentColor   = this.currentColor;
   otherColor      = this.otherColor;
}

I have to admit I didn’t get it in the first couple of second, but it should be clear that this constructor is basically not doing anything. It basically overrides the constructor parameters with the current values in the object, which is more than likely not what we want to do.

BTW it also has a pet peeve of mine, it calls “super()” (the parent’s public no arg constructor). A lot of people don’t know that this is done automatically for you, no need to write code to for it.

Anyways, this one could have been prevented if you had a policy saying that method parameters should be named differently from class members. However, I personally like naming them the same. I guess it shouldn’t be unreasonable for the compiler to give you a warning when you do this, I’m going to guess most of the time you don’t want to do this.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Comments »

 
 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

WP SlimStat