An Episode of Clean Code

Photo by Moritz Kindler on Unsplash

Hello there, welcome to an episode of Clean code! In this episode, we will talk about everything you need to know about applying the clean code to your code, of course.

For starters, let’s talk about what clean code is and what makes it so “clean.” As Robert C. Martin stated in his book Clean Code: A Handbook of Agile Software Craftsmanship,

“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.”

there is also another quotation from Bjarne Stroustrup, inventor of C++ and author of The C++ Programming Language:

“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.”

You will notice that a written code is a lot more than just a working code from the quotes above. It has to be simple, elegant, efficient, and many more adjectives you can find in those quotes. You might be thinking, “Why should I care?”

Well, you should. Because the code you write is rarely just be written and forgotten. There might be times when you or other people need to work on the code, and you need to understand the code well to work on it again. If you write clean code, then you are helping yourself and your co-workers in the future.

Characteristics of Clean Code

  1. Simple: The code follows the Single Responsibility Principle (SRP)
  2. Elegant: Pleasing to read
  3. Readable: Easy to read
  4. Testable: Run all the tests

Next, we will see how to write the clean code based on these characteristics and definitions above.

How To Write Clean Code

Meaningful Names

Example:

Do

  • Use intention-revealing names
  • Pronounceable
  • Searchable
# good
today_timestamp = Time.current

Don't

  • Unpopular acronyms
  • Using confusing letters for names
# bad
today_ts = Time.current
t= Time.current

Functions

Looking back to the characteristics, the function should be following the Single Responsibility Principle, which means that the function needs to only one thing. Here are some tips to make your functions cleaner:

  • Use less than 3 arguments
  • Don’t do code duplication (DRY — Don’t Repeat Yourself).
  • Use descriptive names

Comments

Code Organization & Formatting

  • Group code by their functionality, the related function must be close.
  • Make sure the code is ordered as of the calling sequence. The caller before the callee.
  • Avoid too long files. 1000–2000 lines are okay.
  • Follow proper indentation across code files.

Exception Handling

The code contains a case where the interface gets empty data so that it doesn’t cause an error but presents other text.

Unit Tests

  • Don’t write production code until you have written a failing unit test
  • You may not write more of a unit test than is sufficient to fail, and not compiling is failing.
  • Don’t write more production code than what is sufficient to pass the failing test.

Here’s an example:

These tests are used to check the copy of a page’s user interface. As the law says, write the failing code first before you write the implementation. So you wrote these test and push to the repository to get the failing result (red), and then you can start writing the implementation code to get green result.

More explanation about this will be on the Test-Driven Development topic article later, stay tune!

It may take some time to get used to getting clean code applied to your code. But start doing it! It’ll make your code better and more maintainable, and also make you a good programmer😉

See you on another episode!

Reference

How to Write Clean Code?| Hackernoon

A Short Summary On Clean Coding Best Practices

Computer Science, University of Indonesia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store