December 24, 2007

Code Bloat and Design Patterns

Steve Yegge recently wrote an article about the horrors of code bloat in Java and Jeff Atwood at Coding Horror provides a summary with remarks. Although I am not much of a Java coder, Steve's assessment that both the language itself and the mindset of the developers using it tends to lead to bloated code is something that my brief exposures to Java have confirmed for me. However, Steve moves beyond Java to more general programming architectures, such as design patterns and static typing that I'm not sure is completely fair. His primary complaint seems to be that in Java, implementation of design patterns tends to lead to cut and paste boilerplate in Java because it has no generics/templates, but he also seems to reject design patterns in general as "boxes" for arranging code that inherently make the same functional code implementation take up more space, helping to contribute to his 500K lines of bloated code.

While I agree that with code, less is often more, I think that Steve is also suffering from a lack of perspective here. First, he notes that his Java game is composed of 500K lines of his own code. I believe his first mistake is to forget that his code, in using Java libraries, is probably really built of a significantly larger code base of tens, if not hundreds of thousands of additional lines of code in the languages libraries that he no doubt uses. The same is try of almost any modern language. Even short C programs often rely on hundreds, if not thousands of lines of code composing core libraries, such as stdlib, stdio, string, etc. and also various operating system, filesystem, device drivers, etc. In other words, almost any modern program relies of a vast base of code that we view in abstract terms, but would never try to comprehend thoroughly because it is both impossible and unnecessary. However, we often conveniently ignore this fact and talk about only our own code.

Given this perspective, I disagree with some of Steve's critique which seems to reject static typing (primarily, it would seem, because of Java's lack of good features for avoiding duplicating code) and design patterns as inherently bloating. In fact, my understanding of design patterns is that by design, they provide more consist and abstract ways of interfacing with blocks of code to provide an abstract usage of the code that does not require so much specific knowledge of the internals, much like the above mentioned core libraries, os implementations, drivers, etc. While supporting that lower coupling often requires a bit more code to make the behavior less specific and more self-contained, at least theoretically, this should decrease the amount entagled code at a higher level.

Thus, while Steve makes some good points about Java in particular, I wouldn't be so quick to reject static languages and design patterns.

Bibliography
1. Steve Yegge. "Code's Worst Enemy". Stevey's Blog Rants. December 19, 2007.
2. Jeff Atwood. "Size is the Enemy". Coding Horror. December 23, 2007.

1 comment:

Anonymous said...

Thanks for writing this.