Beautiful Code Is Important
Any half-decent coder will agree that writing readable code is important and that good comments are a part of that. More and more I’m getting the impression that that’s not enough.
Code should be a thing of beauty. Not necessarily "oh that’s a beautiful algorithm" and definitely not "oh wow that’s clever" but beautifully laid out with every blank line considered for meaning and well thought out comments everywhere. Every construct and every line should match the selected coding style and real thought should be put into why that particular coding style was selected.
All that sounds like a waste of time and productivity - you don’t need to go that far for the code to be maintainable and in fact I suspect that in the simple case writing beautiful code does cost productivity even in the long run. The reason I think you should do it anyway is the classic slippery slope argument. If you let the odd thing slip because they don’t really matter then you are more and more tempted to let something else slip. You don’t feel bad if that method isn’t commented because there are already a couple of other methods in that file without comments. You don’t care if that line you just wrote isn’t as clear as possible because the rest of the method is a little complex anyway.
More importantly though, you lose the ability for tools like Checkstyle to really benefit you. If you have thousands of checkstyle errors and warnings throughout the codebase it’s difficult to notice that the code you just added creates a new one. If the codebase is nice and clean though, being the person who creates the only checkstyle error isn’t much fun so you tend to avoid doing it. Classic broken window stuff - as soon as one window is broken the rest of the windows rapidly get broken.
The other big advantage of writing beautiful code is that it demonstrates good coding practices to anyone who reads that code and anyone who reads beautiful code immediately has a high level of respect for the programmer and for the codebase. They’re more likely to take time to understand problems and contribute fixes if they respect the codebase rather than seeing a mass of uncommented code, swearing at the programmers and going off to find a better written tool.
The best advice I can give then is to install the checkstyle plugin for whatever IDE you use (or something equivalent), tweak the configuration to suit your tastes (but don’t disable rules without carefully justifying why that rule provides no benefit) and then make sure that as you code you avoid even the smallest of warnings. Your code will thank you for it and those who read your code will be worshipping at your feet in no time.

November 28th, 2004 at 10:56 am
I let my IDE auto-format my code (on Eclipse, I type Alt-S, O).
I agree that “Code should be a thing of beauty”. For me, beauty is of a different kind: beautiful design e.g. judicious application of the GoF design patterns.
November 28th, 2004 at 8:20 pm
Thank’s a lot for arrising such an important issue. From what I’ve seen so far this issue (nice-looking code) is the first to be neglected in most projects … :(
November 29th, 2004 at 1:34 am
I’d rather replace the word beautiful by elegant. Furthermore, I’m addicted by a kind of intentional programming, so tiny methods body always dispatching to other small methods, even if no method reuse is achieved.
The code became so tidy that even my grandma can understand.
November 29th, 2004 at 2:52 am
I agree with Jonathan. To me ‘beautiful code’ is mostly about design.
Code format/identation is left for my IDE to sort out.
Comments are useful, but mostly to producing javadocs out of the code for API reference. When reading the code, the design should explain itself.
The only thing I worry about in code ’style’ is attempting to keep the lines within the 80 character width, and that’s because I don’t like to scroll horizontaly in my small 15″ laptop screen. That also forces me to break down huge lines into bite size ‘readable’ code.
Having said that, making text files look neat is something we should leave behind. I’m all for breaking away from the text file format and move towards richer binary formats, if that means we have more power to express our design.
November 29th, 2004 at 11:46 pm
I certainly find reading ‘beautiful’ code more easy than the norm - but I do think I waste too much time beautifying my own code. It’s almost an excuse to not start working on the next hard problem, as the current one isn’t ‘done’ until it’s pretty.
It should all be delegated to tools.
June 10th, 2007 at 6:14 am
i agree with code beuty.Becouse codding is a kind of creativity.And creativity requires beauty.