How to improve code quality, refactoring is not a “panacea”

With the continuous advancement of programming technology, programming languages are becoming more and more advanced, and functional encapsulation is becoming more and more complete. Various technologies are helping programmers improve the efficiency of writing code. Through layers of encapsulation, programmers don’t seem to need to understand the technical details, they only need to translate the required content line by line.
Many programmers don’t understand how to organize code, improve operating efficiency, and what the underlying principles are, but the code they wrote passed compilation, testing, and ran online for a month without problems. Work has a significant negative impact.
Although some “rookies” have expressed concerns about potential problems with code quality, these concerns are usually appeased by saying “the current project progress is urgent, and we will refactor the code after the project progress is relaxed in a few months.”
Although we cannot deny that in some (extremely limited) scenarios, refactoring is a way to solve the problem. However, after writing a lot of code, we will find that refactoring is often one of the most complex tasks in the program development process. If we spent a month writing a bunch of bad code, it might take longer to refactor it and the risk might be higher.
We’ve gone through several intolerably large-scale refactorings in the past. Before each refactoring, we will gather the experts in the group, conduct multiple analysis meetings, pause the needs of the group, and then start refactoring. However, during the reconstruction process, we often encounter various difficulties, and many unexpected problems appear almost every day. Moreover, there will almost certainly be some problems when going online.
When refactoring complex code, from a technical perspective, three tasks need to be performed: understanding the original code, decomposing the original code, and building new code. However, the original code to be refactored is often difficult to understand; the modules are overly coupled, and every move may affect the entire system, making it difficult to control the scope of impact; the original code is not easy to test, and the correctness of the new code cannot be ensured.
How much efficiency will be improved after refactoring? How much will the risk be reduced? This question is difficult to answer because low-quality code itself cannot be simply standardized.

Different from writing poor quality code, there are many prerequisites for writing good code:

? Understand the functional requirements to be developed.

? Understand how the program works.

? Make reasonable abstractions.

? Organize complex logic.

? Correct estimation of your own development efficiency.

? Continuous practice.

Therefore, with the advancement of technology, can we finally achieve the century-old reconciliation between rapid delivery and guaranteed code quality?

The emergence of AI code generation tools, represented by FuncGPT (Hui Function), recently launched by SoFlu Software Robot, which focuses on AI-generated Java functions, may bring new hope.

As an important part of Feisuan SoFlu software robot, FuncGPT (Hui Function) supports the creation of all types of functions. Use natural language to describe Java function requirements and generate high-quality, highly readable Java function code in real time. The generated code can be directly copied to IDEA, or imported into the Java fully automatic development tool function library with one click. At the same time, FuncGPT adopts the best practices of code writing and large-scale machine joint training, and is committed to empowering software development through AIGF (AI-Generated Function) to provide Chinese software developers with full-stack and full-process software development. The best experience, let programmers say goodbye to 996!

FuncGPT (FuncGPT) has the following five capabilities:

● Natural language: Functions can be generated through natural language, lowering the threshold for software development.

● Second-level function development: Fully automatic second-level function development, completed in seconds, improving efficiency by a thousand times.

● One-stop function development and application: functions can be directly applied after generation, with guaranteed efficiency and safety.

● Reliable quality: The generated function code complies with industry standards, has good readability, and is consistent with the optimal solution.

● Flexible generation: It can be generated according to the specific needs of developers and supports adjustment and modification.

What is the code quality of FuncGPT (Hui Function)? Let’s take a Java function to implement BigDecimal’s expression operation requirements as an example, and let FuncGPT compete with the mountain of code.

Code shit mountain example

@MethodDesc(desc="BigDecimal is calculated according to the formula, and the result is rounded to N decimal places", params= {
            @ParamDesc(name="scaleLen",desc="result decimal places"),
            @ParamDesc(name="middleScaleLen",desc="Decimal digits for middle operation division (passing 0 defaults to 6 digits)"),
            @ParamDesc(name="formualText",desc="Calculation formula (such as: A + B/C), if there is a negative number, please replace it with an underscore (for example: A + B*-1 is written as A + B*_1"),
            @ParamDesc(name="varArr",desc="Variable name and variable value list (such as: A:90, B:89)"),
    })
    public static BigDecimal bigDecimalFormualCalc(int scaleLen, int middleScaleLen, String formualText, Map<String, Object> varArr) {
        int iMiddleScaleLen=6;
        if(middleScaleLen>0){
            iMiddleScaleLen=middleScaleLen;
        }
        Set<String> keySet = varArr.keySet();
        for (String key : keySet) {
            varArr.put(key, varArr.get(key).toString().replace("-", "_"));
        }

        BigDecimal result=null;
        String formalTextStr=formualText;
        String sFormual="";
        String valueTemp="";
        int iLeftBracketIndex=formualTextStr.lastIndexOf(LEFT);
        int iRightBracketIndex=0;
        if(iLeftBracketIndex>=0){
            while (iLeftBracketIndex>=0){
                iRightBracketIndex=formualTextStr.indexOf(RIGHT,iLeftBracketIndex);
                sFormual=formualTextStr.substring(iLeftBracketIndex,iRightBracketIndex + 1);
                sFormual=sFormual.replace(LEFT, REPLACEMENT);
                sFormual=sFormual.replace(RIGHT,REPLACEMENT);
                result= FloatUtils.oneBigDecimalFormualCalc(middleScaleLen,iMiddleScaleLen,sFormual,varArr);
                valueTemp=result.toString().replace("-","_");
                formualTextStr=formualTextStr.replace(LEFT.concat(sFormual).concat(RIGHT),valueTemp);
                iLeftBracketIndex=formualTextStr.lastIndexOf(LEFT);
            }
        }

        result=FloatUtils.oneBigDecimalFormualCalc(scaleLen,iMiddleScaleLen,formualTextStr,varArr);
        return result;
    }
\t
\t
public class FloatUtils {
    private FloatUtils(){}
    public static BigDecimal oneBigDecimalFormualCalc(int scaleLen, int middleScaleLen,
                                                      String oneFormualText, Map<String, Object> varArr) {

        BigDecimal result=null;
        String formualTextStr=oneFormualText;
        String linkStr="";

        Set<String> keySet = varArr.keySet();
        for (String key : keySet) {
            formualTextStr = formualTextStr.replace(key, varArr.get(key).toString());
        }
        char c ;
        List<Character> chars = new ArrayList<>();
        List<String> valueList = new ArrayList<>();
        String oneValue="";
        chars.add(' + ');
        chars.add('-');
        chars.add('*');
        chars.add('/');
        boolean isFindedLink=false;
        while (formualTextStr.length()>0) {
            isFindedLink=false;
            for (int i = 0; i < formalTextStr.length(); i + + ) {
                c = formalTextStr.charAt(i);
                if (chars.contains(c)) {
                    linkStr = linkStr.concat(String.valueOf(c));
                    oneValue = formalTextStr.substring(0, i);
                    oneValue=oneValue.replace("_","-");
                    formualTextStr = formualTextStr.substring(i + 1, formualTextStr.length());
                    valueList.add(oneValue);
                    isFindedLink=true;
                    break;
                }
            }
            if(!isFindedLink){
                formualTextStr=formualTextStr.replace("_","-");
                valueList.add(formualTextStr);
                formualTextStr="";
            }
        }
        BigDecimal[] valueArr=new BigDecimal[valueList.size()];
        for(int i=0;i<valueList.size();i + + ){
            valueArr[i]=new BigDecimal(valueList.get(i).toString());
        }

        result= FloatFunction.bigDecimalValueCalc2(scaleLen,middleScaleLen,linkStr,valueArr);
        return result;
    }


}


@MethodDesc(desc="BigDecimal operates on a value list (the decimal place in the middle division is controlled by parameters), the result is rounded to N decimal places", params= {
            @ParamDesc(name="scaleLen",desc="number of decimal places"),
            @ParamDesc(name="middleScaleLen",desc="Decimal digits for middle operation division (passing 0 defaults to 6 digits)"),
            @ParamDesc(name="linkArr",desc="Operator list (such as: A + B/C, then pass + /)"),
            @ParamDesc(name="valueArr",desc="String value list (such as: A + B/C, then pass 3 values of ABC)"),})
    public static BigDecimal bigDecimalValueCalc2(int scaleLen, int middleScaleLen,String linkArr,Object[] valueArr) {
        BigDecimal result;
        int iMiddleScaleLen=6;
        if(middleScaleLen>0){
            iMiddleScaleLen=middleScaleLen;
        }
        ArrayList<BigDecimal> valueList=new ArrayList();
        ArrayList linkList=new ArrayList();
        for (int i=0;i<valueArr.length;i + + ){
            if (valueArr[i] instanceof BigDecimal){
                valueList.add((BigDecimal)valueArr[i]);
            }else if (valueArr[i] instanceof Double){
                valueList.add(BigDecimal.valueOf((double)valueArr[i]));
            }else if (valueArr[i] instanceof Integer){
                valueList.add(BigDecimal.valueOf((int)valueArr[i]));
            }else {
                valueList.add(new BigDecimal(valueArr[i].toString()));
            }
        }
        for (int i=0;i<linkArr.length();i + + ){
            linkList.add(linkArr.charAt(i));
        }
        String linkStr="";
        BigDecimal valueOne=null;
        BigDecimal valueTwo=null;
        int iIndex=0;
        int multiplyDivideIndex=-1;
        BigDecimal tempValue= BigDecimal.ZERO;
        while (linkList.size()>0){
            multiplyDivideIndex=-1;
            for (int i=0;i<linkList.size();i + + ) {
                linkStr=linkList.get(i).toString();
                if ("*".equals(linkStr)|| "/".equals(linkStr)) {
                    multiplyDivideIndex = i;
                    break;
                }
            }
            if(multiplyDivideIndex<0) {
                iIndex=0;
            }
            else {
                iIndex=multiplyDivideIndex;
            }
            linkStr=linkList.get(iIndex).toString();
            valueOne=valueList.get(iIndex);
            valueTwo=valueList.get(iIndex + 1);
            switch (linkStr) {
                case " + ":
                    tempValue=valueOne.add(valueTwo);
                    break;
                case "-":
                    tempValue=valueOne.subtract(valueTwo);
                    break;
                case "*":
                    tempValue=valueOne.multiply(valueTwo);
                    break;
                case "/":
                    tempValue=valueOne.divide(valueTwo,iMiddleScaleLen,BigDecimal.ROUND_HALF_UP);
                    break;
                default:
                    break;
            }
            valueList.remove(iIndex);
            valueList.remove(iIndex);
            valueList.add(iIndex,tempValue);
            linkList.remove(iIndex);
        }
        if (scaleLen<0){
            result=valueList.get(0);
        } else {
            result=valueList.get(0).setScale(scaleLen,BigDecimal.ROUND_HALF_UP);
        }
        return result;
    }

FuncGPT(FuncGPT)

//Class name: BigDecimalCalculator
// Function name: calculateExpression
// Function: Use BigDecimal to calculate the result of an expression
// POM dependency package:
// <dependency>
// <groupId>org.apache.commons</groupId>
// <artifactId>commons-lang3</artifactId>
// <version>3.12.0</version>
// </dependency>

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Stack;

public class BigDecimalCalculator {
    /**
     * Use BigDecimal to calculate the result of an expression
     * @param expression The expression to be calculated
     * @return calculation result
     */
    public static BigDecimal calculateExpression(String expression) {
        if (StringUtils.isBlank(expression)) {
            throw new IllegalArgumentException("Expression cannot be blank");
        }

        Stack<BigDecimal> numberStack = new Stack<>();
        Stack<Character> operatorStack = new Stack<>();

        for (int i = 0; i < expression.length(); i + + ) {
            char ch = expression.charAt(i);
            if (Character.isDigit(ch)) {
                StringBuilder sb = new StringBuilder();
                while (i < expression.length() & amp; & amp; (Character.isDigit(expression.charAt(i)) || expression.charAt(i) == '.')) {
                    sb.append(expression.charAt(i));
                    i + + ;
                }
                i--;

                BigDecimal number = new BigDecimal(sb.toString());
                numberStack.push(number);
            } else if (ch == '(') {
                operatorStack.push(ch);
            } else if (ch == ')') {
                while (!operatorStack.isEmpty() & amp; & amp; operatorStack.peek() != '(') {
                    BigDecimal result = performOperation(numberStack, operatorStack);
                    numberStack.push(result);
                }
                if (!operatorStack.isEmpty() & amp; & amp; operatorStack.peek() == '(') {
                    operatorStack.pop();
                }
            } else if (isOperator(ch)) {
                while (!operatorStack.isEmpty() & amp; & amp; hasHigherPrecedence(ch, operatorStack.peek())) {
                    BigDecimal result = performOperation(numberStack, operatorStack);
                    numberStack.push(result);
                }
                operatorStack.push(ch);
            }
        }

        while (!operatorStack.isEmpty()) {
            BigDecimal result = performOperation(numberStack, operatorStack);
            numberStack.push(result);
        }

        return numberStack.pop();
    }

    private static boolean isOperator(char ch) {
        return ch == ' + ' || ch == '-' || ch == '*' || ch == '/';
    }

    private static boolean hasHigherPrecedence(char op1, char op2) {
        if ((op1 == '*' || op1 == '/') & amp; & amp; (op2 == ' + ' || op2 == '-')) {
            return true;
        }
        return false;
    }

    private static BigDecimal performOperation(Stack<BigDecimal> numberStack, Stack<Character> operatorStack) {
        BigDecimal num2 = numberStack.pop();
        BigDecimal num1 = numberStack.pop();
        char operator = operatorStack.pop();

        BigDecimal result;
        switch (operator) {
            case ' + ':
                result = num1.add(num2);
                break;
            case '-':
                result = num1.subtract(num2);
                break;
            case '*':
                result = num1.multiply(num2);
                break;
            case '/':
                result = num1.divide(num2, 2, RoundingMode.HALF_UP);
                break;
            default:
                throw new IllegalArgumentException("Invalid operator");
        }

        return result;
    }
}

// Function example
// Example of the result of using BigDecimal to calculate an expression
// Input parameter: expression, the expression to be calculated
// Output parameter: result, calculation result
// Call example:
// String expression = "2.5 + 3 * (4 - 1)";
// BigDecimal result = BigDecimalCalculator.calculateExpression(expression);
// System.out.println(result);
//Output result: For example, the result of calculating the expression "2.5 + 3 * (4 - 1)" is: 11.5
//The output result is: 11.5

After comparative analysis, it is not difficult to find:

In terms of readability:

Manual code naming is truncated and irregular, comments are not concise and clear enough, and code readability is poor;

FuncGPT uses reasonable naming and comments. The naming of functions and variables is clear and clear. The comments explain the function and parameters of the function, which improves the readability of the code.

In terms of maintainability:

Manual code is nested layer by layer, and the method is cumbersome, which is not conducive to later maintenance and modification;

FuncGPT uses appropriate data structures and algorithms. It uses two stacks to handle operators and operands, and avoids complex nested logic and multiple judgments by traversing expression characters and performing calculations based on priority.

In terms of security:

Manual code does not judge null values, has loopholes, and has poor robustness;

FuncGPT introduces the Apache Commons Lang library and uses the isBlank method of the StringUtils class to determine whether the expression is empty, which improves the robustness and reliability of the code.

In summary, the code generated by FuncGPT (Hui Function) has better readability, maintainability and exception handling, and uses reverse Polish expression calculation and third-party libraries to provide more powerful functions.

FuncGPT(FuncGPT) is free to use and generates functions in seconds

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 130235 people are learning the system