Let's start by defining what's clean code?. This definition can get very particular and specific depending on the language you're using for developing and the assumptions of the system. But basically, I would say that clean code is code that is easy to understand and easy to use for the environment that it was created for. Just like 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.”
Sound pretty straight forward isn't ? But truth is that it takes time and effort to have clean code, and another important thing to notice, is that code is really the core of any system, and that it almost always is reused by other developers, so it's important to keep it clean, besides, having bad code lead to terrible outcomes that will be explained later. Extending a little the defition, this is what I mean with code easy to understand:
- Relationships between projects are easy to understand
- The methods are easy to understand and they don't need a lot of additional comments more than their name and the context to make sense of the usage
- The flow of the system is easy to understand and you can guess it by reading the code
- The names of the variables make sense and it can give you context of where they belong and their usage
On the other hand, this is what it means to have a code that is easy to use:
- Classes and methods are small and they're in charge of only one thing
- There are API for projects and that's the only way to communicate between other sub-systems
- Testing is easy and there are interfaces for important classes
- At least 80% of the code has unit tests, and there are integration tests for all the features
- There is no need to have a big set of documentation for the project. The methods, classes and structure of the projects, make it easy to understand
Something to really keep in mind are names of the classes and methods. They are very important because business people also talk about them, so you are very likely to have smart conversations using names of variables / classes / projects. Besides, new developers might be re factoring or adding new features to the code, and they will have a big "?" on their minds if the names are "encoded" in a weird language. You don't want to look like him, right?
The cost of not taking care of the code is really high. We're talking about:
- Productivity
- Efficiency
- Assertiveness
Another entry comes to an end. I hope you're enjoying the series, let me know if you want me to talk about some particular subject on software development, I will be happy to do so. By the way, I really recommend to read Robert C. Martin book: Clean Code: A Handbook of Agile Software Craftsmanship . I'm sure that it will help you in creating better code.
No comments:
Post a Comment