Good Code is Testable Code

0

Do you know what makes code good and testable? In software development, the two key concepts are “good code” and “testable code.” Understanding and applying these can significantly enhance our development efficiency and improve software quality.

pixabay

Definition and Importance of Testability

First, what is testability? The definition of software testability can be found in various sources. Generally, it is defined as “how easy it is to test a system” or “to what extent the code can be tested.” However, these definitions can be subjective.

I would like to define testability as “a characteristic of software that explains the relationship between the complexity of the code and the complexity of the test setup.” Testability depends on how the code and test setup complexity interact. You cannot judge testability by the code alone; it must always be evaluated based on the relationship with its complexity.

The Relationship Between Code Complexity and Testability

Understanding the relationship between code complexity and test setup complexity is essential. For example, let’s look at the two functions below.

export function add(a: number, b: number) {
  return a + b;
}

export async function updatePost(postId: string, payload: Post) {
  const existingPost = await db.posts.findFirst({ id: postId });

  if (!existingPost) {
    throw new InputError(`Cannot find post with id "${postId}"`);
  }

  const nextPost = await existingPost.update(payload);
  return nextPost;
}

Objectively, the add function is less complex than the updatePost function. However, lower complexity does not necessarily mean higher testability. Each function should be designed to fulfill its purpose, and complexity can be justified if necessary.

The Importance of Test Setup

Even complex code can be easy to test with well-designed test setups. Conversely, even simple code can be difficult to test if the test setup is complex. For example, let’s look at the isLegacyUser function below.

export async function isLegacyUser(userId: string) {
  const user = await db.users.findFirst({ id: userId });
  return user?.type === 'legacy';
}

This function seems simple on the surface, but its dependency on the database can complicate the test setup. This complexity illustrates the mismatch between the code’s intent and the test setup’s complexity.

Good Code and Testability

Good code is always testable code. To improve code quality, aim to write good code. However, writing good code alone is not enough. Testability is a characteristic that must be continuously observed and managed.

It is also important to invest in simplifying the test setup by providing utility functions that automate repetitive tasks. These efforts significantly reduce the complexity of the test setup and ensure better testability.

Conclusion

Good code and testable code are crucial elements in software development. Understanding and applying these two aspects can lead to better software development. Managing code complexity and test setup complexity appropriately is key to enhancing testability. Refer to this guide and start writing better code today!

Reference: Epic Web Dev, “Good Code, Testable Code”

Leave a Reply