Programmer's Road to Growth Experience - Efficient Coding Tips

With the rapid development of AIGC, programmers are increasingly able to feel the pressure from the outside world and themselves. How can we not fall behind or be replaced in the era of vigorous development of AI? The development efficiency of the project plays a vital role.

First ask a few questions:

  1. How to achieve efficient programming?
  2. Where is the core of efficient programming?

1. How to achieve efficient programming?

1) Accumulate programming experience and improve the understanding of the company's development architecture

Now let's imagine this scenario:
Your superior requires you to complete a project within 3 days. This project is not easy to achieve, and requires the use of timed task framework, message push and excel import and export functions. If you are a junior programmer or a new employee who has just joined the company, this job may be a bit difficult. As a junior programmer, you lack development experience, and as a new employee, you lack understanding of the company's structure. So what should we do?

That's right, in order to achieve efficient programming, we first need to accumulate programming experience and improve our understanding of the company's development architecture and systems. Accumulating programming experience is not only to write more code, but also to improve the ability to deal with problems, so as to avoid stepping on the same pit twice.

For junior programmers or new employees who have just entered the industry, developing projects with experienced colleagues can quickly increase their ability to solve problems, which is the so-called teaching. In the process of teaching, discussing problems with each other and forming a good technical discussion atmosphere will also play a better role in improving; for intermediate programmers or more advanced programmers, it is necessary to gradually solve problems in the process of problem solving. Form your own "system", and troubleshoot problems according to your own system every time you encounter an error or problem, and update your own error data set in time after the processing is completed.

2) Realize code reuse

Now let's imagine another scenario:
we write code every day, but the efficiency has not improved because of it, and as the number of projects increases, it increases our code volume and coding pressure. How to deal with these lengthy and complex codes, which can not only reduce our coding burden, but also improve the code quality?

To achieve efficient programming, an advanced way is to achieve code reuse. We may have heard an interesting story, a programmer likes to carry a USB flash drive with him, which records all the projects he has developed, but one day, he lost his precious USB flash drive, and he sighed His career is over.

When I saw this story, my first reaction was that this programmer is at least not a rookie. He will save and reuse his code to facilitate subsequent system development. But the reality is that many programmers do not do this. They may write additions, deletions, changes, and checks all day long, living in their own comfort zone, thinking that they are "hardworking", but never thinking of being "lazy".

So, how to achieve code reuse?

As an experienced java development engineer, I would first think of using tools such as reflection, annotations, generics, enumerations, and design patterns to build code functions. For example, to check whether the data is empty or not, I will write a comment first.

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NotBlank {
    
    
    String message() default "参数不能为空";
}

Then use reflection to get all DeclaredFields in a class and its parent class, that is, all member variables, including private ones, and then get the annotated variables in it, check whether it is empty, and throw an exception if it is empty.

/**
 * 实体工具类
 * @author zyl
 */
@Slf4j
public class EntityUtils {
    
    

    public static <T> boolean verifyIsEmpty(T entity) {
    
    
        if (entity == null) {
    
    
            throw new MyException("实体类为空");
        }
        try {
    
    
            Class<?> cls = entity.getClass();
            String clsName = cls.getSimpleName();
            while (!"Object".equalsIgnoreCase(clsName.trim())) {
    
    
                Field[] fields = cls.getDeclaredFields();
                for (Field f : fields) {
    
    
                    f.setAccessible(true);
                    NotBlank anno = f.getAnnotation(NotBlank.class);
                    if (anno != null) {
    
    
                        Object obj = f.get(entity);
                        if (obj == null || "".equals(obj)) {
    
    
                            throw new MyException(anno.message());
                        }
                        if (obj instanceof List && ((List) obj).isEmpty()) {
    
    
                            throw new MyException(anno.message());
                        }
                        String str = String.valueOf(obj);
                        if (str.isEmpty()) {
    
    
                            throw new MyException(anno.message());
                        }
                    }
                }
                cls = cls.getSuperclass();
                clsName = cls.getSimpleName();
            }
            return false;
        } catch (Exception ex) {
    
    
            log.error(ex.getMessage());
            throw new MyException(ex.getMessage());
        }
    }
}

If you can’t think of using this method, you can also use the @NotBlank annotation that comes with spring to achieve the above functions.

The second path to code reuse is the writing of components. This has certain requirements for programming ability, such as design patterns, such as the grasp of the overall function of components. But once you learn how to package and use components, then for general business system development, the efficiency will be greatly improved. For example, it is troublesome to process wood into wheels, but it becomes easy to assemble the wheels into a frame.
insert image description here
The higher level is to implement the code generator by yourself, that is, to write a code generation framework by yourself. This is too difficult, haha. Fortunately, there are relatively mature code generation tools on the market, such as Ali's Yidai, Baidu's Aispeed, etc.

The core of efficient programming

  1. Repeated things are standardized, and standard things are automated. It is not advisable to work hard, sometimes laziness is king.
  2. Accumulate experience and maintain an open-minded learning attitude.

Guess you like

Origin blog.csdn.net/qq_31236027/article/details/132046808