Introduction to Clean Coding for Testers
The omnipresence of software cannot be ignored. It is synonymous with today’s connected world. Our lives are heavily influenced by software. As a result of our industry’s rapid growth, we are creating code at a faster pace than ever before. One thing that often gets overlooked amidst all this exponential growth is the loss we are making because of poor quality & code. The purpose of this article is to introduce you to the art of “clean coding”, along with some tips and ideas for getting started.
Table Of Contents
What is Clean Code?
Clean code is the art of writing code that is easy to read and understand. It also means that the code can be debugged easily. It can be seen as a work of art, simple and written with great care and thought. A clean code has 3 major attributes:
- Elegant: Clean code always looks like it was written by someone who cares.
- Efficient: Well Organized & Arranged
- Simple: To understand. Note that simple is not easy, and easy is not simple.
The goal of clean coding might look easy to someone new to programming. However, in a race to go fast, most programmers end up taking shortcuts and technical debt,s leaving your codebase filled with non-clean code.
“It doesn’t require an awful lot of skill to write a program that a computer understands.The skill is writing programs that human understands.”Robert C. Martin
Why is Clean Coding Important?
It is a commonly observed anti-pattern in the software industry to treat testers as second-class programmers. Test code, however, is just as important as production code. A shorter time to market cannot be achieved without clean automation code. This is where clean coding comes in. The goal of clean coding is to write code that can be easily maintained and read by other programmers.
Clean coding practices are important not only for programmers but also for those who work with them. Having a good understanding of what’s going on within the code will help them ensure that it works efficiently.
The majority of engineers still see code as merely a technical tool for implementing features or fixing bugs. Code can certainly do those things, but it’s also a means of communicating with others: colleagues and your future self. The majority of our time is spent reading code rather than writing it, so we should optimize code for readability instead of focusing on how fast we can create it. The ability to write clean code is an imperative skill for both developers and testers as it helps them to develop code that is bug-free.
“Simplicity is a prerequisite for reliability.”Edsger W. Dijkstra
How to Start Writing Clean Code?
It is important to be able to use a variety of libraries and frameworks as a test engineer. Nevertheless, what is going to move you forward is learning such pillars of this profession. Clean code is what separates professional programmers from amateurs. In order to get started with clean coding as a tester, here are a few tips & strategies:
1. Writing Meaningful Variable Names: As uncle bob says, you should name a variable using the same care with which you name a first-born child. Variable names should be such that they are:
i. Intent revealing
ii. Context revealing
iii. Easy to remember
vi. Free from typos and spelling mistakes
In addition to variable naming, these guidelines apply to the names of test scripts, test suite markers, method names, etc.
2. Meaningful Commit Messages: Commit Messages are like an audit trail of your coding effort. When multiple programmers work on the same branch or repository, it is very difficult to track code changes if the commit messages are not meaningful. A meaningful commit message typically takes a few minutes to write. However, spending just a few more minutes to do your work well really pays off in the long run.
3. Write Fluent Assertions: If you have some experience with writing test script code, you would have already used normal assertions to apply checks in your script. Assertions can, however, be made more readable by being fluent. In a more readable and convenient manner, Fluent Assertions describe assertions naturally in a human-understandable way.
4. Using AAA Pattern for arranging test script code: A better way to organize test scripts is by following the AAA pattern. AAA stands for Arrange, Act, Assert.
i. Arrange: During the Arrange part, you define all the preconditions needed for your tests. You set up the input values, the mocked dependencies, and everything else needed to run the test.
ii. Act: The Act part is where you eventually call the code to perform the main action for the test.
iii. Assert: Finally, the Assert part, where you check that everything worked as expected. You can use the fluent assertions explained in the previous point here.
5. Meaningful Exception Handling (try; catch; finally): One of the biggest mistakes that programmers do is to put Exception-Handling blocks on every method leaving most catch blocks empty. It is important to provide enough meaning to determine the cause and location of an error. Throwing custom errors message and appropriate information makes code cleaner and helps to avoid checking for status values throughout the code.
6. Meaningful Documentation: Any application is as good as it’s documentation. The same applies to test applications and solutions. Most test automation projects never get extended or collaborated on because of no or very poor documentation.
Make sure to document information related to:
2. Build steps
3. Test selection markers
4. Backlog tasks, etc.
5. Script Meta Data:
i. Author name
ii. ALM test id
iii. Bug id (if blocked)
iv. Reason for skip (if skipped)
“Clean code always looks like it was written by someone who cares.”Robert C. Martin
I hope this article helped you learn more about clean coding and how to start your clean coding journey. If you want to dive deep into this topic, I recommend you to check out the popular book Clean Code.
Explore a test automation tool that lets you automate tests written in simple English.
Read more articles by QA Expert, Rahul Parwal:
A to Z of Software Testing: Terms, Tips, & Advice for Newbies
4 Most Common Myths Around Test Automation
Open Source for Testers – What, Where, Who, Why, When, and How- 5W1H