Refactoring is a series of equal transformations

Source: Internet
Author: User
Tags comments date now final variables

System reconfiguration requires us to modify every step of the code, can not change the external behavior of the software, so in the system refactoring all the methods, is a kind of code equivalent transformation. The process of refactoring is like doing a math problem, step-by-step, the equivalent transformation of the formula. After a series of equal transformations, the final results are different in form from the original, but the results can be identical with the original formula.

This isometric transformation is very important for refactoring, which allows us to refactor the program, the code, or the code. However, the equivalent transformation does not mean standing still. Just as the matrix can get the solution of the equations by the equivalent transformation, the calculus can calculate the final result by the equivalent transformation, and reconstruct the program structure by the equivalent transformation, while guaranteeing the correctness of the code. To illustrate this equivalent transformation in system refactoring, let's look at a simple example of the original program:

public class HelloWorld {public
    string SayHello (Date now, String user) {
        Calendar C;
        int h;
        String s = null;
        c = calendar.getinstance ();
        C.settime (now);
        h = c.get (calendar.hour_of_day);
        if (h>=6 && h<12) {
            s = "good morning!";
        } else if (h>=12 && h<19) {
            s = "good afternoon!";
        } else{
            s = "good night!";
        }
        s = "Hi," +user+ "." +s;
        return s;
    }
}

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

This is a very simple HelloWorld program, written simply to make it easier for everyone to understand the process of transformation of the program. This program is simple but it conforms to many features of legacy systems: no annotations, sequential programming, no hierarchy, low aggregation, and so on. So we did a preliminary refactoring, adding annotations, adjusting order, renaming variables, and segmenting:

/**
 * The Refactoring ' s Hello-world program
 * @author Fangang/public
class HelloWorld {
    /**
     * Say Hello to everyone
     * @param now
     * @param user
     * @return the words what to Say
     */public
    String SayHello (Date now, String user) {
        //get current hour of
        calendar calendar = calendar.getinstance ();
        Calendar.settime (now);
        int hour = Calendar.get (calendar.hour_of_day);
            
        Get the right words to say hello
        String words = null;
        if (hour>=6 && hour<12) {
            words = "good morning!";
        } else if (hour>=12 && hour<19) {
            words = "good afternoon!";
        } else{
            words = "good night!";
        }
        Words = "Hi," +user+ "." +words;
        return words
    }
}

The

Then extracts the code from the two comments to form the Gethour () and getsecondgreeting () functions:

/** * The Refactoring ' s hello-world program * @author Fangang/public class HelloWorld {/** * Say hello to Everyone * @param now * @param user * @return The words what to say/public String SayHello (Dat
        e Now, String user} {int hour = Gethour (now); Return "Hi," +user+ "."
    +getsecondgreeting (hour);
     }/** * Get current hour of the day. * @param now * @return current hour of/private int gethour (Date now) {Calendar calendar = Cal
        Endar.getinstance ();
        Calendar.settime (now);
    Return Calendar.get (Calendar.hour_of_day);
     }/** * Get the second greeting. * @param hour * @return the second greeting * * Private String getsecondgreeting (int hour) {if (HOUR&G
        T;=6 && hour<12) {return "good morning!";
        }else if (hour>=12 && hour<19) {return "good afternoon!";
       }else{     Return "Good night!"; }
    }
}

Through this example, we can see that the sequence of the sentence is not ordered to adjust the order is an equal amount of transformation, the statement of a relatively independent statement to form a function, and let the original statement to call this function, is an equivalent transformation. In addition, adjusting the function name, modifying the variable name, and so on, are equal transformations. The equivalent transformation, the program or those programs, the results of the implementation or those results, but the program organizational structure has changed, become more readable, maintainable, easy to change, this is the meaning of refactoring.

The dense program code is divided into several functions in order to effectively improve the readability of the code, the various variables and functions in the program are reasonably named, and in the function header or definition of the timely comments, but also improve the readability of the Code , it is effective to improve the maintainability and ease of the system to organize the various kinds of functions appropriately in their respective objects. These are very helpful for the day-to-day maintenance and life continuity of a legacy system.

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.