Clean your code, please

source: https://res.cloudinary.com/wlion/f_auto,q_auto,c_fill,w_800/wlion/2017/04/CleanCode.jpg

wow hold up for a sec there cowboy! no one could enter this room looking like that, clean yourself and your code ASAP!

lets be honest here, we’ve all heard about clean code before… it's not going to be an unfamiliar topic for you if you are reading this article right?? or is it?

well, it's quite an intriguing topic for me for a really loooooong time, majoring in a CS degree for almost 3 years now, it surely is a topic that's going to be brought up from time to time among my peers.

relax… I will try to explain clean code in the “cleanest” and most straightforward way in this article, I promise you by the end of this article, you would have the knowledge to explain clean code to anybody else who might also ask the same question 😸

So what is clean code?

well in actuality, the definition of what a “clean” code is, is very subjective and idiosyncratically personal for every developer. well yeah, it might be true that there are some ideas that might be considered as the ideal way or best practices on doing some things, what constitutes as clean code within the industry and community has always been vague with no definitive distinction.

butt, if I could summarize it into a single explicit line of definition after “tons” of research, then it would be this.

clean code is a code that is both easy to understand, and easy to change

wait that's it?

yeah, to be honest with you it's quite simple, isn't it?

well, let us break it down into simple parts and examine the points behind it.

what it means to be easily understood is to be easy to read, whether by the original author or anybody else. it should be as plain and as simple as reading a book, with minor technobabble of course ;).

a code is like a joke. when you *have* to explain it, it's bad

it means that the meaning is clear to the point minimizing the need for any guesswork to happen and possibly clear any misunderstandings.

if I have to specify it into different specific stages, it would probably be :

  • easy to understand the entire execution flow
  • easy to understand how different objects communicate with each other
  • what each and every class do is clear and easy to understand
  • each method, expression, and variable must be clear of its purpose

the next is easy to change.

well, a code is easy to change if it is easy to be extended and refactored. it is also imperative that your code is easy to fix if some bugs were to appear in later development

through the definition above, a code is easy to change if :

  • classes and methods should have only one responsibility
  • the code is easily testable
  • the tests are easy to understand and change

as you can see in the example above, it is probably quite clear why we should create a clean codebase… or not?

So, why should you care?

“Clean code is code that has been taken care of. Someone has taken the time to keep it simple and orderly. They have paid appropriate attention to details. They have cared.” — Robert C. Martin

but still, why should you care? what if it *just works*, what's the problem with it?

well, the actual problem is a codebase is almost never written only once, most of the time either you or somebody else would likely be (re)writing it again or using it in some way, that in order to efficiently work on it you need to understand the code.

and so we must create a code that is not only understandable by computers, but also by humans.

“Programming is the art of telling another human what one wants the computer to do.”— Donald Knuth

by coding a “clean” code, you are helping your future self and your coworkers. simply reducing the amount of maintenance and service of the application or project that you are writing.

a lot, and I mean a lot of headaches could be avoided by simply choosing the correct class name, or splitting a method into two.

by applying the principles for clean code, you basically are making sure that the work in the years to come will be much more enjoyable and efficient, for you and everyone involved in the project.

So, In conclusion…

in this post, I have been trying to convince you to write “clean” code, but as a side note, don't be too obsessed with it. the most important thing is still to create a code that both works and generates value.

spending hours making it perfect but neglecting the actual purpose of your code is plain wrong.

it won't happen on your first try anyway, just keep coding and you will improve in both mindset and skill, that way your next code will always be better than your previous one.

so always be wary of mistakes, but don't be “that” guy yeah.

that's it for today folks, I hope you learn a thing or two by reading this.

cheers! 👋

a software engineer trying new things