Tips for improving code readability: The importance of comments

A: Why don’t you even add comments when writing code? B: Why does the boss need to add comments? A: If you don’t add comments, how do you know I can understand your code? B: When you encounter a problem, you can just find it, right? A: What will the company do if you get sick and ask for leave, get upset, and leave your job? B: I don’t think there is any problem now. I am also very satisfied with the company. I feel at ease!

It is another wonderful performance of the post-00s rectification of the workplace. It is undeniable that in the actual software development process, many developers are still unwilling to write comments because they think it will be a waste of time, or they think that their code is clear enough and does not require additional explanation. But this idea is too young and too simple. Code comments have a profound impact on the quality and efficiency of the project, and their importance in software development cannot be underestimated. 1. Improve code readability Good comments can help other developers better understand the logic and functionality of the code. When you or others need to read the code, you can quickly understand the role and purpose of the code by reading the comments. This helps improve code readability and maintainability. 2. Code documentation comments can be used as documentation for the code, providing detailed information about the code, including the role of the function, the purpose of the parameters, and the return value of the function. This information can be very useful to other developers and yourself when reviewing the code in the future, saving you the time and effort of re-reading and understanding the code. 3. Code quality and error troubleshooting. Appropriate comments can help find problems in the code, such as logical errors, spelling errors, etc. When other developers or yourself are reading the code, if they find code behavior or logic that is inconsistent with the comments, they can make timely corrections to improve code quality. 4. Improve development efficiency. The time spent writing comments is actually an investment. While writing comments may seem like it slows down development, you can actually save a lot of time by using comments. By quickly understanding the function and logic of the code, you can work faster and improve development efficiency. 5. Teamwork and knowledge sharing In team development, annotation is an important tool for knowledge sharing. Team members can understand other members' work ideas and implementation methods by reading comments. This helps communication and collaboration between team members and improves the efficiency of teamwork. We are fully aware of the importance of comments, but due to the pressure of delivery time and the limitations of some developers' abilities, we have no way of writing clear and useful comments. With the development of AI technology, many coding assistance tools have solved this problem to a certain extent. Take the function AI generator FuncGPT (Hui Function) launched by the intelligent development tool Feisuan SoFlu software robot as an example. 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. Contains clear code comments to help development engineers understand the purpose, input and output of functions. The generated code can be directly copied to IDEA, or imported into the Java fully automatic development tool function library with one click.

Take the "BigDecimal expression operation" generated by FuncGPT (intelligent function) as an example: `// Class name: BigDecimalCalculator // Function name: calculateExpression // Function function: Use BigDecimal to calculate the result of the 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 the expression * @param expression The expression to be calculated * @return the 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() && (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() && operatorStack.peek() != '(') {
                BigDecimal result = performOperation(numberStack, operatorStack);
                numberStack.push(result);
            }
            if (!operatorStack.isEmpty() && operatorStack.peek() == '(') {
                operatorStack.pop();
            }
        } else if (isOperator(ch)) {
            while (!operatorStack.isEmpty() && 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 == '/') && (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, expression to be calculated // Output parameter: result, calculation result // Calling example: // String expression = "2.5 + 3 * (4 - 1)"; // BigDecimal result = BigDecimalCalculator.calculateExpression(expression); // System.out.println(result); // Output result: for example, calculate the expression "2.5 + 3 * (4 - 1) "The result is: 11.5 // The output result is: 11.5 ` The function generated by FuncGPT (Hui Function) uses reasonable naming and comments. The naming of functions and variables is clear and clear, and the comments describe the functions and parameters of the function. Description, improves the readability of the code. Spark Big Model commented, "Generally speaking, the comments of this code are of high quality and can provide enough information for other developers to understand and use this class."

FuncGPT (FuncGPT) is now free to use, download link: https://c.suo.nz/d88yp

The author of the open source framework NanUI switched to selling steel, and the project was suspended. The first free list in the Apple App Store is the pornographic software TypeScript. It has just become popular, why do the big guys start to abandon it? TIOBE October list: Java has the biggest decline, C# is approaching Java Rust 1.73.0 Released A man was encouraged by his AI girlfriend to assassinate the Queen of England and was sentenced to nine years in prison Qt 6.6 officially released Reuters: RISC-V technology becomes the key to the Sino-US technology war New battlefield RISC-V: Not controlled by any single company or country, Lenovo plans to launch Android PC
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4868096/blog/10117455