Clean Code: Explanation, Benefits, and Examples
Clean Code: Explanation, Benefits, and Examples
To paraphrase Martin Fowler, good code isn't just readable by a machine, but by humans as well. Read on for some advice on sticking to clean principles.
Join the DZone community and get the full member experience.Join For Free
See why over 50,000 companies trust Jira Software to plan, track, release, and report great software faster than ever before. Try the #1 software development tool used by agile teams.
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 on 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 and of building the software project right from the beginning.
What Is Clean Code?
I want to start this article 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 explains the essence of clean coding.
When we talk about clean code, we talk about a 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” has become 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 us precious time. It's the same in software development and 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 in to focus on the design of your code, on architecture. 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: the 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 the 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 a 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: the foundation and architecture are key! In the long term, it will save you time and money on “redoing” work.
8 Reasons Why Clean Code Matters
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 use namespaces, classes, and methods instead of words.
2. Best Practices
In recent years, software best practices like unit testing, TDD, CI, etc. have been 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 testing. 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.” 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 the majority of your time and the cost of maintaining code.
5. Easy to Test
By building clean code, automated testing of that code is encouraged. By automated testing, I mean Test-Driven Development - which is the most effective way to improve the quality of 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.
At Apiumhub, we love the KISS principle (keep it simple, stupid), as well as the DRY principle, which means don’t repeat yourself. It allows software developers to avoid duplication and allows them to produce much cleaner code compared to the programmer who uses unnecessary repetition.
And don’t add extra features because you might need them 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 in 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 the 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 the names of the items. Having a proper naming convention is extremely important in code for future edits. Having irrelevant or contradicting names for your pages, variables, functions or arrays will only create trouble for you in the future. Therefore, naming elements on the basis of what they are is a common rule helps a lot. It creates consistency and makes it easier to come back and work on the project at a later time.
Actually, this leads us to the next step for clean code – creating a 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 the 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 you shouldn't be afraid to defend your project and 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! 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:
Published at DZone with permission of Ekaterina Novoseltseva . See the original article here.
Opinions expressed by DZone contributors are their own.