The Importance of Code Quality: Production vs. Test
Code quality is a topic of prime importance in any software development cycle. It influences not only code functionality but also its maintainability, scalability, and long-term viability. Unfortunately, it is often forgotten or outright ignored. As a QA Manager, it’s crucial to understand and advocate for the necessity of high-quality code – in both production and test scenarios.
Before discussing code quality, let’s differentiate between test scripts and test code.
Automated Test Scripts vs. Automated Test Code: A Brief Comparison
Automated test scripts are typically created in one of two ways: using record-and-playback tools, then converting the results into scripts, or writing scripts line by line, action by action, without reusable components. While these might be simple to create, they lack flexibility and tend to be brittle, making them susceptible to breaking when the software changes, potentially leading to regression gaps as scripts are abandoned. Automated test scripts are most suitable for simple, static workflows.
Conversely, automated test code involves writing code to perform automated tests, ranging from unit to end-to-end tests. This approach requires a higher level of programming skills, such as knowing object-oriented principles, page object models, or how to use facades and factories. But they offer superior flexibility, robustness, and efficiency. Moreover, the focus is on software functionality rather than just the user interface or unit test, making it ideal for complex projects or those with longer lifecycles.
Why Does Code Quality Matter?
Code quality is the cornerstone of any reliable, robust, and efficient software system. Poor code quality can lead to bugs, security vulnerabilities, performance issues, and even system failures. On the other hand, high-quality code addresses these concerns while being easier to read, maintain, and extend, reducing the time and resources needed for debugging and shortening release cycles. High-quality code is good.
I Have High-Quality Production Code; Why Should I Care About Test Code Quality?
The urgency to focus on production code quality often overshadows test code quality. However, it’s vital to remember that test code validates production code, and testing is your primary defense against software bugs and defects, not code quality. Therefore, if your test code is ambiguous, poorly structured, or not thorough enough, it may not effectively test your system. Worse yet, poor testing may produce false positives/negatives, resulting in unjustified confidence. In either case, production issues are likely.
On the other hand, effective testing helps identify issues within production code regardless of its code quality. Tests do not care about production code quality so long as they work, and a poorly written system can work as expected if well tested.
Ultimately, testing is a matter of trust, and it’s easier to trust something of higher quality. Since testing is like an insurance policy for your software, you will want to insure it for as long as you use it. And having the best insurance policy means one of high quality, including code quality,
What’s More Important, Production Code Quality Or Test Code Quality?
The answer should be both, but if you can only choose one, which would you choose? Would it surprise you that I chose test code quality? Unfortunately, it’s rare for companies to invest time and money into code quality, so I see poorly written production code all the time. But, even if they did invest in code quality, the best-written production code would still have bugs; it’s just the nature of development.
But effective testing can ensure your system works as expected, no matter how poorly it might be coded. It might take days to fix a minor problem with garbage code, but when it is fixed, as proven by testing, effective testing will also provide the confidence that nothing is regressed.
Given the opportunity, I choose high-quality for both. But having to choose only one, you will understand why I think high-quality test code is more important than high-quality production code.
Summary
This article underscores the importance of code quality in software development and testing. It shows how code quality can shape a software’s functionality, maintainability, and long-term sustainability. Distinguishing between automated test scripts and test code, it accentuates the latter’s superiority in flexibility and robustness. Then, in a surprising twist, we learn that placing a higher emphasis on test code quality can compensate for deficiencies in production code quality while protecting against bugs. Are you ready to challenge your views and see test code quality in a new light?
Kim Filiatrault
Founder and President
Stay tuned for our next article this coming Monday, “High Quality Code with Ankrpt HealthCheck”, to see how we address code quality analysis.