Refactoring: reorganizing functions

1, Put a piece of code into a separate function

1.1 code example

1.2 application scenarios

When there is an overly long function or a piece of code that needs comments to make people understand its purpose, put this code into a separate function.

1.3 benefits

First, there is a greater chance that functions can be reused with each other; Second, function override s are easier.

2, Insert the function ontology at the function call point, and then remove the function

2.1 code example

The code is as follows (example):

2.2 application scenarios

Sometimes when you encounter some functions, the internal code and function name are also clear and readable. At this time, you should remove this function and use the code directly. Indirection may help, but unnecessary indirection is always uncomfortable.

Another situation is that there are a group of functions that are not reasonably organized. You can move them all to a large function, and then extract small functions that are reasonably organized. It is easier to move a large function as a whole than to move a function and all other functions it calls.

2.3 benefits

If others use too many indirect layers, so that all functions in the system seem to be simple delegates to another function, causing us to be confused in these delegates, we usually use this method.
The indirect layer has its value, but not all the introduction layers have value. We can remove those useless introduction layers.

3, Create an independent and corresponding temporary variable for each assignment

3.1 code example

3.2 application scenarios and benefits

Temporary variables have various uses, some of which will cause temporary variables to be assigned multiple times. "Cyclic variables" and "set cyclic variables" are two typical examples.
Loop variables change with the operation of the loop, such as I in for (int i = 0; I < 10; i++);
Set cyclic variables are responsible for collecting a value formed by [the operation of the whole function].

In addition to these two cases, there are many temporary variables used to save the operation results of a lengthy code for later use. This variable should be assigned only once.
If they are assigned more than once, it means that they assume more than one responsibility in the function. If a temporary variable bears multiple responsibilities, it should be replaced by multiple temporary variables, each of which bears only one responsibility.
The same temporary variable undertakes two different things, which will confuse code readers.

3, Replace the position of a parameter with a temporary variable

3.1 code example

3.2 application scenarios and benefits

Pass an object as a parameter to a function, and "assign a value to the parameter" is to make the parameter reference (point to) another object. If you operate on the incoming object itself, it's no problem, but if the object is pointed to a completely different object, there may be problems, such as:

void avethoa(Object foo){
	foo.modifySomeWay(); //that's OK
	foo = anotherObject;  //trouble will follow you

The reason why I don't like this use is that it reduces the clarity of the code and confuses the two parameter transmission methods of value transmission and address transmission.
Java only adopts value passing. Let's look at a code:

public class CSDN {
    public static void main(String[] args) {
        int x = 1;
        System.out.println("Called in the main function triple Later x: "+x);

    private static void triple(int x) {
        x = x * 3;
        System.out.println("triple Later x: "+ x);

Output results:

If an object is passed in the parameter, it may be confusing, for example:

import java.util.Date;
public class CSDN {
    public static void main(String[] args) {
        Date data1 = new Date();
        System.out.println("date1 after nextDateUpdate:"+data1);

        Date data2 = new Date();
        System.out.println("date1 after nextDateReplace:"+data2);
    private static void nextDateUpdate(Date data1) {
        System.out.println("date1 in nextDateUpdate"+data1);
    private static void nextDateReplace(Date data2) {
        data2 = new Date(data2.getYear(),data2.getMonth(),data2.getDate()+1);
        System.out.println("date1 in nextDateReplace"+data2);

Output results:

In essence, object references are passed by value, so we can modify the internal state of the parameter object, but it is meaningless to reassign the parameter object.

4, Replace the function ontology with another algorithm

4.1 code example

4.2 application scenarios and benefits

Sometimes, it is found that there is a simpler solution than the original method, and the original algorithm needs to be changed at this time.
Sometimes you want to modify the original algorithm to do something slightly different from the original action. At this time, you can also replace the original algorithm with a more easily modified algorithm, so that the subsequent modification will be much easier.

Tags: Java programming language

Posted by TheIceman5 on Wed, 10 Aug 2022 21:29:37 +0300