Every year, a tremendous amount of time and significant resources are lost because of poorly written code. Developers very often rush because they feel pressure from their managers or from the client to get the job done quickly, sometimes even sacrificing the quality. This is a big issue nowadays and therefore I decided to write an article about clean code, where I want to show all the benefits of clean coding, of building the software project right from the beginning.
This article I want to start with a very good quote: “Any fool can write code that a computer can understand. Good programmers write code that humans can understand. – Martin Fowler. This quote very good explains the essence of clean coding.
When we talk about clean code, we talk about reader-focused development style that produces software that’s easy to write, read and maintain. Clean code is code that is easy to understand and easy to change.
The word “clean became very trendy nowadays, if you look at design, photography, etc. people go for clean things, because nowadays our life is extremely complicated and we want to choose clean and clear options, because it calms us down and saves our precious time. Same in software development & architecture, if you have more code than you need, it shouldn’t be there, there shouldn’t be anything extra.
Your code should be as efficient, readable, and maintainable as possible, and instead of only solving the problem, you should always put a bit of extra time to also focus on the design of your code, on architecture. Because your code should be understandable, should be clean. This means the code is easy to read, whether that reader is the original author of the code or somebody else. There shouldn’t be doubts and misunderstandings. For example, the following should be clear: execution flow of the application, how different objects collaborate with each other, the role and responsibility of each class, each method purpose, purpose of each expression and variable, etc.
Also, it is extremely important to have this ability to easily extend and refactor your code. This can be achieved if the person making the changes understands the code and also feels confident that the changes introduced in the code do not break any existing functionality. For the code to be easy to change, you need to be sure that you took into account that: classes and methods are small and only have single responsibility, that classes have clear and concise public APIs, classes and methods are predictable and work as expected, the code is easily testable and has unit tests, that tests are easy to understand and easy to change, etc.
What is important to keep in mind, is that a clean coder makes sure he fully understands the problem before beginning to code. It is just like building a house, foundation and architecture is key! In the long term it will save you time and money on “redoing works.
It’s easy to forget that each line of code software developers write is likely to be read many times by humans during its lifetime. These humans are usually co-workers. They’re busy fixing bugs and adding features. Therefore each developer should take care of the code and make it as clean and clear as possible. Developers are like authors, great authors are known for writing books that tell a clear, compelling story. They use chapters, headings, and paragraphs to clearly organize their thoughts and painlessly guide their reader. Developers work in a very similar system, but simply use different words of namespaces, classes, and methods.
2. Best practices
In recent years software best practices like unit testing, TDD, CI, etc. are growing very fast in terms of adoption. These practices elevate code quality and maintainability. Implementing clean code principles is a foundational skill that pays off especially well when it’s time to refactor code or bring code under test. Clean code makes it easier to read and test. If you think of it as part of a house, clean code is the foundation.
3. Logic behind the code
If someone asks you about your code quality, you should provide a rational justification. If you’ve never methodically considered the quality of your coding style, there’s likely plenty of opportunity for improvement. Those who write clean code have concrete activities, patterns and techniques they use to keep their code clean.
Writing code is relatively easy, reading is hard. This is why so many developers prefer to rewrite rather than do the hard work of reading and comprehending existing code. By writing code that is readable, you are optimizing for the 90% of the time we are reading code, rather than the 10% of the time you are writing it. This is a significantly more cost-effective strategy than the alternative strategy of writing code as quickly as possible without concern for the readability of the code. Also, it makes it almost impossible to say “Oh, this code is not mine, it is Juan’s one. With clean code you won’t need to blame others for the poor quality of the code, clean code is a standard, a foundation for everyone to work on. So, at the end of the day, by creating code that is maintainable, you are optimizing for the 70% of the time and cost of maintaining code rather than the 30% of the time and cost of creating code.
5. Easy to Test
By building clean code, automated testing of that code is encouraged. By automated testing I mean Test-Driven Development- most effective way to improve the quality of the code, improve the long-term velocity of a team, and reduce the number of software defects. All of these factors contribute heavily to the overall ROI of the software.
Keep your code as simple and readable as possible. Don’t over-complicate problems, which is a common issue among software developers. By keeping it simple you can produce higher quality code, solve problems faster and work better in groups.
In Apiumhub, we love KISS principle; keep it simple, stupid, as well as DRY one, which means don’t repeat yourself. It allows software developers to avoid duplication and allows produce much cleaner code compared to the programmer who uses unnecessary repetition.
And don’t add extra features because you might need it in the future. Never do that. It’s a useless waste of time and money. And it’s actually harmful. When you over-complicate the code by adding extra features, you are making the code harder to read, understand, maintain, and test. By doing this, you’re creating bugs into your code. And you don’t know the future, and nine out of ten times your assumption will be wrong. Even if you were right that a feature would be necessary later, it might only be needed two years from now, and by then, you might have found a better way to do it. Focus on MVP.
Imagine you go to a shop and there is no consistency over how the items are placed in the area. It would be hard to find products you are searching for. Indentation in the code is much like the arrangement that you need in a supermarket. When your code is indented, it becomes more readable and easier to find what you’re looking for. Especially when you pay attention to names of the items. Having a proper naming convention is extremely important in a code for future edits. Having irrelevant or contradicting names to your pages, variables, functions or arrays will only create troubles for you in the future. Therefore, naming elements on the basis of what they are setting a common rule helps a lot. It creates consistency and easiness to keep working on the project. Actually this leads us to the next step for clean code – creating common language, or a “ubiquitous language, if you follow the ideas of Domain Driven Design. It seems obvious, but unfortunately many developers skip this part. So, once again I would like to repeat that wording of code is very important because you want your variable names, class names, and package names to make sense no matter who is looking at the code.
8. Cost savings
By doing clean code, you gain all those advantages listed above, and all of them lead to cost savings.
As a conclusion, I would like to say that don’t be afraid to defend your project, your code. Build quality, working software and take as much time as you need.
Many managers defend the schedule and requirements with passion, but that’s their job. It’s your job to defend your clean code with equal passion! Because it helps to increase the overall value, and reduce the overall cost, of both creating and maintaining software. It does this by focusing on creating reader-centric code that is simple, readable, understandable, testable and maintainable.
If you are interested in knowing more about clean code, I recommend you to read these 2 books, written by Robert C. Martin:
- Clean Code: A Handbook of Agile Software Craftsmanship
- Clean Architecture: A Craftsman’s Guide to Software Structure and Design
If you are interested in best practices in software development, I highly recommend you to subscribe to our monthly newsletter here.
This post was originally published on apiumhub.com