If you are a software developer, who already knows how to use for-loops and switch statements, and how to create different structures of data or use algorithms, then you have the fundamentals to be a good developer. However, to be a great one you need to follow the clean code principles.
Once you land your first programming job, you’ll be a part of big and complex projects, that might have been in development for years. So, writing clean code will allow you to leave your footprint by adding new mission-critical features while still avoiding messing up your team member’s code.
WHAT is a clean code?
The term first appeared in the book Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin originally published in 2008. From that moment on, it created the trend for writing better code. Many years later, there is no definitive answer to this question, but we can all agree that clean code should be easy to read, understand, and enhance by a developer other than its original author. It should have meaningful names, that make it look more like a good story rather than a boring maths schoolbook. Additionally, clean code should be organized in such a manner that provides only one way of doing something. And last but not least, it should have unit tests, which verify that each method can solve the problem for which it was written.
why does writing clean code matter?
Unlike any school or university assignment where most of the time you are working on your own, at work you are going to be a part of a team. This team might have been working on the project for years before you join and continue to work on it after you leave.
The most important thing to understand is that you are not writing code for yourself. You are writing code for your existing and future colleagues. So the best thing you can do is make their lives easier by producing readable and maintainable code.
This would allow you to hand broken code to a team member, and they will be able to understand it and fix it easily. Humans, unlike machines, can give recommendations only to things they can comprehend, which in this case is code broken into small and simple routines.
As your career progresses you will notice that adding new features to the existing code base can be risky if you can’t fully understand all of its quirks and perks. So, when you introduce new changes, you risk creating new bugs as well. This usually happens when no one cares enough about producing quality code.
clean code principles
Your motto should be: Write readable code for people! Working on a software project feels more like participating in a marathon, rather than a sprint event at a computer science competition. In real life saving the time to type “p1”, instead of “person”, works in the opposite way and can harm the long-term performance of the project. You no longer write code solely to satisfy a compiler or interpreter, but to create a good code “story”. That way even if you come back to it 6 or 12 months later, it will take you seconds to understand what’s going on.
Good naming saves time. When you are stuck on a task, and you ask your mentor for help, if they notice that you have tried to add “p2” to “c1” they will be totally puzzled. But if they spot that you are adding a “person” to a “contract”, they will immediately figure out that there is a business rule that doesn’t allow the operation. Thus, you shouldn’t try to rework the whole project to make it possible.
Short names like “p2” and “c1” will start to eat up your time in the long term, as they are not easy to locate across a given class. You will remember “p2” and “c1”, but when you search the class for them you might find a lot of occurrences, instead of navigating right away to the place you need. And it’s going to be a shame to waste your time for coffee or beer just because of bad naming.
You want to keep your code small and well organized. Think of the code base as a TikTok or Instagram reel, which is generally shorter than 60 seconds and has a clear goal. You can achieve this by following the SOLID principles. They might seem rather abstract and vague, but once you start writing you will be able to quickly spot if you are doing something “wrong”. For example, when you implement a given interface and leave a few of the methods throwing NotImplementedException, then you should remember the Interface Segregation Principle and refactor your code.
This leads us to the next important habit you should develop. Remember that your goal isn’t simply to get the program to work and then move on to the next task, leaving the program in whatever state you finally got it to “work”. This means that you shouldn’t leave unused functions, TODOs, or commented-out parts of the code. When you are ready to create a Pull Request make sure that you remove everything redundant and keep only the parts that matter. Don’t bother thinking about whether you are going to need something in the future or not. Nowadays we all use some sort of version control system like Git, so you can track the history of each file.
Also, it’s a good idea to start using certain design patterns that will help you build clean and maintainable code. The one you can focus on from the very beginning is the Template method, used to eliminate code duplication. When writing you will notice that different classes and modules have similar behavior, which can be defined on an abstract level, leaving the subclasses to override the given implementation. When those classes share duplicated code then once again, we can move this logic to the abstract level. Don’t go too wild with this approach and keep it focused on parts of the code that are doing similar things such as importing data from files with different formats.
Unless you enjoy reading boring documentation, the best way to understand what a software project is supposed to do is to read the unit tests. They are the guardians of each project and your best friend when it comes to refactoring or adding new features.
The benefit of the tests is that they will save you a lot of time when working on a larger project. Imagine a case where you make a small change to the codebase, and you want to verify that the whole project is still working. So, try to start it. If the project consists of a few microservices and a separate UI project, you are going to wait 10 to 15 minutes to get everything running, just to find that you forgot to add a null check. Instead, you can spend some time writing tests, that will help you verify if your code changes are correct. What’s even better, they would stay there as long as the project lives.
This way unit tests provide their value in the long term. When being part of a development project, often you must add new functionality to already existing code. Whilst adding some extra lines is a piece of cake, there is always a chance to alter the system behavior in an unpredictable direction, and to introduce new bugs. The only way to make sure that you are not violating any preset business rules is by passing unit tests.
Just by glancing at your tests, you should be able to tell if the new features you are working on will create a conflict with the existing business logic, or if the management should rethink their vision of the project.
WHAT can you do to improve your development skills?
- Do some pair programming with a more skilled developer. This will allow you to better understand the project you are working on and to deliver a solution to your tasks much faster. Another benefit is that you see how other people are tackling tasks and improve your problem-solving skills.
- Make code reviews, so you can see how other people are writing code. If you can understand it, without reading the requirements, probably it’s a good solution. By making code reviews, you will be able to spread your knowledge about clean code and give valuable suggestions to your team members.
- Keep learning and challenge your knowledge. There is no definitive answer to any problem. If a given architecture or framework is the industry standard today, in 5 years it might become the worst way to build an application.
5 POPULAR PROGRAMMING LANGUAGES TO LEARN IN 2022
be a good boy scout
Clean code is code that has been taken care of. You should take the time to keep it simple and orderly. When you pay the appropriate attention to all details, you will notice how your code will simply get better as time goes by. This will happen as the desire for continuous improvement is an intrinsic characteristic of the best professionals.
If you are interested in mastering the principles behind writing clean code and developing your career as a Software Consultant, make sure to also check out our open positions.