Logical aggregation of code refactoring

Source: Internet
Author: User

In the previous article, I wrote a refactoring data structure, which is an example of the aggregation. One of the previous experiences was to refactor a long function, presumably to gather the relevant logic together. I call it logical aggregation. This is a process of refactoring the code logic.

The story is like this. In my hand is a function consisting of hundreds of lines of code. The program seems to have a relatively clear block of code on the whole, some of which are similar, but with different details. Have tried to modify this code. However, because some local variables run through the entire function, they work in different places, and different blocks of code interact with each other. Therefore, it is difficult to modify the logic of this function, a small change will be changed several places. So, just want to focus on this code. The immediate goal is to shorten the function to dozens of lines and make the function easier to understand.

First, several seemingly similar blocks of code are extracted and abstracted into a single function. For the different parts of these blocks, they are represented as variables, as arguments to the function. It is important to note that, during the modification process, the variables that function only in the code block are used as local variables, and the arguments passed by the preceding code are introduced as function arguments, and the code block is modified, and subsequent code is useful as a function return parameter.

Second, the other logical relatively independent blocks of code are extracted and abstracted into separate functions. Also pay attention to the above mentioned matters.

Best of all, it's important to note that each extracted function has a straightforward name.

After this modification, I was amazed to find that the whole function became very short and easy to understand and intuitive. The whole function might look something like this.

1 voidProcess_data (int* Data,intLenintflag) {2 parse_data (data, Len);3   if(flag) {4 transform_data_with_method_1 (data, Len);5}Else {6 transform_data_with_method_2 (data, Len);7   }8 compress_data (data, Len);9}

And the local variables that were mentioned earlier throughout the function are eliminated. It is much easier to try to make some changes to the code at this time.


Finally, summarize the benefits of logical aggregation

    • The short function of aggregation is easier to reuse.
    • A short function that gathers together is easier to test
    • The code made up of several calls to these short functions is easier to understand, more problematic to find, and easier to modify and extend.
    • Short functions make the scope of local variables smaller, so the program is easier to do in parallel on the whole, and in some cases can make the program run more efficiently.

Logical aggregation of code refactoring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.