Write code that is easy to understand and maintain

Tram Ho

Introduce

Good code is not just the code that solves the problem with high performance, it must also be a code that can be read and understood as natural language. Based on that, people who do not write down these lines of code or even those without programming knowledge can understand what their task is.

So let’s look at the following code:

This code is used to filter out even numbers, and it also looks pretty easy to understand. However, the problem is only more complicated when we add some filtering conditions, such as:

It also looks cumbersome, right? If you adjust it as follows to look clearer:

It unintentionally increased the number of iterations more than before, because now it has to filter again two more times from the array of results of the first filter. Check out the results below:

Case Study

Let’s dig deeper with the following example, we created a struct Person that defines properties like name, eye color, hair color. And the enum contains the following eye and hair color:

We can easily filter out people with certain eye colors and hair colors by combining the filter conditions:

But it is a little hard to read and maintain, so we will revise the code above to make it more readable and maintainable.

Filter

Let’s wrap the filter condition into an object as follows:

The struct Filter will now contain filtering conditions for the generic element, so that we can use it for many different objects without having to create other struct. Then extend the Array to use the Filter:

We can add matching and non-matching components to the given conditions, and the results will include both types of output:

And we wrap the components in a closure, so that the filter will only be done when it is called, we will adjust the Array extension as follows:

Now we can write:

let subset = people.filter { $0.eyesColor == .blue }

Fort:

It looks neater and easier to read than right, but what if we need more conditions to filter?

We have a performance problem again, as we mentioned at the beginning of this article. However we will fix it in the next section below, let’s continue reading offline.

Add additional functions

To support the combination of filter conditions, we can create additional functions for it, these are the basic functions for operators like and, or.

We can rewrite the above code using the following new operators:

let subset = people.filtering(hasBlueEyes.and(hasBlondeHair)).matching

Even more functions can be created:

Combining filter conditions will now be very easy, we can write it as follows:

Final Result

To create reusable filters, we can extend the Filter and add the following common cases:

Let’s return to the code from the beginning that we want to refactor:

It was rewritten to:

Filter even more complex cases:

Or filter both Dog …

Conclude

For programmers, coding doesn’t just stop at solving problems, it has to be easy to read, understand, and maintain, and it can be read as a natural language. Using generics is also one of the ways we can easily reuse and maintain. In the process of creating an application, writing better code is an interesting challenge that programmers must always try to develop.

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo