08May

“Clean Code” is the code smell of a missing language feature.

Posted by Elf Sternberg as Uncategorized

Uncle Bob has a passage early in his book where he criticizes the function below, calling it “too long” and “missing context”. I agree that it’s cluttered and hard to read, but his representative solution is, frankly, absurd. He turns this into a C++ class with static methods for providing the modifiers to the text, all the while ignoring the huge elephant in the code: it does two things.

Here’s how you should write this code:

This is a look-up table. That’s all it is; using an algorithmic guide, you’re looking something up, translating one thing into another. Formatted this way, this is shorter, more readable, and more extensible. This version is a little performance and memory-wonky, using the format() macro twice and string-ifying statics, but worrying about that is a premature optimization waiting to take root; as it is, this function pair is literally an ideal until profiling tells you otherwise.

And while I’m being snarky about using a grown-up language, there’s nothing in C++ that says you couldn’t achieve the same results. Rust’s match is nothing more than a switch statement turned into an expression and using the move-semantic to avoid memory leaks. C++ has both lambda expressions and move semantics. Java, as of Java 9, has tuples and lambdas and garbage collection. Go is, well, Go; you get what you pay for.

I’m not asking for much. I just want you to stop writing code like it’s 1998.. I was there. It wasn’t fun. Writing code isn’t a privilege, and lots of extra lines isn’t extra value, it’s extra liability.

Oh, notice something else? My version separates the formatting from the printing: it can be tested. Write the tests first.

2 Responses to “Clean Code” is the code smell of a missing language feature.

Ken Shirriff

May 9th, 2020 at 2:39 pm

I know this is a bit of a tangent, but please don’t write code like that if there is any chance that it will be internationalized. I know it’s efficient to piece together sentences from words and bits of words, but it is an internationalization disaster. After translation into, say, French or Chinese, fragments like “s” and “are no” will just yield nonsense when they are mashed together. The code doesn’t even work for English: pass in “mouse” or “man” and you get “There are no mouses” or “There are 2 mans”. Nowadays, there’s enough memory to store complete sentences. (I’ve had to fix a lot of code for internationalization, so I’m sensitive about this. My apologies for going off topic.)

Elf Sternberg

June 22nd, 2020 at 10:43 am

Oh, I got that part right away! I wouldn’t do anything like that in real life (and am, right now, firefighting with a team that thinks it’s perfectly okay, dammit). I’m much more attuned to accessibility than internationalization, as that’s what I was trained in, but I’m aware enough of I18N issues not to pull something like the above in real life.

Comment Form

Subscribe to Feed

Categories

Calendar

May 2020
M T W T F S S
« Jan   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031