Bjarne Stroustrup: Recognizing Beautiful Code

Excerpt from a YouTube interview (Bjarne Stroustrup) on recognizing "beautiful" code. Stroustrup argues experienced developers can sense when code "smells" or mixes metaphors, that rules (e.g., the C Core Guidelines) can limit mistakes but cannot by themselves produce masterpieces, and that producing exceptional code requires inspiration and craftsmanship (analogized to Michelangelo and Van Gogh), which cannot be regulated.
So how do you inspire or ensure that the Einstein level of simplification is reached? So can you do code review? Can you look at code is there? If I gave you the code for the Ford F150 and said here, is this a mess or, or is this okay, is it possible to tell? Is it possible to regulate?
An experienced developer can look at code and see if it smells mixed metaphors deliberately. The point is that it is hard to generate something that is really obviously clean and can be appreciated, but you can usually recognize when you haven't reached that point. And so I have never looked at the IF150 code, so I wouldn't know. But I know what I ought to be looking for. I'll be looking for some tricks that correlates with bugs and elsewhere. And I have tried to formulate rules for what good code looks like. And the current version of that is called the C Core Guidelines. One thing people should remember is there's what you can do in a language and what you should do in a language. You have lots of things that is necessary in some context, but not in others as things that exist. Just because there's 30 year old code out there and you can't get rid of it, but you can't have rules that says when you create it, try and follow these rules. This does not create good programs by themselves, but it limits the damage from mistakes, it limits the possibilities of mistakes. And basically we are trying to say, what is it that a good programmer does at the fairly simple level of where you use the language and how you use it? Now I can put all the rules for chiseling in marble. It doesn't mean that somebody who follows all of those rules can do a masterpiece by Michelangelo, that is there's something else to write a good program. Just is there something else to create a important work of art that is some kind of inspiration, understanding, gift? But we can approach the sort of technical, the craftsmanship level of it. The famous painters, the famous sculptures was among other things, superb craftsmen. They could express their ideas using their tools very well. And so these days I think what I'm doing, what a lot of people are doing, we are still trying to figure out how it is to use our tools very well. For a really good piece of code you need a spark of inspiration. And you can't, I think, regulate that. You cannot say that I'll take a picture only, I'll buy your picture only if you're at least Van Gogh. There are other things you can regulate, but not the inspiration I think that's quite beautifully put.
It is true that there is, as an experienced programmer, when you see code that's inspired, that's like Michelangelo. You know it when you see it. And the opposite of that is code that is messy code that smells, you know when you see it. And I'm not sure you can describe it in words, except vaguely through guidelines and so on.
Yes, it's easier to recognize ugly than to recognize beauty in code, and for the reason is that sometimes beauty comes from something that's innovative and unusual, and you have to sometimes think reasonably hard to appreciate that. On the other hand, the messes have things in common. Sa.










