In my earlier blogpost, A way to sustainability through a clean code I wrote about how I think a good mindset would be to write clean code. In this post, I will give some rules of thumb on how you can write cleaner, more sustainable code that are less prone to needing maintenance.
I will start with three easy-to-remember focus areas that will greatly help with keeping code clean if followed. They are as follows; readability, atomizing and testing. These guidelines may seem basic and easy, but in reality, they can be a challenge even for more experienced coders.
- The most basic and important advice I can give to improve readability is to look at the naming of variables and functions. A name must describe its use so no explanation needs to be added, and anyone can easily see its use. Make no exceptions for this, no matter how a variable is used it should have a descriptive name. A function name starts with a verb describing its use, for example, get or set.
- The length of a line of code should not have too many characters so it exceeds the width of a screen. This can be very different from screen to screen and font size, so a good standard is to try not to exceed 67 characters. This is widely debated so find what suits your team the best.
- Spacing and indentation of the code will improve the legibility of your code. Having consistency in the separation of functions, variables, and statements with newlines will help the reader distinguish the different parts of the code is made up of. Gathering and sorting related variables, functions, and statements will also add more to the legibility. But also keep in mind that the code should not be too spacious either, density is also important for readability.
- Clean code should be so understandable that it should not need a comment to explain what it does. If you must write several comments to describe what is going on, then try rewriting to make it more understandable. Fewer comments will also lessen maintenance since changes in code may also need maintenance on comments. A good indication of well written code is code that does not need additional comments to be understood.
- You should think of your code as building blocks put together to make your program. To make good building blocks, functions should be small and only do one thing and do that well. Readability will increase as well as make the code easily customizable and reduce the needed maintenance.
- A function should not take more than three parameters, if more data is needed, you gather the data in an object. This will make the reading function easier since you do not need to keep track of as many parameters.
- Unit testing is a great way to know if your code is working. Strive to cover 100% of the exposed functionality in the code with unit tests. A key ingredient in writing great unit tests is readability. Do not commit code with failed or missing tests.
- Practicing test-driven development is a great routine to have when writing code. I find that this will help in considering what approach will be best to solve the given task.
To sum up there is a lot to consider when you write your code if you want to write clean and professional code. These are some areas that are easy to understand but may be hard to master. Continually working on all of this should be something every coder should keep in mind throughout their whole career.
I will also mention that there are several more areas that I have not mentioned here that will improve your coding that I probably will write a blog post on. This is my understanding of how to start writing clean and sustainable code. If you have anything you would like to add or disagree with, I would greatly appreciate a comment.
About Lars Snellingen
Lars Snellingen is one of Sogeti’s experts in technical testing with a deep knowledge in UI automation and API automation. He has been involved and fully responsible of testing multiple system transitions from on-prem to cloud in the retail industry. As a lead technical tester, Lars has experience in multiple roles such as a Test Developer, Test Analysist, Test Coordinator, managing test environments, and being a mentor. Lars is involved in the technical core team in Norway where he has had several presentations with different tools in API testing. He has been involved in a global API community where he worked with people across Sogeti globally. Lars is driven by taking on new complex challenges and has with this achieved great trust and responsibility from the customers. He is passionate to learn, develop and strongly believe in delivering quality in his work.
More on Lars Snellingen.