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.
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Sep | Nov » | |||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | ||
Archives
Categories
- Android (3)
- Apple (26)
- Books (7)
- Eclipse (14)
- Errors (3)
- Firefox (7)
- Git (2)
- Hardware (16)
- Horror Code (8)
- Internet (18)
- Java (98)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (25)
- Linux (50)
- Opinions (25)
- OSX (4)
- Python (1)
- Software (27)
- Speeches and Conferences (8)
- Unix (3)
- Web (21)
- Windows (19)
Tag Cloud
Android apple architecture Bash colors configuration CSS Development Düsseldorf Eclipse germany Git Google Hardware hdr How-To Java JAXB job junit Karmic Linux MacBook music night Open Source Opinion oracle OSX patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu video Web XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links




















No Responses to “Programmers like writing dumb code”
Please Wait
Leave a Reply