What is Beautiful Code?

First, I must preface this by saying I haven’t read the book Beautiful Code. However, reading the book description under the section Editorial Reviews.

“The authors think aloud as they work through their project’s architecture, the tradeoffs made in its construction, and when it was important to break rules. Beautiful Code is an opportunity for master coders to tell their story.”

In my opinion, this does not make for Beautiful Code. Maybe Pragmatic Code, Convenient Code, Judicious Code but not Beautiful Code.

Chapter 1 talks about “A Regular Expression Matcher by Brian Kernighan…and a problem can lead to a concise and elegant solution.” Great! While I have no doubt that the author and others in the book are FAR FAR more intelligent and accomplished that I am, I must say that what you might consider beautiful might not be what I consider beautiful. For example, regex expressions. I point you to Scott Hanselman’s post on LINQ to Everything – LINQ to XSD adds more LINQiness . Here is some sample code from his blog.

public sealed class AmountType {    
public static Microsoft.Xml.Schema.Linq.SimpleTypeValidator TypeDefinition =
new Microsoft.Xml.Schema.Linq.AtomicSimpleTypeValidator(XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String),
new Microsoft.Xml.Schema.Linq.RestrictionFacets(((Microsoft.Xml.Schema.Linq.RestrictionFlags)(46)), null, 0, 0, null, null, 32, null, null, 1,
new string[] { “^(([\+\-]?[0-9]*(([0-9][,\.]?)|([,\.][0-9]))[0-9]*))$”}, 0, XmlSchemaWhiteSpace.Collapse));    
private AmountType() {} }

This code may be very functional and to some very beautiful. However, to me Beautiful Code MUST be simple and readable, even by a Freshman Computer Science student. Look at the complexity and the change in thinking required to write Regex code. I have to get my thinking away from the programming language I am currently writing and context switch into the Regex language. Once I am done with the Regex I must switch back to the language and continue my work.

That brings me to another point. Why isn’t there a complete Regex library in the Microsoft Namespace. Things like validation for phone numbers based on region, validation for addresses, validation for email based on the RFC standard and other useful Regex . I understand that a lot of the regex created is custom but some of the most used regex is going to be the simple reusable stuff.

I came across this little gem from a blog called Alarming Development.

“O’Reilly just published Beautiful Code. I was invited to contribute, but I just could not go along with the premise. I disagree that beauty is a guiding principle of programming. Here is how I responded.

I am having trouble with this assignment. Telling an inspiring story about a beautiful design feels disingenuous. Yes, we all strive for beautiful code. But that is not what a talented young programmer needs to hear. I wish someone had instead warned me that programming is a desperate losing battle against the unconquerable complexity of code, and the treachery of requirements.”

Bravo. As always, I always want to “Ask Jeff” too. Jeff Atwood of Coding Horror had this to say:

“Unfortunately, Beautiful Code wasn’t nearly as enjoyable of a read as I had hoped it would be. It is by no means a bad book, but there’s something about it that’s not quite right.

Part of the problem is that it’s a compilation of disconnected essays, much like The Best Software Writing I.

I absolutely agree with the Jeff. (Yes I said the Jeff. Maybe it should say “The Jeff”) He goes on to say:

“But the code itself is not beautiful. The beauty of code lies in the architecture, the ideas, the grander algorithms and strategies that code represents.”

I agree with the first sentence However, I don’t think software architecture is beautiful. OH MY GOD! BLASPHEMY!! Yes I said it. Architecture is not beautiful. Think about it. If a problem is presented, each person tackling it will probably create different architecture for the same problem. Heck, the same person might do it differently each time based on things they learn from the previous attempt.

In my opinion the beauty lies in…….the problem! There is one unique problem and we have many way to solve it. The satisfaction gained from appreciating the problem, studying it and learning about it is beautiful. So the next time you come across the opportunity to solve a problem, savor the taste. I know I will.


2 thoughts on “What is Beautiful Code?

  1. tigerauge says:

    Sounds quite interesting so far – maybe i have the chance to read it

  2. aboutdev says:

    Thanks for your comment tigerauge. I hope you have a chance to read it too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: