I was doing some Java programming today, and I noticed an anti-pattern that was in an existing code base. It went something like the following:
public Class Klass {
...
public Klass(int foo, boolean flag, String s1, String s2, String s3, String s4) {
initialize(foo, flag, s1, s2, s3, s4);
}
public Klass(int foo, boolean flag, String s1, String s2, String s3) {
initialize(foo, flag, s1, s2, s3, null);
}
public Klass(int foo, boolean flag, String s1, String s2) {
initialize(foo, flag, s1, s2, null, null);
}
public Klass(int foo, boolean flag, String s1) {
initialize(foo, flag, s1, null, null, null);
}
public Klass(int foo, boolean flag) {
initialize(foo, flag, null, null, null, null);
}
private initialize(int foo, boolean flag, String s1, String s2, String s3, String s4) {
_foo = foo;
_flag = flag;
_s1 = s1;
_s2 = s2;
_s3 = s3;
_s4 = s4;
}
...
}
Immediately, my Don't Repeat Yourself sensor went off. It felt like the previous coder was essentially trying to use some default values for a method. You can do something like this in other languages, even other statically typed ones. In Ruby, it would be pretty simple: