Beauty Is Only Skin Deep
By Adrian Sutton
A couple of people commented on my beautful code entry saying that the IDE should take care of code formatting and indentation and that ‘beautiful code’ is mostly about design. I’d have to disagree, beauty is only skin deep. The design of code doesn’t make it beautiful, it makes it maintainable – it makes it work.
You’ll have to excuse the inherent sexism in the following analogy – it happens to be the best one I can think of.
Assume you’re a guy looking for a wife. You develop a set of requirements for your wife. She has to be able to cook, clean and sew for instance. You could go out and find an ugly wife who can cook, clean and sew and live happily with all your requirements met. However, more (stereotypically) likely, you’ll go out looking for a beautiful wife who can cook, clean and sew. Why? The appearance of your wife doesn’t affect how well your dinner is cooked, how clean your house is or how many holes there are in your socks. In fact, a beautiful wife comes with some drawbacks because she wastes hours each morning putting on makeup and cleansers and what-not. Why on earth then would you want a beautiful wife? The pay-off comes when someone else looks at your wife – they recognise how beautiful your wife is and assume there’s something fantastic about you that would make such a beautiful wife want you.
On the other hand, you’d be a fool to go out and get a beautiful wife if she couldn’t cook to save herself. The underlying design is important, it’s more important than beauty, but that doesn’t mean you should just ignore beauty all together.
Code beauty is more than just indenting too. The most important part of beautiful code is the comments. Every method should have a good comment on it and every class variable. It doesn’t matter how clean the design of the code is – most of the time you’re not looking at it, you’re off in some other method wanting to know what some method call does. With good comments you hover your mouse over the method call and the IDE helpfully tells you what the method does. Without good comments the IDE helpfully tells you the method signiture and then you have to interrupt your thought pattern and go and look at the code to this other method to find out about it then any methods it calls and finally you come back to where you were and try to remember what you were working on.
It’s okay to leave the formatting of code to your IDE (I use Jalopy a lot for code formatting) but that only gets you average looking code – not beautiful code. Beautiful code takes effort and that effort is rewarded by higher productivity, greater sense of pride in your work, greater respect from others who read the code and most importantly it develops a culture of writing great code which is both beautiful and functional.
Just like most guys are more inclined to hold the door open for a beautiful girl, people tend to take shortcuts if the code looks messy already but people take more care to do things right when the code is beautiful.
And for female readers who don’t appreciate the analogy, try replacing beautiful wife with hansome husband and cooking, cleaning and sewing with mowing, handiwork and car repairs.