DEV Community

Sumant
Sumant

Posted on

Clean Code Book

The book, clean code by Robert Martin is always listed in the "list of books every developer must read". This book was first published in 2008. That is 14 years ago from the date of this post.

As the name suggests, the book covers different aspects of code quality along with dos and don'ts. While most code is in Java, I believe the lessons apply to most languages used by programmers. In short, the code you write must be understood by others, just like you write a letter/blog/tweet that makes sense of the context.

I will go over some of the chapters present and hope to cover them in brief.

The first chapter starts with the definition of clean code and defines what is bad code. Bad code is something that you write and it makes no sense after some time along with being a mess. Mess here means code does not follow standards. The standards are what form the other chapters in the book.

Names for variables, methods, etc. in any application code is the most important part. They give meaning to the objects in the code. Naming conventions in different languages may differ but they must be meaningful. We cannot have something like "s1" or "getMon()", which only the developers at the moment understand. What happens when another developer joins and is supposed to understand the code. Domain-specific names are best as they can be quickly grasped.

Functions are the components that drive any application. They need to be precise and concise as well. The name of the function must be clearly defined. Most importantly, the functions must perform one function. Meaning, a function must not do something else, if doing so will introduce side effects. Imaging a function called saveToDb() which saves data, make sure to add functionality that only saves the data and does not transform it in any way. This is something we must avoid.

Comments are what give meaning to the code. Conveying the intent of code, letting others know what exact functionality is being done is what comments are used for. Now, it's up to the programmer to write appropriate comments, not only for himself but for the benefit of others. Always leave a comment which makes sense in the domain of the code and puns must be avoided at all costs.

With lots of IDEs and Code Editors available, formatting is one click away. But why exactly is formatting needed? To make the code readable and easy to understand of course. Proper alignment of functions/methods, classes, interfaces, etc. makes the reading of code more visually appealing than a mess of code jumbled up.

Most languages are object-oriented and emphasize objects. Taking into account Java programming language, objects are what drive an application. We need to follow the concept of data abstraction which means hiding the essential data. In Java terms, making them private. Making use of interfaces to expose limited functionality is also one of the ways to achieve abstraction.

Errors are what make an application perfect eventually. Errors are bound to happen while developing the application and the programmer must handle them. Checked and Unchecked exceptions need to be handled properly at each place where the exception might be thrown. Try catch blocks are useful in this scenario. As programmers, we need to check for null values before we pass them to methods, which may lead to NullPointerExceptions which can cause the whole application to halt in some cases.

Third-party libraries make a good amount of contribution to the overall application we intend to develop. These libraries add functionality that makes it easier for the programmer to quickly start using various methods instead of creating them. Now with the added complexity of libraries, it's important to understand the boundaries. Meaning that these libraries come into the picture and testing them along with the application. Sometimes, the application is fine but the library being used may produce undesired results. Careful planning is needed to find suitable libraries.

Next, we come to the most important part of SDLC. Tests. Testing can be for each class as in Java or a particular behavior, it cannot be missed. A whole paradigm exists called TTD (Test Driven Development) which aims to put test cases first then write the actual code. Every method in every class must be considered for testing to achieve good test coverage.

Using OOPs concepts which is the primary way to use Java, Classes are the main components in any application. Class must follow the Single Responsibility Principle, which means that a class does one thing alone. This also means that a class is responsible for the single activity of a domain. A class needs to be small and contain enough information to make sense in the application. The well-organized class makes the code or application look and perform better.

Threads and multithreading are topics that deserve a different post altogether. In all, thread management is essential and care must be taken when writing code that involves threads.

Systems interact with other system and we need to make sure it happens without any errors or issues which stops the whole flow. The programmer needs to address this in form of system design.
There are other topics in a book like efficient system design which cannot be explained in brief. These are more important when having a holistic view of application development.

Clean code is essential for the overall performance of applications right from a single class to connecting to other systems. There is a lot to understand for this book and I have only outlined things that are common in day-to-day tasks for programmers.

Top comments (0)