Imagine that you are starting with a new project with your team. You have the client’s legacy code and now your task is to go through it. Glancing over the code, you realise your worst nightmare as a Developer. The code has no comments, no explanation of what a method is achieving, methods are hundreds of lines long, there is a vague concept of encapsulating related items in a space. Every thought going through your head is sure to be a cuss word. If that’s what is happening in your head, then the code that you are reading is not a Clean Code.

As eloquently noted by Robert Martin in his book “Clean Code”, the only valid measurement of code quality is the number of WTFs per minute. If that count has reached a good number by just going through one of the modules, then that code is not an example of Clean Code.

Why do we need Clean Code?

On more formal terms, the quality of a code directly correlates to the maintainability of the product. Under the pressure of a deadline, you might rush to go faster, leaving a messy code. You get your feature to work, but now a bug pops up. You go back to your messy code, and even you are not able to make any sense out of it. This add-on of debugging the messy code will generally take much more time than the amount of time you spent on writing the code. On the other hand, if you would have written a “cleaner” code, with a second glance you would have been able to tell what every line of your code is doing, and debugging would have been that much easier.

Just keep in mind, Programmers are really authors, and their target audience should not be the computer, but other developers. If another developer can read your code without mocking you, then well you have written a clean code.

How to Write Clean Code ?

Meaningful Names

Every variable, method, class, package in our code has a name. So, naming each of them appropriately should be the first concern of a Good Programmer.

####### Use Intention Revealing Names

The name of a variable, function, or class, should answer all the big questions a developer might have. It should tell him why it exists, what it does, and how it is used. If the name requires a comment along with it, then the name does not reveal it’s intent.

#scala #tech blogs #best practices for coding #clean code #healthy code

Let’s Clean that Code
3.65 GEEK