About programming
Jump to navigation
Jump to search
Elegant code
From Slashdot:
Elegant != Complicated.
—
There are two ways of constructing a software design: One way is to make it
so simple that there are obviously no deficiencies, and the other way is to
make it so complicated that there are no obvious deficiencies. The first
method is far more difficult. --Tony Hoare
—
Elegant code:
- Handles errors gracefully
- Only reinvents the wheel when there is a measurable benefit in doing so.
- Has consistent naming conventions.
- Has comments around ugly code explaining why it is ugly.
- Compiles (or runs if interpreted) without warnings.
—
Elegant code is...
- Simple — leveraging the "natural" way to use the programming language
- Compact — not cluttered with special cases and boilerplate
- Logical — like secondary documentation, acting as a clear description of how to solve a problem
- Modular — functions or classes should be clearly grouped as modules
- Easy to understand — not full of stupid hacks and "clever" tricks
- Reasonably efficient — performing reasonably well, not at the expense of simplicity
- Maintainable — any decent programmer could pick up the code without fear and trepidation
- Commented — some comments should be present, but not too much
- Correct — it should do what it is meant to do, and only this