See: Description
Class | Description |
---|---|
IntVariety<I> |
A tool for distinguishing cases that consist of multiple independent boolean decisions
related to an input of a particular type.
|
Variety<I,R> |
A tool for distinguishing cases that consist of multiple independent boolean decisions
related to an input of a particular type.
|
Variety.Stage<I> |
Represents a preliminary stage of instantiating a
Variety . |
Enum | Description |
---|---|
BitOrder |
Defines the bit-order to be used for converting a series of boolean results into an int value.
|
When decisions have to be made between several (more than two) possible results based on several independent criteria, confusing code often results. Example:
Given an Input parameter that offers three independent criteria ...
public interface Input { boolean isConditionOne(); boolean isConditionTwo(); boolean isConditionThree(); }
Now a choice should be made between five possible results {A, B, C, D, E}
based on the
criteria of the Input parameter ...
A classic normalized implementation might look something like this ...
public enum Result { A, B, C, D, E; public static Result map(final Input input) { if (input.isConditionOne()) { if (input.isConditionTwo()) { if (input.isConditionThree()) { return C; } else { return D; } } else { if (input.isConditionThree()) { return A; } else { return B; } } } else { if (input.isConditionTwo()) { if (input.isConditionThree()) { return B; } else { return C; } } else { if (input.isConditionThree()) { return E; } else { return A; } } } } }
In practice, individual branches of the decision cascade can often be combined, which makes the code a little shorter but hardly clearer. Each additional criterion would roughly double the required code of the decision cascade.
The Decision classes defined in this module allows a different implementation of the same decision cascade ...
public enum Result { A, B, C, D, E; private static final Variety<Input, Result> VARIETY = Variety.joined(Input::isConditionOne, Input::isConditionTwo, Input::isConditionThree) .replying(A, B, C, D, E, A, B, C); public static Result map(final Input input) { return VARIETY.apply(input); } }
The interested programmer can decide for himself whether this is better. In my opinion, maintainability have been improved. Readability depends largely on whether the concept has been understood.
Copyright © 2025 Andreas Kluge-Kaindl, Bremen (de). All rights reserved.