Code Isn't Beautiful

I was thrilled to see the book Beautiful Code: Leading Programmers Explain How They Think show up in my Amazon recommendations. It seems like exactly the type of book I would enjoy. So of course I bought a copy.

This is a companion discussion topic for the original blog entry at:

But the code itself is not beautiful.

Actually, it is. To those who speak it. That was Dmitry’s point. And therefore, a book presenting beautiful communication to people who don’t speak the language has to be different than an ordinary book of poetry which would be read by fluent users of its language.

In the first chapter Kernighan concludes that the example “really uses the underlying language to good effect” and goes on to explain why. The .NET framework is essential for Petzolds’ chapter; pseudo code would not make sense.

My problem with the book is that I don’t have a C.S. degree, so much of it just feels impenetrable.

I would hold up “Programmers at Work” and “Out of their Minds” as examples of what Beautiful Code was trying to achieve.

A commonly heard (and silly) saying is “beauty is skin deep, but ugly goes to the bone”. It’s funny in part because we all know that it is often the opposite of reality. Pretty skin can hide an ugly algorith, but no amount of beauty at the algorithmic level can hide the ugliness that is some languages.

@Jess: and physically ugly people may be pretty nice to talk to as well. So that saying is indeed silly, since the opposite is just as common.

Humm, I quite enjoyed the book (even though I can’t read, grok, love or tolerate lisp ;)). And while there was a lot of code in there, the authors also wrote about their thoughts, designs, et cetera - maybe it’s just me, but I think most chapters presented the reason why the author thought the thing was beautiful, and for their various reasons.

It was also nice seeing a lot of different languages and their associated programming styles, from the condensed and impenetrable to the almost self-documenting.

But I guess if your definition of “beautiful” is too narrow, you could end up disappointed :slight_smile:

The difference is that “The Best Software Writing” carefully selected from years of the best writing whereas beautiful code just asked N people for a free bit of text the correct length.

A beautiful algorithm, a beautiful implementation, and beautiful code can all be very different things.

You get complaints when you post VB code because VB is ugly. The syntax, a twisted corruption of what was once a very simple attempt to make interpreted pseudocode attacks us on an almost visceral level, the few remaining English keywords hurtling us into that uncanny valley where freaks and monsters lament their cruel fate. :wink:

But of course, you can take a beautiful algorithm and implement it beautifully in VB. It won’t be pleasant to look at, but once you get past the scars and blemishes on the skin you can still find beauty in the meaning.

You just posted about Michael Abrash’s books. Most of us today read assembly only with difficulty if at all, yet those books were entertaining because of the great stories surrounding the code. IMHO, that’s the way to write about code - give it a narrative, a plot, a protagonist a villain and a great struggle which at length is overcome. But of course, the code itself doesn’t have to be beautiful, and indeed may be far more entertaining if it isn’t - the success of The Daily WTF should prove that.

The idea of beautiful code, like beautiful poetry, is deeply attached to the language in which it is written. There’s no point in translating “The Wasteland” into Latin, and any modern English translation of “Jabberwocky” is just boring - the beauty comes from how they use the language in which they’re written, not necessarily any specific problem solved… or even the ideas conveyed. Beautiful code, like beautiful prose, can open our eyes to how we may use a language.

There are plenty of gripping stories. And plenty of clever algorithms. Truly beautiful code is rare.

Well they do say that beauty is in the eye of the beholder. So as Josh said if you understand the language you might also see the beauty, but someone like myself who is a web developer, I only know PHP, AJAX, and a handful of other seemingly useless languages (HTML etc), any lisp or even any C(++, #) over the vary basics would not make any sense to me so I don’t think I’d see it as very beautiful. In addition what I see as beautiful, like a PHP script to do entire file system handling, would not be seen as beautiful to someone who knows C(++, #) or even shell scripting.

There is no “elegant” code, either. “Elegant” is an adjective reserved solely for artists, poets and fashion. Code works - well - or it’s “gorilla” - and crap.

I think you could describe some of the Ruby language as either beautiful or elegant. That is probably why the chapter by Yukihiro Matsumoto stood out.

The following code, for example, I would say is very elegant:

(3…6).each { |num| puts num }

= 3

= 4

= 5

= 6

I think beautiful code exists. However, not only do you have to understand the language, but the code itself always is built with an abstraction that is RARELY beautiful underneath the hood.

At that point, you have something akin to pseudocode anyways, that represents the bigger picture in a compact fashion.

The book has some good stuffs, but I never could enjoy it much ve read like code complete etc…

As you said either they’ve to stick with single programming language and explain things in that way or to stick with pseudo code.

But still having some good nice stuffs inside it. I was almost stopped reading it. Anyway have to restart it again with the interesting part first :slight_smile: (Obviously goes to C/C++ items)

One part of the beauty is when your brain likes what it gets served.

I feel that ruby comes very close to this mind, but i guess other languages have a similar way (though different syntax).

What matters most at the end of the day of course is the good IDEAS that are put forward and realized. But until then I can dream of a better (software) world, where bugs fix itself (or even better dont even start to exist)

characterized by forceful and appropriate expression

I’ve never written elegant code… but eloquent code, ah yes!

End Function, CType, Me. That is ugly. There is such a thing as beautiful code it is anything with a curly bracket or a def.

What’s the most beautiful code? DNA, I guess, even without understanding what it does.

It’s like you don’t even read your own posts, Jeff. Your last one linked to an entire book about optimizing C++ and assembly at the instruction level and this one complains about how much it sucks when random people dump a bunch of code on you and ask you to see the beauty.

Abrash’s book is full of complex, subtle, and, yes, even beautiful code. That’s why you mentioned it and linked to it. Why did you like his books so much and hate these other authors who are proud of their particularly elegant solutions and similarly display their own code for all to learn from?

If you’re going to drive all of your readers to the beach to show us how neat all the water is, don’t bitch about a specific lake the next day. Not only is your obvious “me too” criticism a waste of your time, it makes you sound wishy-washy.

@Sean you obviously missed the point. It’s not the code that’s not beautiful, it’s the lack of context with which to understand why it’s beautiful. That’s why his last post is not incongruous with this post.

In any case, great post Jeff. How about a new book, “Beautiful Blog”. :wink: