"Clever" and "elegant" in programming are two very different things in my book. Let's thread!

An elegant solution is small, simple, clear. Elegant solutions tend to not bite you back whenever some small detail of the environment around it changes, and tend to outline patterns that can be reused.

Show thread

Or they even are a reuse themselves: noticing a pattern and applying a classic solution from the literature ("hey, this calls for a priority queue") or a library ("let's not reinvent XML parsing!") often ends up in more elegant code than ad hoc solutions.

Show thread

A clever solution is often also small but usually not simple: they may require exploiting some accidental but not essential property of the system, they may involve solving an architectural problem by peeking through an abstraction, etc.

Show thread

That's why the words "clever hack" often go together. They are often very context sensitive and end up being fragile. Lua's "NaN trick" is a great example: it was a smart way to pack values that worked great... until computers went 64-bit.

Show thread
Follow

Reading elegant code makes you think "this solves the problem and is very clear to understand". Reading clever code makes you think "oh, the person who wrote this must be very smart!". Hence clever often has a greater "wow factor" than elegant, unfortunately.

· · Mastodon Twitter Crossposter · 1 · 0 · 0

That is not to say that clever isn't sometimes needed. E.g. the solution I recently came up with to make LuaJIT support ARM64 stack pointers in lightuserdata is what I'd call clever ("flip bit 46 if bit 47 is set!") but it's definitely not elegant.

Show thread

It pragmatically solves the problem, but adds a dependency on the particular architecture's addressing scheme that ideally shouldn't be there. (And it's going to require re-patching when 52-bit pointers arrive.) I'm happy it works, but I'm not proud of it.

Show thread

OTOH, going full declarative in Kong schemas was an design choice driven by CS fundamentals that proved fruitful: when we introduced declarative config in Kong 1.1, I was able to compose our existing schemas, tack a YAML parser in front (I know) and ta-da! declarative config.

Show thread

I'm proud of this one: people were impressed at how quickly the feature turned out. I was literally asked if I had spent long nights on it, but I didn't: it materialized quickly because it was a fruit of compositionality.

Show thread

Sometimes it's hard to justify a design with "trust me, it will make things cleaner later on", especially when it blocks so many "clever" additions ("but we could fix so many special use-cases by adding a Turing-complete escape hatch!") but it pays to resist the temptation.

Show thread
Sign in to participate in the conversation
Mastodon

Server run by the main developers of the project 🐘 It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!