DEV Community

Fagner Brack
Fagner Brack

Posted on • Originally published at fagnerbrack.com on

Book Summary: Clean Code — A Handbook of Agile Software Craftsmanship

Book Summary: Clean Code — A Handbook of Agile Software Craftsmanship

by Robert C. Martin

“Clean Code: A Handbook of Agile Software Craftsmanship” by Robert C. Martin (who is also the creator of SOLID) is a guide to writing high-quality, maintainable code that is easy to understand and modify. The book provides practical advice, best practices, and principles to help software developers write clean, efficient code that stands the test of time.

The most valuable points from the book are:

Meaningful Names

Choose names for variables, functions, classes, and modules that are clear, descriptive, and reveal their intent. For example, instead of using a vague name like “d” or “result,” use a more descriptive name like “daysSinceCreation” or “formattedResult”. Also, instead of “x” and “y” for coordinates, use “latitude” and “longitude.”

Functions Should Do One Thing

Each function should have a single responsibility and do it well. This makes the code easier to understand, test, and maintain. For example, a function called “parseAndSaveData” should be split into two separate functions: one for parsing the data and another for saving it. Also, a function that calculates the total price and applies a discount should be divided into two separate functions.

Short Functions

Keep functions short and focused, ideally no longer than a screenful of code. This makes them easier to read, understand, and test. For example, break down a complex function that processes and validates user input into smaller, more focused functions that each handle a specific part of the process. Also, divide a long function that calculates multiple statistics into smaller functions that each compute a single statistic.

Clean/Clear Comments

Write clear, concise comments that explain the purpose and functionality of the code. Avoid redundant or outdated comments that can be misleading or confusing. For example, instead of writing a comment like “// calculate sum,” write a more meaningful comment like “// compute the sum of all positive integers in the array.” Also, replace comments like “// fix later” with more specific explanations of the issue and potential solutions.

Proper Error Handling

Handle errors gracefully and provide useful error messages to help users and developers diagnose and fix problems. For example, instead of returning a generic error code when an error occurs, throw a descriptive exception that explains the cause of the error, such as “FileNotFoundException.”

Code Formatting and Organization

Follow consistent coding conventions, such as indentation, spacing, and capitalization, to make the code more readable and maintainable. Organize the code logically, grouping related functions and classes together. For example, place data access functions in a separate module or class instead of mixing them with user interface code. Also, separate business logic from presentation logic in your application.

Test-Driven Development

Write unit tests for your code to ensure it works as expected and is resilient to future changes. Test-driven development (TDD) involves writing the tests before the actual code in a way the tests drive the development of the code, which helps ensure that your code is both correct and maintainable. For example, write tests for a string manipulation function before implementing the function itself. Also, write tests for edge cases and potential errors to ensure the code handles them correctly.

See also the Transformation Priority Premise.

Refactoring

Regularly review and improve your code to make it more efficient, readable, and maintainable. Refactoring involves making small, incremental changes to the code without altering its functionality. For example, replace a series of nested if-else statements with a more readable switch-case statement. Also, remove duplicate code by extracting common functionality into reusable functions or classes.

By following the principles and practices outlined in “Clean Code,” developers can create software that is easier to read, understand, maintain, and enhance. This leads to higher-quality applications, fewer bugs, and more efficient development processes, ultimately benefiting both the development team and the end-users of the software. By incorporating these best practices into your daily coding routines, you’ll contribute to a more collaborative, efficient, and enjoyable software development environment.

Thanks for reading. If you have feedback, contact me on Twitter, LinkedIn or Github.

Top comments (0)