HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Class that represents a conditional expression

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
expressionconditionalthatclassrepresents

Problem

I have a class with which I want to persist a conditional expression:

boolean x = a [GT] i


I am only concerned with the boolean outcome of that comparison. 'a' '[GT]' and 'i' are supplied as arguments at runtime.

public class RuleClauseCondition {
    RuleClauseOperator operator;
    Double             threshold;
    Double             operand;

    private RuleClauseCondition()
    {
    }

    public RuleClauseCondition(RuleClauseOperator operator, Double threshold, Double operand) {
        this.operator = operator;
        this.threshold = threshold;
        this.operand = operand;
    }

    boolean isValid() {
        return operator.isValid(operand, threshold);
    }

}

public enum RuleClauseOperator{
    EQ {
        @Override
        public boolean isValid(Double inputValue, Double thresholdValue) {
            return inputValue.equals(thresholdValue);
        }

        @Override
        public String toString() {
            return " == ";
        }
    }, LT {
        @Override
        public boolean isValid(Double inputValue, Double thresholdValue) {
            return inputValue  thresholdValue;
        }

        @Override
        public String toString() {
            return " > ";
        }
    }


I am looking for better names to represent the left and right side of the operator as well as the class name.

Are current names intuitive and self documenting, or do I need to improve them?

Solution

Well, being the class a binary comparator, and being that the enum contains comparison operators, I would suggest to call them BinaryComparator and ComparisonOperator. Unless you have a naming convention or something else that would make those names not usable, obviously.

I would suggest naming classes by what they represent, if possible it should be a name not related to the context (if such names don't make the code less readable or less understandable).

Context

StackExchange Code Review Q#87764, answer score: 5

Revisions (0)

No revisions yet.