w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Is it a good practice to return a boolean/value instead of null in case of no-ops?

I will set it this way, is the responsibillity of the putList in this case to tell you whether the list you try to put is accepted or not by returning a boolean? In my opinion no, this is because the putList should handle only the single module of replacing the list pointer and not return something.

If you actually want to know whether the conditions are granted or not(or for any ubnormal behavior that could occur), use Exceptions. Then simply catch those exceptions into your main when the putList is used(with try-catch blocks) and do whatever you wish.

For example:

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);
    }
}

On the other hand, the case that the method would return a true/false is when it should actually handle that module and determine whether a list will be accepted or not by testing the condition. Thus, the method name will be 'isListAccepted()' (a parameter is not needed since it doesnt play any role). However, in java it is noticed (i.e See LinkedList click here) that sometimes methods like public boolean add(E e) returns true/false. This is because those methods are implemented under the collection interface and there are some preconditions in the way the collection works. Additionally, in this case as well oracle documentation says:

public boolean add(E e) Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false).

So based on that, I believe that these operations, handle the module of telling you if your collection has changed or not after their use rather than adding the element(even if they do in some cases).

An example:

private boolean isListAccepted() {
    return this.fooList == null;
}

Finally, since I am not sure too about what do you mean with "instead of null in case of no-operations", I am gonna say that: when you use the putList(..) and the condition is not granted, it doesnt return null, but rather it is doing nothing. However, it is always preferable in this case as well to use Exceptions as I already demonstrate(in the first example) so that you will know what went wrong in case you expect the putList() to actually replace the pointer. And this is because you wont always have the chance to spend time searching the code to understand what went wrong. This is not really important with the code example you provided since it is simple but what if you had a putList that was more complex and multiple things could go wrong ?

Overall, i can not say if putListWithBoolean() is a bad practice because it depends on the way it is used(as shown with the java example), while the putList() without exceptions can be considered as a bad practice, because your classes wont be that simple always, and many things could go wrong, so you better know what went wrong and where.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.