Do you really check if the object is empty?

First of all, this question is very interesting. I believe that the first reaction of most people is not it null?

for example:

if(str != null){

}

However, many times we judge that the value sent by the front end may be an empty string, so the stricter way of writing is:

if(str != null && !str.equals("")){

}

Students with quick eyes and quick hands must have discovered the problem. What should I do if strit is null?

In fact, it nulldoesn’t matter even if it is, because it has been judged that it is not null, &&it is a short circuit and, if the previous condition is not true, the latter will not be executed at all.

Therefore, this code has become the first choice of many companies.

Someone asked, why do you want to judge whether it is an empty string, I don't judge whether it works.

In some cases it works, but in many cases it just doesn't.

For example, look at the code here:

if(userNo != null){
  User user = userMapper.selectOne(userNo);
  log.info(user.userName);
}

userIf an empty string is passed in, then this logic will be entered, and if it is found out null, user.userNamea null pointer will be reported when it is.

A few hard-headed students will ask again, so it’s fine if I throw an exception.

if(userNo != null && userNo.equals("")){
  User user = userMapper.selectOne(userNo);
  if(user == null){
    throw new Exception("用户不存在!");
  }
  log.info(user.userName);
}

At first glance, it looks fine, but when you look closely, it becomes a big problem. For example, my business scenario is that if the user exists, the points will be added, and if the user does not exist, the user will be created.

Look at the code:

if(userNo != null){
  User user = userMapper.selectOne(userNo);
  if(user == null){
    throw new Exception("用户不存在!");
  }
  addPoints(user,100);
}else{
  addUser();
}

The original intention of the code is, if there is one, userNoI will give him points, if not, userNoadd this user.

Now you pass an empty string, which means empty, but you have entered the logic of adding points. Even if an error is reported, is it really what the customer wants? The customer is hope, I don't have a user account, you can add one for me.

So it's against the business.

Well, I can actually read minds. You may be thinking at this moment, then if I just send userNoyou something that I can’t find, isn’t it addUserthe logic that you can’t follow?

nullDear, the original intention of you passing an empty string must be that I don't have this thing. But if you pass a wrong data, the nature will be different, which means that I have this thing, but I filled it in wrongly. Then there is no problem with this logic.

Therefore, we must keep in mind that if the concept of this thing in business is empty, we must judge both the nullempty string and the two-pronged approach to ensure nothing goes wrong.

Of course, in fact, such a common function, we can use org.apache.commons.lang3.StringUtilsthe isNotBlankmethod:

isNotBlank

    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

isWhite

public static boolean isBlank(CharSequence cs) {
        int strLen = length(cs);
        if (strLen == 0) {
            return true;
        } else {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        }
    }

length

    public static int length(CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }

It not only helps you judge null and empty strings, but also judges spaces for you, which is really cool!

A small short judgment may cause you a big loss. If a short judgment is not done well, then the logic inside will be completely exposed. I believe you must NullPointerExceptionhave

Guess you like

Origin blog.csdn.net/weixin_39570751/article/details/130420044