Programmers like writing dumb code

It is quite fun when you see an extremely verbose code that can be replaced by two or three lines, just removing useless stuff, or using the correct API. But it is much less fun, when an expert makes a huge refactoring on your codebase covering it of useless crap. It happened to me today (I should say yesterday I just passed the midnight).
On the good side of the coin, I have material to post in my horror code gallery for the next 3 or 4 days.
This is the first masterpiece.

Here is conceptually1 how the code was, before the expert's action:

 1public class DoSomethingTask() {
 2    // private fields
 3
 4    public DoSomethingTask(Dependency1 dependency1) {
 5        this.dependency1 = dependency1;
 6        this.dependency2 = new Dependency2(dependency1);
 7        this.dependency3 = new Dependency3();
 8        // ... and so on...
 9    }
10}

Easy uh? How the expert improved that code?
Simple: he removed constructor arguments, and substituted with setters and getters, then he implemented the "lazy instantiation" (wow! a pattern!).

Here is what he did:

 1public class DoSomethingTask() {
 2    // private fields
 3
 4    public DoSomethingTask() {
 5        // nothing here
 6    }
 7
 8    public void setDependency1(Dependency1 dependency1) {
 9        this.dependency1 = dependency1;
10    }
11
12    public Dependency1 getDependency1() {
13        if (dependency1 == null) 
14            dependency1 = Dependency1.getInstance();
15        return dependency1;
16    }
17    
18    public void setDependency2(Dependency2 dependency2) {
19        this.dependency2 = dependency2;
20    }
21    
22    public Dependency2 getDependency2() {
23        if (dependency2 == null)
24            dependency2 = new Dependency2(getDependency1());
25        return dependency2;
26    }
27
28    public Dependency3 getDependency3() {
29        if (dependency3 == null)
30            dependency3 = new Dependency3();
31        return dependency3;
32    }
33
34    public void setDependency3(Dependency3 dependency3) {
35        this.dependency3 = dependency3;
36    }
37
38    // autopilot-mode=on ... same stuff for every single field I was having in the class, and for every class 
39}

Once a colleague asked why he was always providing accessors for all the arguments declared on the constructor. He replied something like: "because when you declare something to the constructor, you are telling the world that the object has those public properties, then you have to provide getter and setters". Have you ever heard a bigger one? Encapsulation and information hiding... these unknown...
There is people around liking to write complete verbose, useless, dumb and harmful code. Hope for them that they are paid on number of lines of code they write.

Yes, it has been an hard day.

NOTE
1 As I am a contractor, I cannot disclose real code, so I replaced the actual code with conceptually equivalent stuff. BTW, if anybody wants to grab horrific techniques from my horror code gallery, please do at his own risk.


No Responses to “Programmers like writing dumb code”  

  1. No 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=""> <s> <strike> <strong>



Calendar

October 2008
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Follow me

twitter flickr LinkedIn feed

Subscribe by email

Enter your email address:

Archives


Categories

Tag Cloud


Listening