Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When speaking with a non-programmer friend, I happened to mention the concept of "beautiful code" and she wanted to understand what that meant, but I was at a bit of a loss as to how to explain it to someone who would have no context whatsoever.

When all code looks like gibberish to someone anyways, how can you explain what makes one piece of code prettier than another? Analogies could be useful too.

share

locked by World Engineer Jun 18 '13 at 0:09

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

closed as not constructive by Giorgio, MichaelT, Dynamic, Martijn Pieters, World Engineer Jun 18 '13 at 0:09

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

12  
Can you explain beautiful code to a programmer without showing the code? The description is itself an art. –  kojiro Apr 5 '11 at 11:50
9  
If we can come up with a good idea here, maybe I can use it to describe a beautiful proof to a non-mathematician. –  David Thornley Apr 5 '11 at 14:00

16 Answers 16

up vote 88 down vote accepted

Language analogy

Think of your most favorite story. It is probably beautifully written. To a non-English speaker, just because they can't understand it or comprehend why it is beautiful doesn't detract from its beauty.

Construction analogy

Consider a shoddily built house. It has doors and windows that are obviously bricked over. The mortar is cracked in one corner where the foundation was insufficient. And no one can understand why there is a stairwell leading up to a 2nd floor that doesn't exist. The toilet is built next to the kitchen with no exhaust fan and situated such that the prevailing winds will blow the nasty bog odors through the rest of the house. The chimney from the open fireplace is 5cm away from some wood, ready to catch fire.

Contrast that with a house where everything is where it should be, it is low maintenance, perfectly insulated so that a bit of solar heat will keep it warm in winter, energy is recovered from the ventilation, so you have both fresh air, comfortable temperature and negligible power bills. It is built of non-flammable materials so that it can't burn down from a fire. It will withstand a 9.0 earthquake and an F5 tornado. It only costs 30% more to build than the other house, but will pay the difference off in 5 years. And it is aesthetically pleasing.

The GUI is like the outward appearance of a house. A user can recognize beauty in a GUI, just like they can appreciate a beautiful house. But perhaps even more importantly, the real beauty in the design in both houses and programs are the things that are invisible to the uninitiated, but important or nice to have regardless.

share
19  
That construction analogy is perfect! –  Daniel Vandersluis Apr 5 '11 at 2:10
1  
+1 @user21007: Long, long time ago I was an information architect for huge sites - everyone gets the construction analogy, and concept of systems that just work and feel right. –  blunders Apr 5 '11 at 3:27
1  
Good analogy for products with GUIs. Not so good for frameworks and libraries. –  Den Nov 22 '11 at 12:20
1  
Kind of reminds me of this advert: youtube.com/watch?v=p9tjs-6wbsI –  JohnL Dec 8 '11 at 18:29
4  
I've used the language analogy a lot in the past: "I'm working with this ugly code..." "Wait, what do you mean by ugly?" "u see, its write liek that" –  Niphra Feb 27 '13 at 16:31

Jackson Pollock paintings are an example of something beautiful to some and gibberish to others.

share

All of the code present, is required, and none of it requires explanation.

share
2  
@zdan: I'd counter by saying that those who find it beautiful, have the requisite knowledge (hence, in the eyes of the beholder). Besides, clever != beautiful IMO. –  Steve Evers Apr 5 '11 at 1:33
2  
@zdan: When it requires a lot of explanation like that, it's not "beautiful code," but "a clever hack." –  Mason Wheeler Apr 5 '11 at 1:33
1  
The FFT is a very smart hack, but there's no way you could call it beautiful. –  Kyte Apr 5 '11 at 2:15
1  
I agree with SnOrfus for the most part. However to me beautiful code must not only be necessary, sufficient and self-explanatory, but it must also subjectively feel perfect & light. I believe lots of code could qualify the criteria exposed by SnOrfus yet feel quite cumbersome or imperfect in some other way. I would lot call such code beautiful. –  asoundmove Apr 5 '11 at 2:16
1  
I do think the idea of "something reduced to essentials, but no more" is on the right track. However, something unmaintainable and written in the wrong language for the task would also fit this definition. Also, by requiring that it not require explanation, it is likely that most tasks fitting your definition must also be trivial. I don't think "Hello World" can be called beautiful. –  user21007 Apr 5 '11 at 2:24

I would refer to poetry:

A well-written poem have a different feel to it than an extract from a badly-translated manual about the same subject.

share

Coding problems are about conceptualization, so beautiful code represents a remarkable conceptualization of the problem.

For example, we like it when one problem can be reduced to an existing solved problem, lending insight into the nature of the problem itself.

Sometimes a re-conceptualization of the problem can make it seem so easy; we speak of elegant solutions that require simple tricks that simplify a difficult task.

To me, the Quicksort is a beautiful example: pick a random element from an array, and then compare every other element in the array to it; if the number is less than it, put it in pile A; if the number is greater than it, put it in pile B. Now, by the triangle inequality, no element in pile A will ever need to be compared to any element in pile B. Recurse on A and B, and you're done.

share

Think about a car.

Most of us look at the car, and can only see the body. If someone repairs some dings and puts a new paint job on the car, it will look much more beautiful. It's basically the same car, but that is all that we see.

A mechanic opens up the car, and looks at the engine. They see how good that engine is. They see that everything is arranged to be easy to get at, maintain, and work on. They see the parts and know how well designed it was. To an experienced mechanic, the engine of a well-maintained high quality car becomes a thing of beauty. They can't explain to you what makes it beautiful, but their notion of beauty is likely to translate into lower maintenance costs, a longer car lifespan and better performance. All of this adds to the worth of that car, even though you can't see it.

When you look at a website, or an application, you're looking at it like most of us see cars. When I look at code I'm looking at it like the mechanic looks at the engine of the car. You can never experience the beauty of that code like I do, it is literally invisible to you. But that beauty is important just like the mechanic's beauty is. It determines how well that software runs, what kinds of problems it will have, how easily the software can be improved, and so on. All of this adds to the worth of that software, even though you can't see it.

share

Reminds me of this:

http://imgs.xkcd.com/comics/lisp.jpg

Source : XKCD - LISP

share
5  
Better to have that as a link especially as the hover text is missing. –  Mark Apr 5 '11 at 12:49
1  
And the hovertext is half the joke in this case. –  user1249 Jul 8 '11 at 11:53

To me the language(s) are my tools.

And like any craftsman, I prefer my tools to be in mint condition.

The better the condition of the code, regarding clarity of concept, maintainability and readability. the better the code.

So for me, reading well written code is like opening a toolbox with well crafted tools.

I have used this analogy a couple of times with success. Especially people with a more practical or artistic background seems to understand the concept of clean/beautiful code this way.

share

I think it depends what you mean by beautiful code.

To me, code is beautiful when it’s readable. Aside from possible issues with programming concepts, a layman could read and, at least at a high level, understand what the code is doing.

As some other answers have discussed, code can also be beautiful in the sense that it achieves complex functionality by combining a couple of simple concepts. Not sure what sort of analogy would be best to explain that to a non-programmer, it depends on the layman. Puzzle toys come to mind, like the NeoCube.

share

If the person is well versed in mathematics, I like comparing beautiful code to an elegant solution to a problem.

share

No non-trivial code can be perfect because perfect code requires simultaneously satisfying goals that often conflict. Beautiful code, therefore, balances all the important attributes perfectly for the task at hand and comes closer to satisfying all of these simultaneously than most would think possible. In no particular order:

  • Readability
  • Conciseness
  • Efficiency
  • Flexibility
  • Explicitness
  • Robustness
  • Safety/idiot-proofness
  • Completeness
  • Consistency
  • Ease of use (for APIs)
share

There is no "beautiful code", there are "elegant algorithms" and "elegant designs". Design can be understood by non programmers.

I hate programmers who argue about "beautiful code", because one way or another, they either doesn't really know how an interpreter or a compiler will digest it and what the machine will do, so it's more like a children showing his mom how wonderful his story is, but really, it's not.

share

Do you explain people why a nice picture is nice? No. You show them the picture (because, you know, a picture says more than 1000 words). So the best is, to show them a small piece of code that is really elegant, beautiful, perfect (and maybe as comparison how Joe Average would have coded it).

share

In my humble and personal opinion, beautiful code is like a good novel book:

  • you can read it from beginning to the end / you should not need to skip ahead to future pages of a book to understand the current page.
  • it does not have unnecessary repetitions / a book is kind of dull if it is always saying the same things.
  • the intent of each part is evident almost every time / you should not need to have a dictionary by your side at all times to read a book (unless it's poetry / code golfing xD).
  • it is organised in substructures of affordable sizes and complexity (modules, functions, statements, etc.), it does not have too much 'nesting' / chapters, paragraphs and sentences are properly balanced; it does not use too many levels of sentence subordination, etc.
  • it is pleasant to the eye in a aesthetic manner (nicely indented, blocks correctly delimited, etc.) / like a book with a proper typesetting.
share

Some aspects of good code is:

  1. It has large number of small details that all point to same direction
  2. it is consistent in structure, every part following the same pattern
  3. but it does not repeat itself, instead every part is different
  4. it also does not allow behaviour that is considered invalid
  5. it has smallest possible number of different states
  6. you can understand the whole behaviour of the code by reading function prototypes
  7. it has no side effects
  8. the execution of it is guaranteed to stop
  9. it does not use any advanced features available in programming languages
  10. it is not too complex compared to the complexity of the problem it solves
  11. there is no errors or undefined behaviour in it
  12. it can be compiled with compilers from several different vendors
  13. it has no dependencies to code that is not used
share

It's like a lean mean company where everybody knows their role, it's easy to figure out who is good at what, and there's no duplication of effort

Vs.

Office Space where mediocrity is championed, three managers have nothing better to do than bug you about TPS reports, the !@#$ing FAX MACHINE NEVER WORKS!!! and any given employee might only get about 15 minutes of actual work done a week. An employee might not even technically work there anymore because somebody forgot to fire him (it's hard to tell really because nobody works efficiently and it's hard to evaluate what they're trying to accomplish). And to get anything practical done requires navigating an absurdly over-complicated system that somebody read about and thought would look neat-o on their resume even though it doesn't actually solve a problem they had.

share

Not the answer you're looking for? Browse other questions tagged or ask your own question.