Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在没有操作的情况下,返回布尔/值而不是null是否是一种好的做法?_Java_Design Patterns_Boolean - Fatal编程技术网

Java 在没有操作的情况下,返回布尔/值而不是null是否是一种好的做法?

Java 在没有操作的情况下,返回布尔/值而不是null是否是一种好的做法?,java,design-patterns,boolean,Java,Design Patterns,Boolean,此观察是在查看中的addNodecode之后进行的。编码人员使用了真和假来区分无操作和操作。下面的代码是一个类似的示例,它只是为了方便地问我的问题而定制的。是否建议/良好的做法是返回布尔值以区分op和无op,从而为代码的客户端提供更高的可见性 public class FreeMain { private List<Integer> fooList; FreeMain ( ) { } /** * Lazy init - either set

此观察是在查看中的
addNode
code之后进行的。编码人员使用了真和假来区分无操作和操作。下面的代码是一个类似的示例,它只是为了方便地问我的问题而定制的。是否建议/良好的做法是返回布尔值以区分op和无op,从而为代码的客户端提供更高的可见性

public class FreeMain {

    private List<Integer> fooList;

    FreeMain ( ) {  }

    /**
     * Lazy init - either set a value of no-op.
     * 
     * @param barList
     */
    public void putList(List<Integer> barList) {
        if (this.fooList == null) {
            this.fooList = barList;
        }
    }



    /**
     * Boolean returned as an indication to the user of operation status
     * 
     * @param barList 
     * @return true if putLists sets the value, false is no-op.
     */
    public boolean putListWithBoolean(List<Integer> barList) {
        if (this.fooList == null) {
            this.fooList = barList;
            return true;
        }
        return false;
    }
}
公共类FreeMain{
私人名单傻瓜;
弗里曼(){}
/**
*Lazy init-设置no-op的值。
* 
*@param barList
*/
公共作废列表(列表栏列表){
if(this.whoolist==null){
this.whoolist=barList;
}
}
/**
*作为操作状态指示返回给用户的布尔值
* 
*@param barList
*@return true如果putLists设置该值,则false为no-op。
*/
公共布尔putListWithBoolean(列表栏列表){
if(this.whoolist==null){
this.whoolist=barList;
返回true;
}
返回false;
}
}

我将这样设置,在这种情况下,putList是否有责任通过返回布尔值来告诉您尝试放置的列表是否被接受?我认为不是,这是因为putList应该只处理替换列表指针的单个模块,而不返回任何内容

如果您确实想知道条件是否被授予(或者对于可能发生的任何异常行为),请使用异常。然后,在使用putList时(使用try-catch块),只需将这些异常捕获到main中,然后做任何您想做的事情

例如:

public void putList(List<Integer> barList) throws new MyListException{
    if (this.fooList == null) {
        this.fooList = barList;
    } else {
        throw new MyListException("The pointer of fooList can not be changed because the fooList is not null");
    }
}

public class MyListException extends Exception {
    public MyListException(String s) {
        super(s);
    }
}
最后,由于我不太清楚“在没有操作的情况下不是null”是什么意思,我想说:当您使用
putList(…)
并且条件未被授予时,它不会返回
null
,而是什么也不做。但是,在这种情况下,最好使用我已经演示过的异常(在第一个示例中),这样您就可以知道在期望putList()实际替换指针的情况下发生了什么错误。这是因为你不会总是有机会花时间搜索代码来理解哪里出了问题。这对于您提供的代码示例来说并不重要,因为它很简单,但是如果您有一个更复杂的putList,并且可能会出现多个问题,该怎么办


总的来说,我不能说putListWithBoolean()是否是一种不好的做法,因为这取决于它的使用方式(如java示例所示),而没有例外的putList()可以被认为是一种不好的做法,因为您的类不会总是那么简单,而且很多事情可能会出错,因此,您最好知道哪里出了问题,哪里出了问题。

我将这样设置,在这种情况下,putList是否有责任通过返回布尔值来告诉您尝试放置的列表是否被接受?我认为不是,这是因为putList应该只处理替换列表指针的单个模块,而不返回任何内容

如果您确实想知道条件是否被授予(或者对于可能发生的任何异常行为),请使用异常。然后,在使用putList时(使用try-catch块),只需将这些异常捕获到main中,然后做任何您想做的事情

例如:

public void putList(List<Integer> barList) throws new MyListException{
    if (this.fooList == null) {
        this.fooList = barList;
    } else {
        throw new MyListException("The pointer of fooList can not be changed because the fooList is not null");
    }
}

public class MyListException extends Exception {
    public MyListException(String s) {
        super(s);
    }
}
最后,由于我不太清楚“在没有操作的情况下不是null”是什么意思,我想说:当您使用
putList(…)
并且条件未被授予时,它不会返回
null
,而是什么也不做。但是,在这种情况下,最好使用我已经演示过的异常(在第一个示例中),这样您就可以知道在期望putList()实际替换指针的情况下发生了什么错误。这是因为你不会总是有机会花时间搜索代码来理解哪里出了问题。这对于您提供的代码示例来说并不重要,因为它很简单,但是如果您有一个更复杂的putList,并且可能会出现多个问题,该怎么办


总的来说,我不能说putListWithBoolean()是否是一种不好的做法,因为这取决于它的使用方式(如java示例所示),而没有例外的putList()可以被认为是一种不好的做法,因为您的类不会总是那么简单,而且很多事情可能会出错,因此,您最好知道哪里出了问题,哪里出了问题。

我将这样设置,在这种情况下,putList是否有责任通过返回布尔值来告诉您尝试放置的列表是否被接受?我认为不是,这是因为putList应该只处理替换列表指针的单个模块,而不返回任何内容

如果您确实想知道条件是否被授予(或者对于可能发生的任何异常行为),请使用异常。然后,在使用putList时(使用try-catch块),只需将这些异常捕获到main中,然后做任何您想做的事情

例如:

public void putList(List<Integer> barList) throws new MyListException{
    if (this.fooList == null) {
        this.fooList = barList;
    } else {
        throw new MyListException("The pointer of fooList can not be changed because the fooList is not null");
    }
}

public class MyListException extends Exception {
    public MyListException(String s) {
        super(s);
    }
}
最后,由于我不太清楚“在没有操作的情况下不是null”是什么意思,我想说:当您使用
putList(…)
并且条件未被授予时,它不会返回
null
,而是什么也不做。但是,在这种情况下,最好使用我已经演示过的异常(在第一个示例中),这样您就可以知道在期望putList()实际替换指针的情况下发生了什么错误。这是因为你不会总是有机会花时间搜索代码来理解哪里出了问题。这对于您提供的代码示例来说并不重要,因为它很简单,但是如果您有一个更复杂的putList,并且可能会出现多个问题,该怎么办

总的来说,我是c