Elegance is a tough concept to define. Each person may think different things are elegant.

As an ex-physicist I consider elegance to mean when different phenomina can be treated in a similar way, typically operating within a particular parameter space. For example how General Relativity can be simplified down to Newton’s laws when masses are (relatively) low.

Other examples are how some situations can follow the same laws as others. Again gravity is an inverse square law:

\[F = \frac{G m_1 m_2}{r^2}\]

where \(F\) is force, \(G\) is the universal gravitation constant, \(m_1\) and \(m_2\) are the masses of the two bodies involved, and \(r\) is the distance between the two bodies.

Similarly electromagnetic radiation is an inverse square law:

\[I = \frac{P}{A} = \frac{P}{4 \pi r^2}\]

where \(I\) is the intensity at a point and \(r\) is the distance between the point and the source.

Both of these have similar solutions, but they apply under different conditions (mass vs electromagnetic radiation). Elegance.

This situation comes up in software development as well. For example the way the Unicode specification simplifies down to the ASCII alphabet when considering only a-zA-Z0-9 and special characters.


This is also the case for regular expressions. By default, grep uses regular expressions to search for text in a string. I thought this morning that I should write a version of grep that searches for literal strings1. I realised that for simple a-zA-Z strings (which is usually what I’m searching for) that regular expressions simplify down to literal strings.


  1. I know that this exists in fgrep and grep -F but I can never remember what it’s called. ↩︎