Java talks about guard statement (guard statement) if else from ugly example code

Recently, I was refactoring some old projects and found some typical code blocks.

Thinking of these common writing methods, it may be easier for beginners to 'produce', so I decided to talk about them.

Example:


As you can see, this is a function used to detect duplication of a name before adding & modifying it.

    /**
     * 检测标签名是否重复
     * @param Id
     * @param name
     * @return
     */
    Boolean checkLabelNameExist(Integer Id,String name);

Because new additions and modifications are combined, there is an additional ID input parameter to determine whether it is a new addition (if the ID is empty, it is a new addition, and if it is not empty, it is a modification).

So, for new additions, it means that if the data with this name is found to exist, it is duplicated.

As for the modification, it means that the data with this name needs to be excluded. It is considered a duplicate if it still exists.

So let’s take a look at the ugly output :

    /**
     * 编辑或新增前 ,判断名称是否已存在
     * @param id
     * @param name
     * @return true 为存在  ; false 为不存在
     */
    @Override
    public Boolean checkLabelNameExist(Integer id, String name) {
        Label label = labelService.queryOneByName(name);
        if (Objects.isNull(id)){
            if (Objects.isNull(label)){
                return  false;
            }else {
                return  true;
            }
        }else {
            if (Objects.isNull(label)){
                return  false;
            }else {
                if (!label.getId().equals(id)){
                    return  true;
                }else {
                    return  false;
                }
            }
        }
    }

When you first see the code above, does it look familiar to you? 

Have you ever done or are you still doing similar output?

Let's take a look at what the code looks like after using the guard statement method (isn't it much simpler):

    @Override
    public Boolean checkLabelNameExist(Integer id, String name) {
        Label label = labelService.queryOneByName(name);
        //查出来是空的,代表肯定不存在
        if (Objects.isNull(label)){
            return  false;
        }
        //如果是新增,查询出来不为空,则代表已存在
        if (Objects.isNull(id)){
            return  true;
        }
        //若是编辑,仅需要判断传入的ID 和查询出来的是否一致,不一致则代表 存在
        return !label.getId().equals(id);
    }

There was no problem with the journal code at the beginning, but it did seem a lot confusing, and I was just taking it step by step, without overall planning from a higher-level perspective.

At first, I was not familiar with the idea of ​​using guard sentences. It doesn't matter.

After you finish writing, you can read the code line by line.

Find the guard conditions, which conditions do not need to be embedded in the if depth, and can be judged in advance.

 Beginners can take a look at the two code snippets above and think about it for themselves.

Guard statements simplify the flow of the program by performing logical analysis on the original conditions and prioritizing judgment on certain key (guard) conditions, so they are called guard statements.

Guess you like

Origin blog.csdn.net/qq_35387940/article/details/134637398