Home > front end >  Validating combination of values in constructor
Validating combination of values in constructor

Time:07-27

I'm trying to make a class Box<T>. It should have two public immutable(val) properties, isContentMandatory: Boolean, content: T?. This is which combination of values I want to accept:

isContentMandatory content Should allow?
false null YES
false non-null YES
true null NO
true non-null YES

I want to make sure that the constructor(s) I provide won't allow the illegal state of an object. Also, I want multiple constructors(or use default values) so that creation of an object is straight-forward for the client. Following are examples of instantiations:

Box()                        // OK -> isContentMandatory = false, content = null
Box("some-content")          // OK -> isContentMandatory = false, content = "some-content"
Box(false, "some-content")   // OK -> isContentMandatory = false, content = "some-content"
Box(true, "some-content")    // OK -> isContentMandatory = true, content = "some-content"
Box(true, null)              // DON'T ALLOW
Box(true)                    // DON'T ALLOW

The DON'T ALLOWs from above should preferably be forbidden at compile-time(no constructor available for that combination) if it's possible. Otherwise, fail with the exception during creation.

I'm coming from Java world so all those primary/secondary constructors, default values and etc. are a bit fuzzy so please explain along with a solution. I'm also open to different class design if it supports the same business logic.

EDIT: this is how it would look in Java.

public class Box<T> {

    private final boolean isContentMandatory;
    private final T content;

    public Box() {
        this(null);
    }

    public Box(T content) {
        this(false, content);
    }
    
    public Box(boolean isContentMandatory, T content) {
        if (isContentMandatory && content == null) {
            throw new IllegalArgumentException("Invalid combination of parameters");
        }
        this.isContentMandatory = isContentMandatory;
        this.content = content;
    }

    ...getters...
}

CodePudding user response:

Whether or not this is a good approach for the problem is hard to answer without actual domain knowledge of your use case, but it feels to me like it makes little sense that you would make a single class to model those cases which carries around an (otherwise pointless?) boolean to separate the cases.

You could just have 2 classes BoxWithOptionalcontent<T?> and BoxWithContent<T> and you wouldn't need more than the default constructor for either afaict.

sealed interface Box<T: Any?> {
    abstract val content: T?
}
data class BoxWithContent<T: Any>(override val content: T): Box<T>
data class BoxWithOptionalContent<T: Any?>(override val content: T? = null): Box<T?>

This shouldn't change much on the initialization site, on the side of the usage you will probably need to add a case statement to decide which case it is and handle appropriately. But you probably already have some similar logic there anyway, and this will probably be a bit more typesafe and readable.

CodePudding user response:

From your Java approach it seems you just want a runtime check, not really preventing bad calls. Just having default arguments and one init block to validate should work:

class Box(val content: T? = null, val isContentMandatory: Boolean = false)
     init {
         if(content == null && isContentMandatory)
             throw RuntimeException("Cannot have no content if content is mandatory")
     }
}

Any content given though as a first argument will be valid, so to make this break you have to try harder using Box(isContentMandatory=true) or Box(null, true) explicitly.

  • Related