Beautiful Code

I was having a conversation with someone a few weeks back about the beauty you can often find in well-written source code. We were exchanging examples, and I started to think about how I would explain what makes code beautiful to someone else, other than by example.

Unlike the visual arts, we don't really have a concise, descriptive vocabulary for describing the aesthetic qualities of computer source code. Whereas you have words like proportion, balance, and complementary which actually mean specific things when talking about a painting or sculpture or building, more often than not when you want to tell someone how to make a piece of code more beautiful, your best bet is "make it more like that."

That's why it's especially important in computer science education, as well as in practice at companies that develop software, to have a canon of beautiful source code to which you can refer.

Just as there is no one universal definition of beauty, and the perception of beauty varies significantly from population to population, there is no one canon of universally beautiful code. I've found that really good engineers keep their own catalog of beautiful code that they've encountered over their careers. For those looking for potential sources of inspiration, here are a few suggestions:

  • Jon Bentley's Programming Pearls is a classic, full of good code and elegant solutions to digestible problems.
  • Dave Hanson's C Interfaces and Implementations is full of beautiful code. Check out the chapter on arenas, a still very relevant way to think about memory management for certain types of programs.
  • Greg Wilson and Andy Orem actually wrote a book called Beautiful Code a few years back. It has contributions from some of the best coders in the history of our discipline, with examples of their code.

This is a woefully incomplete list. I'd love to hear from others about their sources of coding inspiration, and where they find beauty in the practice of their craft.

Clever code is the enemy of Beautiful code. Code can be efficient and well written but also so clever that its hard to follow. Clever Spaghetti code is the real enemy.

Like
Reply

I think "Clean Code" by uncle Bob should be on the top of the list :)

Like
Reply

Let me provide a DEFINITION, as follows: Given a specification S, and given two programs P1 and P2 which both fulfill S in a correct manner, i.e.: it is presumed that both P1 and P2 are internally consistent and error-free. Moreover, let C be a suitable reversible file compression program which can remove redundancy without destroying essential information. Now let C(P1) and C(P2) be the compressed program files corresponding to P1 and P2. On this basis we call program P1 "more BEAUTIFUL" than program P2, with regard to S, if |C(P1)| < |C(P2)|, whereby |F| denotes the size of a file F in number of bytes. (In case you agree with my definition you can reference it as [Gruner-Linkedin-2014].)

Like
Reply

For me a beautiful code is a work of an Engineering artist with passion, strive to follow process and standards, and who believes in the truth of one day leaving his legacy behind for someone else to take care.

Beautiful code is like looking up at the stars at night, seeing all the amazing connections and the bright `stars' and planets revolving round the sun, perhaps glimpses of other galaxies + universes?

Like
Reply

To view or add a comment, sign in

More articles by Kevin Scott

  • Microsoft at NeurIPS 2023

    This week, many AI researchers and practitioners were at NeurIPS 2023 in New Orleans, the largest and perhaps most…

    26 Comments
  • A New World of AI Apps

    A few weeks ago at #MSBuild, I got to share our vision for how AI models like OpenAI’s Codex and tools like GitHub…

    8 Comments
  • Announcing GitHub Copilot

    Today, GitHub, OpenAI and Microsoft launched a technical preview of GitHub Copilot, a collaboratively developed new AI…

    80 Comments
  • Technology and Creativity at Build 2021

    Earlier today, I had the great pleasure to virtually address thousands of programmers and developers around the world…

    4 Comments
  • What we’ll need to survive, and thrive, amid economic uncertainty

    To make ends meet, my dad was a real hustler. He had been a tall, good-looking center for the high school basketball…

    56 Comments
  • Being of Service

    I learned two very important things growing up in a small, close-knit community in rural America: that the well-being…

    13 Comments
  • Supporting Entrepreneurs Everywhere

    I was in Memphis this week to join Steve Case, JD Vance, and a host of entrepreneurs, investors, and community leaders…

    20 Comments
  • Perspective and Remembering my Grandfather

    Life comes at you so fast and so intensely sometimes that it can be easy to lose perspective. When you're trying to do…

    19 Comments
  • Browsing the Future

    In the technology business, it sometimes seems like we spend all of our time hitting refresh. Startups come and go.

    6 Comments
  • Ignite 2017 Wrap Up

    While I’m not in Orlando with our Microsoft teams this week, watching them from a distance gives me an interesting…

    12 Comments

Others also viewed

Explore content categories