Part 3: Ensuring Code Quality (with examples)
Tags: testing, programming, beginners, writing
Published: 2023-01-13 21:10:19

Unit testing not only helps to catch bugs early in the development process, but it also provides a way to ensure that the code works as intended and that new changes don’t break existing functionality. When a developer writes a unit test, they are essentially creating a set of instructions that verifies the functionality of a specific unit of code. By running these tests every time a change is made, developers can be confident that their code is working as expected.

For example, consider a function that calculates the factorial of a number. A developer writes a test for this function by passing in a number, and then checks that the function returns the expected output. If the developer later changes the function to calculate the square of the number instead of the factorial, the test would fail, alerting the developer of the mistake. This helps to prevent bugs from being introduced and ensures that the code works as intended.

Unit testing also helps to improve the design of the code. By writing tests for each unit of the code, developers are forced to think about how the code is organized and how it can be made more modular and testable. This can lead to a better overall design and a more maintainable codebase.

For example, consider a function that performs a complex calculation. The developer can write multiple unit tests for different scenarios, this way it can help to identify any part of the code that can be refactored, making it more readable and understandable, and also help to identify any dependency that the function has on other parts of the code. By identifying and eliminating these dependencies, the code can become more modular and easier to maintain.

Unit testing also provides a way for developers to refactor their code with confidence. Refactoring is the process of changing the design of the code without changing its functionality. By having a suite of unit tests, developers can make changes to the codebase and know that if the tests still pass, the functionality of the code has not been affected.

For example, consider a function that performs a specific task, but it’s too complex and difficult to understand. A developer can use the unit tests to refactor the function, breaking it down into smaller and more manageable parts, while ensuring that the functionality remains the same.

---
Example Use Case:

Consider a software application that manages patient records for a hospital. The development team is tasked with building a feature that allows doctors to view and edit patient records. The team decides to implement unit testing as part of the development process to ensure that the feature is working as intended and that the code is of high quality.

The team writes several unit tests that cover different scenarios, such as adding a new patient, editing the information of an existing patient, and searching for a patient by name. During development, the team runs these tests frequently to ensure that the code is working as intended.

As the development progresses, the team makes several changes to the code, but thanks to the unit tests, they can be confident that the changes haven’t introduced any bugs. Once the feature is complete, the team runs the tests one final time, and all the tests pass.

The hospital launches the feature, and it functions as expected, allowing doctors to view and edit patient records quickly and easily. Thanks to unit testing, the team was able to catch and fix issues early in the development process, which saved time and resources in the long run and ensured that the code is of high quality.

---


In summary, unit testing provides a way to ensure that the code works as intended, that new changes don’t break existing functionality, and to improve the design of the code. By running unit tests every time a change is made, developers can be confident that their code is working as expected. Unit testing also provides a way for developers to refactor their code with confidence, by making changes to the codebase and knowing that if the tests still pass, the functionality of the code has not been affected.

Series: Why unit testing is important ? All about unit testing

[Part 1: Introduction to Unit Testing](https://dev.to/svenherr/part-1-introduction-to-unit-testing-with-examples-21k2)

[Part 2: Catching Bugs Early](https://dev.to/svenherr/part-2-catching-bugs-early-b3f)

[Part 3: Ensuring Code Quality](https://dev.to/svenherr/part-3-ensuring-code-quality-with-examples-3p3n)

[Part 4: Living Documentation](https://dev.to/svenherr/part-4-living-documentation-with-examples-chk)

[Part 5: Continuous Integration and Deployment](https://dev.to/svenherr/part-5-continuous-integration-and-deployment-with-examples-2hkg)