Code Isn't Beautiful

Beauty is in details. Here is one example below.

A gentleman was once visiting a temple under construction.
In the temple premises, he saw a sculptor making an idol of God.

Suddenly he saw, just a few meters away, another identical idol was lying.

Surprised he asked the sculptor, do you need two statutes of the same idol.

No said the sculptor. We need only one, but the first one got damaged at the last stage.

The gentleman examined the statue. No apparent damage was visible.

Where the damage is? Asked the gentleman.

There is a scratch on the nose of the idol.

Where are you going to keep the idol? Asked the gentleman.

The sculptor replied that it will be installed on a pillar 20 feet high.

The gentleman asked- When the idol will be 20 feet away from the eyes of the beholder, who is going to know that there is scratch on the nose anyways?

The sculptor looked at the gentleman, smiled and said, "The God knows it and I know it ".

The desire to excel should be exclusive of the fact whether someone appreciates it or not.

Excellence is a drive from Inside not Outside.

I too wasn’t particularly impressed with the book in terms of the expectations raised. Some interesting stuff but not a lot of beauty. If you want to see really beautiful code, pick up anything by Donald Knuth.

@Student: The best cure for cognitive dissonance between brackets and end statements, or any two competing syntactic styles, is to learn a language that uses the style you’re not used to. But it’s got to be a language that you enjoy.

Lua was the one that taught me to code switch between “}” and “end,” personally.

to: ThatGuyInTheBack

“and frankly I’m no longer arrogant enough to think I can keep 4 or 5 levels of curly braces straight.”

No matter what the language or indentation, I use a comment after the tail.
} // while whatever
END IF ’ daytime in Norway

What is wrong with VB syntax?
Why should I need all c’s punctuation? ;
Why should I need a ; before } but not after?
Why should I need ( ) around subroutine parameters?

I like that my VB functions and subroutines look like native functions and statements. The only situation where that is not true is where the syntax of my own output formatting subroutine can’t use and ; rather than , between parameters to indicate no spacing.

I will admit that VB has a readability ambiguity design flaw:

Is A a statement label target of a GOTO or GOSUB? or
Is A a call to subroutine A that takes no parameters?
You can’t tell from reading the line.

David

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.

Let’s see…

  • “End Function” is actually less ambiguous than a curly brace. So are “End For”, “End Select” etc…
  • “CType” is just a cast which is present in the ‘curly-brace’ languages. It’s better than C and C# casts because at least “CType”, like C++'s cast templates, are searchable.
  • “Me” is the same thing as “this” in the ‘curly-brace’ languages.
  • An application that is written using VB.NET and then translated to C# (or vice-versa if you want) looks remarkably similar in both VB.NET and C#. The main differences being the keywords used.

In the end, Visual Basic’s keywords and structure flow are in some ways better than the ‘curly-brace’ languages and it looks like you just have a prejudice against Visual Basic. I could call you a ‘language racist’.

I understand why so many people don’t like Visual Basic:

  • It’s different. But that’s no reason to say a language is really “bad”. These languages are different but do not have the criticism VB has: Python, Lisp, F#, Nemerle, assembly.

  • Visual Basic (especially prior to .NET) was breeding ground for coders who can’t code well and mistakingly thought themselves “software engineers” (these people used no engineering practices at all). While these people’s code is ugly and miserable to maintain, this does not mean that VB can’t be used elegantly. Far from it. I’ve seen elegant VB code – both pre- and post-.NET. It’s just that VB has been easier for people to use (not necessarily a bad thing by the way) – but the consequence of that is you have a higher proportion of less skilled people using the language.

As for me… I work daily using C and C++, but I also work on occasion with Python, assembly, F#, C++/CLI, C#, and VB.NET. C++ is my favorite language because of the multiple paradigms it supports.

Jeff,
I don’t think the comparison to Out of Their Minds is very apt (I haven’t read Programmers at Work, so I won’t comment on that one). Out of Their Minds is much more about the biography and personalities of great computer scientists and the most high level description of their work possible. It was written for both laypersons and engineers to read to get a sense of the history of the discipline in terms of its prominent people. It is not very focused on the code. But, to say that focusing on the code is an error is a hard position to defend. If you do not see beauty in code, it’s difficult to create beauty in code.

So do you just run off and buy a book simply based on an Amazon recommendation? I am surprised you have a lot of faith in their recommendations. Why don’t you first check out the book at a nearby bookstore? If you like it, buy it right there or if you want to save some money, order it from Amazon.

Beauty is in the eye of the beholder.

I have to say I disagree with the idea that only a subject (algorithm, idea) can be beautiful and never the representation. Form and content both have value.

http://basildoncoder.com/blog/2008/02/22/code-can-be-beautiful/

Jeff I also purchased this book in the hopes of learning more about how various software systems work. The title of ‘Beautiful Code’ wasn’t the selling factor for me so I guess thats why I wasn’t quite as annoyed with this book as you were. After a while of reading through the book it really does take on a snobby champagne sipping tone that really will annoy lots of folk though. You can just imagine the authors popping cristal and writing about why they believe their code is so elegant. LOL

A big thing with me is that I find certain modern works beautiful. I find early Sonic Youth beautiful. I find John Coltrane and Ornette Coleman’s music to be beautiful. I find Jackson Pollack beautiful.

But I would totally freak out if I saw a code base that looked like “Free Jazz” sounds.

My opinion, dudes:
First off, code is not beautiful. Supermodels are beautiful (at least on the outside!) Beethoven’s Ninth Symphony is beautiful. The sunrise by the shore is beautiful. Code can be well-designed, or uniquely structured but there is nothing beautiful about code. Good code should be appreciated and emulated but never cherished or adored. Strive to write good code, even great code, but don’t strive to make beautiful code. I believe doing so is a mistake.

I understand the point all the essays. I just don’t like the use of the word beautiful being related to coding. Of course, it’s Friday, and maybe a few hours/vodka tonics from now It won’t bother me so much!

Before .NET, it used to be “use C++ to make quick applications, use VB to make applications quickly”

Now, with .NET, VB, C#, and managed C++ all compile to the same thing. There are still a number of things that VB won’t let you do (since it is still designed to eliminate the need to code all of the underlying functionality), but now there is C# as a middle-ground. With C#, you get the curly-brace formatting C++ developers are accustomed to, but the rapid development of VB.

The majority of business apps (not counting critical services like database servers) can be written quickly and efficiently using nothing but VB.

Not really. It’s the context that the code appears.

You expect to find the majority consisting of information about the code.

So the book title “Beautiful Code” doesn’t imply that you’re going to read a lot of beautiful code examples? I think you may have missed the everyday zen mindfulness of the book; if Joshu can find all the world’s beauty in a bucket of dead fish, you can pretty easily find beauty in code that someone has gone out of their way to make you consider. Perhaps the beauty comes from interpreting and unraveling the code yourself (much like a koan) and not form some lengthy explanation of it’s beauty. The process of understanding its elegance is also fundamental to taking away the lesson that the code was meant to teach.

Like they always say, a joke explained is never funny. To me, explanations in some chapters would have ruined everything… it would be like opening a Penthouse to find photos of nude women accompanied by pages of text describing why their poses and the soft lenses make them sexually appealing.

And, at no point, does the book, or the post about it, imply a study of the beauty of C++/Assembly.

If the book you are talking about is Abrash’s, it does indeed imply a study of the beauty of C++ and the raw functionality of ASM. When the book was published, everyone was still trying to beat the industry drums over C++ and its suitability for large projects. Borland and others were leaning heavily on game developers to lead the way because they were willing early adopters and could demonstrate the speed and utility of C++. The book mentions at several points how beautiful and elegant C++ can be and how you should consider using it (with linked or inline assembly, of course!) for graphics or generally optimized programming.

Dmitry Dvoinikov put forward his opinion of the book and Jeff echoed it here. I don’t agree with either and can’t imagine anyone having any rational objections to the book, even if they would have laid out the content differently. I imagine you are meant to read this book the way an architect walks into a building–it’s not a “for dummies” book so why should it have wider appeal and be more accessible?

Did O’Reilly run out of animals to put on the cover?
In all likelihood the authors did not pick the title, the editor or publisher did; and with more the idea of what would sell than relating it to the content. How many have picked it up based on the title, and how many would if the title was ‘Essays on Programming’?

You dont know a language, you dont understand it - of course.
You dont know maths, you can not see beauty on theorems.
Reenforcemen :beauty in theorems does not come from writing it.It is the idea behind the theorem that is beautiful.

I agree the inner beauty of code is most important, but to me, there is also how code looks like: the typography, how are white spaces formed, the form of comments… There is excellent article on typography of code: http://blog.hamstu.com/2008/02/03/the-typography-of-code/

Yeah I will have to agree. Code is eloquent, maybe clever, but not beautiful. SOLUTIONS are beautiful, from “there is no largest prime number” to BitTorrent. When someone can solve a problem in a way that makes you feel dumb for not seeing it immediately, that’s a beautiful thing. But indentation? Trivial verbosity? “Should that curly brace be on the same or next line” ? lol… The only person who does those things beautifully is someone who does them exactly like you!

Then again, I program in COBOL all day, C# all night, and PHP or VBA whenever web sites or excel macros require it :slight_smile:

Hmm… I almost bought this book too…

Another great post Jeff.

One thing to note, that I think you imply or maybe I need sleep, is that if the code functions correctly and runs in optimum time, no matter how clean it is- people of various skill levels may find the code to be ugly for reasons of their own -bracket on a new line- that they’re so used to. Hogwash. Respect another’s creation as you wish respect onto yours… unless if the Windows of your product line and has infinite bugs in it.

Peace

But the code itself is not beautiful.

I disagree. Code CAN be beautiful, and I think that is exactly the point of this book - to show how different languages’ features can be utilized to create the most concise solution to a problem.

I think you can appreciate code in any programming language, even if you are unfamiliar with that language. A spoken language parallel would be something on the lines of appreciating music with lyrics in a foreign language or appreciating poetry written in another dialect - but surely not as foreign as Russian poetry.

Here is a rough translation in two versions:

{
I’m a poet, named Dontknowka,
And I’m giving you a polka.
}

  • Dontknowka must be pronounced as “don’t know”; and “ka” sounds like “cut” without the ‘t’.

And here is another localization, that does not take into account the Russian origin of the input data:
{
I’m a poet, named Jar-Jar,
And I give you a guitar.
}

I prefer the latter.