Code has style when you can't describe why the code is so good. Because when you try to describe it, you know you're selling it short - the code is more than simply its specification. The only way to convey the quality of the code is by reading the code itself. Code has style when you'd rather read the code than an explanation of what it does.
For me, it's always a matter of simplicity. When a problem is hard to describe in English, you naturally assume the code will be a bit complex as well. And yet, sometimes, the problem can be decomposed so neatly that the parts are trivial and fit together effortlessly. For me a requirement is that after carefully reading the code you are absolutely certain there is no room left for bugs. You almost know for certain that the code didn't have any bugs when it was written down for the first time, because it captures intent so well. If you need to refactor it's probably too late already.
So I think books like "Beautiful code" got it entirely wrong. I especially don't agree with icey's opinion that refactoring can make code tasteful or that linebreaks can make or break greatness. It's about bringing human thoughts to computer code. And when that mapping is perfect, the code is great.
I know exactly what you're talking about! To me, you can look at a piece of code, and know if it has a certain Feng Shui to it...
Code that is only dense when it has to be, and is commented when it is.
Artful use of white space.
Code that has obviously been refactored once or twice.
Other than that, it's sometimes hard to describe. But it is very easy to tell if you're looking at code from someone who loves to code versus code written by someone who just does it for a paycheck.
Take the evil out the people they'll be acting right
'cause both black and white is smokin' crack tonight
and only time we chill is when we kill each other
it takes skill to be real, time to heal each other"
there both from Tupac Shakur, but the first one is from the poem " the rose that grew from concrete" and the second one is from the song "Changes" The difference is that in the first one its just words put together with a meaning, they say something. But the second one i like more, because of the rime and the rhythm. The first one is written on paper and published to be read, but the second one is from a song, it has to conform to the rhythm of the song, the words are structured to fit with the music and the flow, the second one has structure and architecture and it has to be simple enough to fit with the music and the beat. Making the second one is harder and its more beautiful. When i see good code, for me that means that it follows the flow of the readers mind, the code just fits and does its job, no fancy magic stuff, just simplicity and beauty. It has to be easy to understand too.
The first one is good of course, but the idea is that i liked the structure and rhythm of the second one. If i was looking for poetic expressiveness i might rethink my choice. The idea of taste is that you look for something, and if you find it in an object, you like that object. If you are looking for strange symbolist metaphors in poetry, you don't look at realism poetry, you look at symbolism. And in PG's essay about taste he said that people just have a lot in common, so they are generally looking for the same stuff.
I like to see code that my mind can parse as easily as the machine can. Tasteful code likely is short, efficient, and readable... but perhaps there are exceptions to that? Good taste is like obscenity - hard to define but you know it when you see it.
This is the same as asking why certain pieces of art or music are beautiful. Any two random people would likely disagree over their interpretations of beauty, but as a whole, many works would likely receive a drastically high 'beautiful' vote, like Pachelbel's Canon.
That said, with code, I think there are a few properties which are considered 'beautiful' or 'tasteful', not in and of themselves, but combined with that impalpable aesthetic we call 'beauty'.
Of all of these, I think the only one most programmers would agree on is that truly incredible code is nominally quite brief.
People have a hard time appreciating poetry in their second or third language, because you need to have such vast knowledge to get the subtleties of the poet's intentions.
I suspect the same holds for code. The more skilled and more experienced the programmer, the more complexity he can handle. And he'll find beauty where mortals like us are simply confused.
I can't appreciate Shakespeare, but I suspect it is as great as many claim it is. I would probably dismiss Shakespeare as unnecessarily complex and verbose if I didn't know better.
Worth thinking about the next time you're annoyed at a piece of complex code, and really want to refactor it so everybody can read it. You may be destroying something beautiful.
I agree with you absolutely with regards to no two people having the same exact idea, but I don't think even the latter case is an exception - brief may be efficient but not "stylish" and "beautiful." Just like the other properties, it depends on the person looking at it:)
I don't know if it can be boiled down to one thing. One likes to see mastery at every level, from the high-level design down to strict adherence to coding conventions.
The software has to have a strong opinion about what it's doing. It's modelled the problem in a way which leads to a straightforward solution, using a minimum number of concepts.
According to the conventions of the language in which it was written, the visible structure must reveal the underlying concepts. To a reasonably experienced practitioner, the code documents itself and comments are infrequent.
It's much easier to see what's bad about code written by other people. I suggest you look at some really bad code, and then try and imagine what a superior programmer would find offensive about your code.
You can always visit thedailywtf.com if you're lucky enough not to be exposed to bad code on a daily basis. My personal pet hates are copy-and-paste code, inconsistent formatting, excessively long lines, bad or non-existent naming conventions, Hungarian notation, and spelling mistakes - pretty basic stuff.
I'd like to add a subquestion: I'm always told to check out other people's code and study it, but I'm also well aware that popular software doesn't necessarily mean good code. What source-available projects would you recommend I look at if I want to see some quality examples of the craft?
The OCaml interpreter/compiler is really good. Compare with the Ruby interpreter, which is really bad. SBCL is also interesting; has a lot of good parts.
I recommend finding a mentor. Someone who writes code that you respect (be it for quality, clarity or efficiency). Start reading their code and asking questions.
The open source community is a good place to start looking if you don't have someone you respect in your own community.
Python. Not all the code is good, but I remember reading a lot of Python code when I was trying to write an interpreter of my own, and some parts of it just left me starting at the screen in amazement, due to the beauty and simplicity of some of the ideas.
Maybe I'm not the best at reading badly commented C code, but Ruby and MzScheme were not nearly as pleasant.
For me, it's always a matter of simplicity. When a problem is hard to describe in English, you naturally assume the code will be a bit complex as well. And yet, sometimes, the problem can be decomposed so neatly that the parts are trivial and fit together effortlessly. For me a requirement is that after carefully reading the code you are absolutely certain there is no room left for bugs. You almost know for certain that the code didn't have any bugs when it was written down for the first time, because it captures intent so well. If you need to refactor it's probably too late already.
So I think books like "Beautiful code" got it entirely wrong. I especially don't agree with icey's opinion that refactoring can make code tasteful or that linebreaks can make or break greatness. It's about bringing human thoughts to computer code. And when that mapping is perfect, the code is great.