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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️