DEV Community

Cover image for The term "clean code" is misleading - this is my take

The term "clean code" is misleading - this is my take

Keff on October 15, 2021

Hey there 👋 I'm back with another short little post. There are many articles about this thing we call "clean code". But I feel they lack ...
Collapse
 
jonlauridsen profile image
Jon Lauridsen • Edited

I’d say clean code refers to the principles of Clean Code. Even if that’s not the intent of whomever uses the phrase it’s the context that surrounds that topic. There’s now a decade+ worth of discussions on top of it so it’s not to say every clean code discussion has to reference a Clean Code chapter, but I dare say it’s crucial to have the awareness of what the words “clean code” originally applied to (whether you agree w. the book’s principles or not) to guide such discussions productively.

Collapse
 
nombrekeff profile image
Keff

Ohh I did not realize the term originally came from the book, good to know! I read the book years back, but thought the term was already established.

Collapse
 
janpauldahlke profile image
jan paul • Edited

at leat you are a honest man. keep on thinking things for your self. but know your meme, in this case books, as well ;-). sapere aude

Collapse
 
francescobienne profile image
Fra

Keff you wrote this article without know that?!

Thread Thread
 
nombrekeff profile image
Keff

Yup a little mistake. As I said, I have read the book and have done a bit of research on the subject, but never realized it was originally from the book (or I might've forgotten about it) 😅

Collapse
 
ksaaskil profile image
Kimmo Sääskilahti

For me, clean code means knowing and applying established coding and design principles like "single responsibility", "program against interfaces", "composition over inheritance" , "comment why, not how" "apply formatting", "write unit tests", "avoid global mutable state", etc. I don't think they are "just opinions", but a result from long real-world experience building maintainable software. There are good reasons why such principles exist. Of course they're not "truths" either, but every developer should know why they exist so they can weigh whether to follow them or make an exception.

Collapse
 
ksaaskil profile image
Kimmo Sääskilahti

Google's code review guide says this about design:

"Aspects of software design are almost never a pure style issue or just a personal preference. They are based on underlying principles and should be weighed on those principles, not simply by personal opinion. Sometimes there are a few valid options. If the author can demonstrate (either through data or based on solid engineering principles) that several approaches are equally valid, then the reviewer should accept the preference of the author. Otherwise the choice is dictated by standard principles of software design."

I think many of the "clean code" principles are such standard practices and not highly opinionated.

Collapse
 
nombrekeff profile image
Keff

Thanks for your take, I agree with you.

I don't think they're just opinions, though some could be interpreted as such. Over time I have seen many beginers and younger folks misinterpret the meaning of it, or even more advanced developers writing posts that just scrap the surface and make people think that's all there is too it. The main point I wanted to get across (though I might have failed) is that it's not something that is done immediately or just by using the principles. As you said one must understand why they are used, when to use them and be consistent thoughout the life of the codebase. It takes time, discipline and knowledge.

Collapse
 
ksaaskil profile image
Kimmo Sääskilahti

Thanks for explaining! I've also seen some posts here titled such as "Don't use else-if", such opinion pieces are easily confused with deeper principles that I think of as "clean code". Thanks for your good and thoughtful post!

Thread Thread
 
nombrekeff profile image
Keff

No worries, glad you liked it! And thanks for your addition.

Yup, those types of posts kinda inspired me to write this post!!

Collapse
 
lexlohr profile image
Alex Lohr

I too think 'clean code' is ambiguous and confusing. I prefer 'code describing the problem it solves' and while this is not as concise as 'clean code', it's much better to convey the idea.

When you start your career as a developer, your aim is to write code that runs without errors. With more experience, your aim extends to code explaining itself in terms of the solution it provides. And lastly, you want the code to describe the problem it solves.

As a simple example, styling comes to mind (which is woefully underrated and thus usually not written well). Let's consider a price tag:

<!-- beginner -->
<b style="background: yellow">$ 4.70</b>

<!-- advanced -->
<span class="bg-yellow bold" aria-label="USD 4.70">$ 4.70</span>

<!-- expert -->
<style>.price-tag { font-weight: bold; background-color: yellow; }</style>
<data class="price-tag"><currency aria-label="USD">$</currency> 4.70</data>
Enter fullscreen mode Exit fullscreen mode

You might ask: but what if I compose my styles in a component with tailwind like the advanced developer? Use a descriptive variable name to hold the different class names.

Why describing the problem and not the solution? Because knowledge of the problem helps understanding the solution more than knowing the solution.

Collapse
 
insidiousthedev profile image
Insidious

Just use Prettier cuz it makes the code "clean".

Collapse
 
nombrekeff profile image
Keff

True that! ;)

Collapse
 
xtrembaker profile image
xtrembaker

As "Uncle Bob" says, Clean code is

  • 0 WTF per minutes
  • Done by someone who cares

youtu.be/7EmboKQH8lM?t=682

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman

Maybe they mean "clean code = consistency"

Collapse
 
nombrekeff profile image
Keff

Yeah, that's probably the case, though why not just call it "consistent code"?

Collapse
 
sherrydays profile image
Sherry Day

I generally agree

Collapse
 
nombrekeff profile image
Keff

Great! Can I ask where you differ?