Data aggregation of code refactoring instances

Source: Internet
Author: User

Agile development emphasizes the need to refactor code frequently. In the development process, often is the development and the reconstruction alternately. Short-term refactoring can make subsequent development and maintenance easier. I think that code refactoring can be divided into logical refactoring and data structure reconstruction. Refactoring of data structures often requires multiple changes to the code, but the refactoring of data structures can also be a great convenience for subsequent development and maintenance. Here is an example of a data structure refactoring.

This is the previous code refactoring experience, remembered today, remember, to help their memory. Of course, since it's refactoring, you have to admit that you wrote the first version of the ugly code.

To facilitate the description, use javascript for illustration.

The story is like this. At first, the task is to draw some squares and connect them with lines, and the boxes are the same size. The more simple the code should be, the better it is, the more you need to record the position of each box with an array. Then iterate through the array and draw a box.

1 var positions = []; 2 Positions.foreach (function(p) {3  4 });

However, the next task changes, you need to display a name in the middle of each box. OK, so we do not change the original code based on, and then add an array to record each box name can be, just draw the function of the box need to add a new parameter, the name of the box. It seems to be possible.

1 var positions = []; 2 var names = []; 3  for (var i = 0; i < positions.length; i++) {4  drawrect (Positions[i], names[i]); 
    5 }

Wait, if this is positions What if it's not the same length as names? So we have to add judgment.

1 varpositions = [];2 varNames = [];3 4 if(Positions.length! =names.length) {5   return;6 }7 8  for(vari = 0; i < positions.length; i++) {9 DrawRect (Positions[i], names[i]);Ten}

Next, the task changed again. There are several different types of boxes, and different types have different representations. Or do the same? However, if there are other functions that deal with the box, wouldn't it be necessary to change each function? And as the code becomes more complex, the changes are becoming more and more difficult.

What about that? In fact, these data are interrelated. The name, position, and type of the box is actually a property of the box. In that case, we'll define a box class, including all of these attributes.

1 var rects = [2  {"Name": "", "position": []}3]; 4 5 Rects.foreach (function(r) {6  drawrect (r); 7 });

Next, the task changed a variety of things, for example, we need to draw a picture for each box, so we need to record the image address; we need to delete the box, so we need to record the handle to the box. This is easy, and the definition of the class to modify the box is fine. The parameters of the function that are processed by all the other boxes do not need to be modified.

Finally, the other benefit of aggregating the relevant data is that it makes the program easier to understand, so the probability of error in development is lower and the modification is easier.

Data aggregation of code refactoring instances

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.