Just because it’s working, doesn’t mean it’s good
“My code is working well, the website I built is looking great, and my client is happy. So why would I still care about writing clean code?”
If this sounds like you, then read on.
A little while ago, I was having a discussion with one of my friends, let’s call him Khalid. Khalid is an experienced programmer. He was working on a complex project, and he was discussing a problem with me. When I asked to see the code for that problem, he said, sounding proud, “I built this project so we are the only ones who can understand the code.”
I was pretty horrified. I asked him if he deliberately wrote dirty code.
“The client didn’t give me enough time,” my friend told me. “He is always in a hurry and pushing for deliveries, so I did not have time to think about cleaning it up.”
This is almost always the excuse I hear when I ask about dirty code.
Some programmers write dirty code because they plan to release the first working version and then work to make it clean. But it does not work; no client gives you time to clean code. Once the first version is released, they will push you for the second. So, make it a habit to write code as clean as you can from the first line of code.
I’ve always learned that using clean code principles has many benefits down the line, and this post will show you why.
It is the job of the project manager, sales head, or client to get the project done in minimum time so they can control the cost of the project. But producing quality, clean code is your duty as the programmer.
Writing clean code is not a big or time-consuming task, but making it your routine, and committing to it, will go a long way toward advancing your career and improving your own time management.
Clean code always looks like it was written by someone who cares.
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler
You’ve probably read this far for two reasons: First, you are a programmer. Second, you want to be a better programmer. Good. We need better programmers.
Keep reading to learn why clean code matters, and you’ll become a better programmer.
Why Should We Strive for Clean Code?
Clean code is readable and easy to understand by everyone whether the reader is the author of the code or a new programmer.
Writing clean code is a necessary mindset. It takes practice to write clean and structured code, and you will learn to do it over time. But you need to start with the mindset of writing this way. And you’ll get used to reviewing and revising your code so it’s the cleanest it can be.
No one is perfect, and so you are not either. You always will find some opportunity to improve or refactor the code when you come back to review your code after a few days or weeks. So, start writing the code as clean as you can from the first line of code so later you can work more on performance and logic improvement.
Benefits of Clean Code
“Why should I care about writing clean code?” you may still be asking yourself.
There are many reasons to get into the clean code mindset I described above. Some of the most important reasons are:
Better Use of Your Time
The first beneficiary of clean code is the programmer themselves. If you are working on a project for months, it’s easy to forget things you did in the code, especially when your client comes back with changes. Clean lines of code make it easier to make changes.
Easier Onboarding for New Team Members
Using clean code principles helps to get a new programmer onboard. There is no need for documentation to understand the code; the new programmer can directly jump into it. This also saves time for both training the new programmer as well as the time it takes for the new programmer to adjust to the project.
Easier Debugging
Whether you write dirty or clean code, bugs are inevitable. But clean code will help you to debug faster, regardless of how much experience or expertise you have.
And it’s not uncommon for your colleagues or managers to help you solve the problem. If you’ve written clean code, no problem: They can jump in and help you out. But if your manager has to work through your dirty code, well, you might end up like my friend Kabir.
More Efficient Maintenance
“Of course bad code can be cleaned up. But it’s very expensive.”
―Robert C. Martin
Maintenance does not refer to bug fixing. As any project grows, it will need new features, or changes to existing features.
How To Write Clean Code?
Now, getting into the real question, how to actually write a clean code? Well, I’m gonna tell you about my personal experience here in PPL course building an Android app using Kotlin. One of the simplest thing you can do to write a cleaner code is very simple, just write a descriptive variable.
“You should name a variable using the same care with which you name a first-born child.” ―Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship
A programmer is an author, but they might make the mistake in identifying the audience. The audience of a programmer is other programmers, not computers. If computers were the audience, then you might be writing the code in machine language.
So, to make it easy to understand for your audience, you should use meaningful nomenclature for variables, functions, and classes. And make it more readable by using indentation, short method, and short statement, where appropriate:
Use easily pronounceable names for variables and methods.
Do not use abbreviations in the variable and method names. Use the variable name in full form so it can be easily pronounced and everyone can understand it.
Look at the picture, that’s how i declare a variable, nothing fancy there but, i just make a clear variable name so that people can easily guess what each variable means. Avoid naming a variable like lgnBtn, make it clear!.
Use the name to show intention.
The purpose of the variable should be understandable to someone reading the name of the variable. Write the name as you would speak it.
It’s another example about how i name my variables and methods, it’s very straightforward, and it works just like how it named. If people can’t tell the purpose of a variable or a method just by it names, you name it wrong, rename it!
Be consistent.
Use one word for similar functions. Don’t use “get” in one class and “fetch” in another.
Don’t hesitate to use technical terms in names.
Go ahead, use the technical term. Your fellow programmer will understand it. For example, “jobQueue” is better than “jobs.”
Use a verb as the first word in method and use a noun for class.
Use your programming language preferred case for naming variable (either snake_case or camelCase for kotlin in my case) for variable and function name. The class should start with capital letter.
Limit a method to perform only a single task
You should also limit a function or method to a single task. (Avoid using “and” in a method name, like “validateAndSave.” Instead, create two methods, one for validation and another for save). For example:
Notice that i separate the function to get and to update, so that my function will only perform a single task, no more.
Actually there are more ways to write clean code, but i think my examples are the most basic one to start with. If you want to be a better programmer, try apply those examples and keep practicing! Cheers!