What is "elegant" code? - Stack Overflow
I see a lot of lip service and talk about the most "elegant" way to do this
or that. I think if you spend enough time programming you begin to obtain a sort of
intuitive feel for what it is we call "elegance". But I'm curious. Even if
we can look at a bit of code, and say instinctively "That's elegant", or
"That's messy", I wonder if any of us really understands what that means. Is
there a precise definition for this "elegance" we keep referring to? If
there is, what is it?
Now, what I mean by a precise definition, is a series of statements which can be used
to derive questions about a peice of code, or a program as a whole, and determine
objectively, or as objectively as possible, whether that code is "elegant"
or not.
May I assert, that perhaps no such definition exists, and it's all just personal
preference. In this case, I ask you a slightly different question: Is there a better
word for "elegance", or a better set of attributes to use for judging code
quality that is perhaps more objective than merely appealing to individual intuition
and taste?
Perhaps code quality is a matter of taste, and the answer to both of my questions is
"no". But I can't help but feel that we could be doing better than just
expressing wishy washy feelings about our code quality. For example, user interface
design is something that to a broad range of people looks for all the world like a
field of study that oughtta be 100% subjective matter of taste. But this is shockingly
and brutally not the case, and there are in fact many objective measures that can be
applied to a user interface to determine its quality. A series of tests could be
written to give a definitive and repeatable score to user interface quality. (See GOMS,
for instance).
Now, okay. is Elegance simply "code quality" or is it something more? Is it
something that can be measured? Or is it a matter of taste? Does our profession have
room for taste? Maybe I'm asking the wrong questions altogether. Help me out here.
Bonus Round
If there is such a thing as elegance in code, and that concept is useful, do you think
that justifies classifying the field of programming as an "Art" capital A,
or merely a "craft". Or is it just an engineering field populated by a bunch
of wishful thinking humans? Consider this question in the light of your thoughts about
the elegance question.
Please note that there is a distinction between code which is considered
"art" in itself, and code that was written merely in the service of creating
an artful program. When I ask this question, I ask if the code itself justifies
calling programming an art.
Bounty Note
I liked the answers to this question so much, I think I'd like to make a photographic
essay book from it. Released as a free PDF, and published on some kind of on demand
printing service of course, such as "zazz" or "tiggle" or
"printley" or something . I'd like some more answers, please!
It's the intersection of simplicity and functionality.
Elegant code is code that does in a few crystal clear lines what you would think would
take heaps of complex code to achieve.
My rough definition of "elegant code":
Code which is written such that the number of lines/statements is minimized while
the readability and functionality is maximized.
Or, to put it another way:
The ratios of readability to number of statements and functionality to number of
statements are both high.
I've specifically included readability here. There are plenty of "one-liners"
that get a lot done, but if no one can tell what they are doing, they aren't elegant.
"Elegant" is a good word choice because it implies a sense of aesthetics.
Deciding what is elegant and what isn't is an aesthetic judgement in code just as in
other fields. It is subjective. What's elegant to one programmer might not be to
another.
To expand the definition a little, you could say elegant code is code that gets
something non-trivial done in a way that is simple. I don't think anyone disagrees on
the non-trivial part, it's the simplicity that is the bugbear. Some people think
simple means "as terse as possible", other people think it is "as easy
to understand as possible", and many opt for some intersection of the two.
The code cannot be elegant as a whole without an elegant design. I think design
elegance can be roughly measured using the MIT or "worse is better" approach
measured by degrees of:
* Simplicity
* Correctness
* Consistency
* Completeness
A long time ago I thought about what attributes contribute to elegance. Here is the
list that I came up with. Some of these apply to hardware as well, and there is some
overlap, but anyway:
* accurate, correct (operates exactly as instructed)
* adaptable (easy to modify to suit other purposes)
* attractive (aesthetically pleasing (but preferably not at the expense of functionality))
* compact (small, lightweight, portable)
* comprehensive (complete, thorough, solves for the general case)
* consistent (predictable operation and user interface)
* controllable (easy for operator to maintain complete control)
* convenient (desirable to use)
* customizable (tweakable)
* durable (robust, rugged)
* economical (inexpensive, cost-effective, low total cost of ownership)
* effective (well-suited to intended purpose)
* efficient (fast or otherwise resource-efficient)
* environmentally-friendly (non-toxic; not wasteful)
* ergonomic (comfortable; not harmful to operator)
* extensible (easy to add new features)
* failsafe (prevents undesired operation; discourages misuse; enforces proper
operating sequences; capable of failing gracefully and safely (but preferably not
silently))
* functional (designed with function as a priority to form)
* interoperable (compatible with other systems)
* intuitive (user-friendly, unambiguous user interface)
* maintainable (components are easy to access, repair, and replace)
* modular (components are interchangeable, or system is interchangeable as a whole)
* orthogonal (features operate independently of each other, or in any sequence, or
all similar features are equally capable)
* practical (economical; quickly and easily deployable)
* precise (results are reproducible)
* quiet (not contributing to noise pollution)
* recyclable (components can be safely and easily recycled to reclaim materials)
* reliable (stable, dependable, fault-tolerant)
* reusable (components can be used for other purposes)
* safe (non-toxic; alerts operator to potential problems)
* scalable (suitable for use with larger-than-optimal workloads)
* responsive (responds quickly to operator changes or changes in operating conditions)
* secure or securable
* self-correcting (automatically adjusts to improve precision)
* self-tuning (automatically adjusts to improve efficiency based on current
operating conditions)
* side-effect-free (does nothing beyond the stated function)
* simple (straightforward, no superfluous features)
* standards-compliant (conforms to established standards)
* sustainable (can operate continuously)
* upgradeable (easy to replace components to increase speed or capacity)
* verifiable (testable, provable)
* versatile (multipurpose, flexible)