DEV Community

Evan Shareef
Evan Shareef

Posted on

How I reduced code size by about 85%

Background Story
I worked on transforming a legacy desktop application into a system with centralized server-based data storage. This involved changing the existing measurement data storage system and implementing comprehensive safety and validation checks to enhance data integrity and security in the new environment.

Problem
There were many steps involved in each data synchronization process. It required some of the API calls, some verification steps, and so on. Here's the old code snippet:

Image description

The code looks messy, Right? I had the same feeling when I saw my code. The repeated checks to see if the previous process succeeded made me realize something was wrong. I find myself doing the same thing repeatedly, and I know it will be repeated everywhere else. Is there a solution for this?

Approach to Solution
The first thing that needs improvement is the repetitive if/else ladder. Specifically, the additional error checking after each step makes the code larger and harder to follow. Anyone reading the whole method needs to understand what's going on, and those if/else conditions would distract them. It's also cumbersome for the coder (me) to repeatedly write out those checks.

What if I could pass each step to a method that would check if the step is successful or not, and then decide whether to proceed to the next step? This means I need a mechanism that can receive each step and make a decision about whether to execute the next steps or not.

I can create a method that receives the previous result and the next step. It can then decide, based on the previous result, whether the next step needs to be executed or not. If the previous result indicates failure, the method won't execute the received step; instead, it will return the error result. This way, the error message can propagate without executing any further steps. There are two paths for my code to take: it can either go to the error line or the success line, but the code will be executed as expected. So for my convenience, I will refer to this method as "GatewayMethod" so that I can use the "GatewayMethod" keyword to clarify which thing played as this method in my codebase.

Updated Code

Image description

See the difference? I shared a simplified version of my task as a demo, but in reality, the actual codebase involves larger functions, which made it difficult for code reviewers to read. However, with this new approach, error handling has become more streamlined, and the code is easier to manage from a centralized location. In the future, if additional conditional checks need to be introduced, I can simply modify the BindAsync() method. The core method responsible for chaining all the necessary steps is BindAsync(). Below is the code snippet for BindAsync().

The Gateway Method

Image description
This approach is called Railway Oriented Programming. Your code will be executed based on the result, but it can take either of the railway lines.

Top comments (1)

Collapse
 
evan_shareef profile image
Evan Shareef

You can connect with me here:
linkedin.com/in/evan-shareef/