Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the Code Review Service reduces Billion DollarMistake NPE risk

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Code review service is how to reduce Billion DollarMistake NPE risk, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

"Null Safety" is one of the most attractive features of Kotin for Java developers, and the goal of the Kotlin type system is to eliminate the dangers of Null reference. Tony Hoare called Null reference "my billion-dollar mistake" on QCon London in 2019. The so-called Null reference is one of the most common traps in many programming languages, which means that accessing members of Null reference will lead to null reference exception. In the Java language, Nullreference is usually equivalent to NullPointerException (that is, NPE).

For Java developers, perhaps only after dealing with NPE can they become a real Java developer. Generally speaking, NPE comes from the following scenarios:

An instance method of an empty object was called

Access or modify domain members of an empty object

When the array is an empty object, take its length

Model or modify some null elements in the array

Throw null as a Throwable value

To put it simply, how to prevent NPE is mainly composed of two elements: means and methods, consciousness and experience. Java developers can refer to the following best practices for air defense:

Call the equals () and equalsIgnoreCase () methods by knowing string constants rather than unknown objects

Use valueOf () first where valueOf () returns the same result as toString ()

Methods and libraries for using null security

Avoid returning null from methods, but instead return empty collection or array

Use the annotations @ NotNull and @ Nullable

Avoid unnecessary autoboxing and unboxing in your code

Follow Contract and define reasonable default values

Using Optional in Java 8

Double input parameter check

Although the ability of Java developers is essential for air defense, it is inevitable that people will make mistakes. To further reduce risk, Huawei Cloud DevCloud recommends that project or product teams use code review services to check for the presence of NPE.

1. Preparatory work

1.1 sample Project

Code4Benchmark is a sample project developed with Java, which is intended to be used to evaluate the ability of code quality assessment tools or cloud services to check for code problems, such as null pointer (Null Pointer), resource leakage, SQL injection (SQL Injection), etc.

For null pointer scenarios, explicit dereferencing, pre-nulling, post-nulling, null return value and so on are mainly used. The code snippet is as follows:

Explicit dereferencing: dereferencing null objects directly

Figure 7 details of the question and suggestions for modification

NPE is a classic problem in Java development. Huawei Cloud DevCloud code review service provides more than the three rules mentioned in this article. For more information, please see "rules". In the actual project development, users can directly select the "comprehensive inspection rules" to carry out omni-directional code quality testing, in order to improve code quality.

W. Edwards Deming once said, "Inspectionis too late. Quality cannot be inspected into a product or servive. It must bebuilt into it." In essence, software quality depends on the engineer's consciousness, ability, method and skill. However, engineers' awareness, abilities, methods and skills need to be constantly improved. Therefore, Huawei Cloud DevCloud code review service still plays an important role in software delivery, which can not only find code quality problems in time, but also strengthen the expected behavior of engineers, and help to improve engineers' awareness, abilities, methods and skills.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report