Clean Code: Keeping Your Code Clean

Hana F. Ardiansyah
4 min readApr 4, 2021

--

source: https://unsplash.com/photos/XJXWbfSo2f0

Have you been in a situation, in hurry, and need to find something important in a messy room? Of course, something messy is very troublesome. Just like our code, to avoid things that can be difficult for yourself, then use ✨Clean Code✨

What is Clean Code?

Bjarne Stroustrup, inventor of C++ and author of The C++ Programming Language said...

I like my code to be elegant and efficient. The logic should be straightforward to make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and performance close to optimal so as not to tempt people to make the code messy with unprincipled optimizations.

Clean code does one thing well.

Then, Ward Cunningham, inventor of Wiki and the godfather of all those who cares about code once said…

You know you are working on clean code when each routine you read turns out to be pretty much what you expected. You can call it beautiful code when the code also makes it look like the language was made for the problem.

Some of the best practices.

  • Meaningful Names — We always name our variables, functions, arguments, and classes. Since we do so much of it, we better do well. When we write names in the project code, we always named them according to their use or context. We do this practice so that we can read the code easily and clearly.
Meaningful Names Implementation
  • Function — Functions are the verbs of that language, and classes are the nouns. This is not some throwback to the hideous old notion that the nouns and verbs in a requirements document are the first guesses of the classes and functions of a system. Rather, this is a much older truth. The art of programming is and has always been, the art of language design.
Function Implementation
  • Unit Test — Unit testing is a developer’s first line of defense for sleeping safely at night. Write them to improve our productivity as a programmer. Making the quality assurance department happy is just a side effect. Unit Test as part of Test Driven Development (TDD). Why TDD is a Clean Code? The problem that a developer might have is when they want to clean up code, but not everyone gets it right. This process can take some time and slow down the entire project. Instead, they want to develop a series of tests to ensure that the code is clean, reduces bugs, and completes it much earlier. Problems with code that need cleaning can create various bugs that slow down the entire team and can be difficult to solve. Those can increase the cost and time spent. This drives a process towards Testing-Driven Development.
Unit Test Implementation
  • Error Handling — Clean code is readable, but it must also be robust. These are not conflicting goals. We can write robust clean code if we see error handling as a separate concern, something that is viewable independently of our main logic. To the degree that we can do that, we can reason about it independently, and we can make great strides in the maintainability of our code.
Error Handling Implementation

Benefits of Code Cleaning

  • Clearness — Every line of code written needs to be read many times by co-workers during its lifetime. They have to fix bugs and add structure. Therefore, every programmer should pay attention to the code and make it as fresh and clean as possible.
  • Best Practices — In recent ages, the software best performs like unit testing, CI, TDD, etc. have been increasing very fast in terms of receiving. Executing clean code ethics is a foundational skill that pays off particularly well when it’s time to refactor code or carry code under testing. Clean code creates it easier to understand and trial.
  • Maintenance — Writing code is easy enough, analysis is hard. This is why so many programmers want to rewrite rather than do the heavy lifting of reading and understanding existing code. With clean code, you don’t have to blame anyone else for poor code quality, clean code is an ordinary, foundation for everybody to work on. So, in the end, by building supportable code, you increase the bulk of the time and the price for storing the code.

Conclusion

Keeping your code clean is an excellent way to enhance not only the advantages of your application but also the presentation of your programming. If you lack your code to put in the effort and look good, you should keep it in good shape.

Reference

Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin

https://www.sataware.com/app-development/code-cleaning-waste-of-time-or-good/

--

--