in reply to IYO, what is elegant code?
Elegance is a funny thing. You know when it's not there, but sometimes it's hard to articulate why it is there when you're seeing it.
To my eye, an elegant piece of code does what it needs to in a way that's easy to follow, without any extra bells-and-whistles (including needless commentary). "Easy to follow" implies well formatted, with just enough horizontal and vertical whitespace. It also implies carefully chosen names, and control-flow structures that match the problem. It implies a well-factoredness, with a certain coherence to each subroutine/method.
A truly elegant piece of code teaches you something good about how to reason about a problem.
An elegant piece of code is written first to inform the reader, and second to be executed by a machine. Sometime, when speed is needed, elegance can be undone.
|
|---|