Simple code is music to my ears

Have you ever opened up a file of source code and flinched at the complexity that comes screaming out at you from the screen? Well I’m imagining an IDE plugin that could do the screaming for you.

There are many measures of code that are objective and metrics driven, but there are others that are more subjective and taste based. I can’t tell you from a quick glance how many afferent couplings there are in a given piece of code, but I do have an almost immediate sense of how elegant the code is. In my mind elegance is all about solving complex problems with simple solutions. That’s the art rather than the science of computer programming.

When I read some code and get that gut feeling that the solution isn’t elegant, one of the first pointers is often how long each method or function is and how deeply indented they get. If you open a class and it just contains a handful of methods that are short (no more than 2 to 4 lines in each) and reasonably named, I breath a natural sigh of relief and know that the class is going to be pretty easy to understand. If on the other hand, there’s some crazy-long Dostoevskian beast of a method that I can’t fully see without scrolling up and down, then I start quaking like an intellectual weakling knowing that I’m going to endure much pain trying to grok what was in that coding genius’ mind when they wrote it. Then once I’ve worked out what they were trying to achieve I’ll have to go and figure out what they actually achieved … in long complicated methods the implementation often diverges from the intent!

Much of this complexity is immediately evident in how the code is nested and indented. Every indentation is the site of some kind of logical fork in the possible pathways through that method. Put in other terms there is a close correlation between the cyclomatic complexity (CCN) of a method and the amount of nesting and indentation. Or explaining this classic metric for complexity another way: each indentation is an invitation to extract a method into a beautiful, small, testable, readable and well-encapsulated unit.

When you’ve spent time living in a codebase where people vigorously accept these invitations to refactor that are offered up by our friend white-space then you tend to have a gut response of pain, horror and loathing when you see a heavily indented chunk of code.

The differences between these two styles of coding is clearly visible in the profile of the first non-whitespace characters on each line. If you look at the code and blur your eyes a bit this profile can look rather like the little LEDs on a graphic equalizer. The profile of indentation within a given source file composes a revealing signature that can be interpreted as a sound wave. Imagine hooking up a music player that could read these patterns. The heavily refactored codebase would purr gently at you with soothing sussurations, while those deeply nested and psychotomatically complex methods would scream at you like a shrill and brain-scrambling siren on a police car.

So step up please you gurus of mashing up Eclipse plugins with Garageband and create me a little plugin that will sing a song of source code whenever I open a file. But before you do that, maybe I should reach for the earmuffs.

3 Replies to “Simple code is music to my ears”

  1. We need good editors and IDEs to help figure out this legacy code (C and C are pretty bad, though I’m sure there’s some nasty Java out there_, but we also need to keep people from using those good editors or IDEs to create that legacy code.

    What if we create a new, hip, language that just happens to only allow ~20 methods in a class, ~20 lines of code per method, no more than 2 layers of nesting in a method, no more than 4 arguments to a method, etc…. and no Statics nor Singletons!

  2. >> then I start quaking like an intellectual weakling knowing that I’m going to endure much pain trying to grok what was in that coding genius’ mind when they wrote it

    I am coming to a similar conclusion that my preference for writing and working with simple, elegant code comes from my stupidity and lack of intellect. People aren’t writing difficult to understand code it’s just that they are geniuses who can easily grok highly complex classes several hundred lines long.

    Who needs things such as encapsulation, loose coupling and high cohesion when your brain can compute cyclic complexities of over 1000? Where’s the intellectual challenge of decoding a simple class when you’re a human Turing machine? OOP principles are for intellectual wimps.

Comments are closed.