简体   繁体   中英

Variable name of opposite of negation

Maybe this is a bit of a silly question, but I cannot get a proper variable name. Currently I got it the other way around, but it doesn't make as much sense in my opinion.

package dao.constraint;

public class Constraint<T> {
    public Operation operation;
    public boolean negated;
    public T data;
    public String field;

    public Constraint(final Operation operation, final boolean negated, final T data, final String field) {
        this.operation = operation;
        this.negated = negated;
        this.data = data;
        this.field = field;
    }
}

Background information: This has to do with constraints that will be used in a SQL select query. For example new Constraint<String>(Operation.IS, false, "me", "username"); will be part of a query where the username is equal to "me" . And new Constraint<String>(Operation.IS, true, "me", "username"); would be part of a query where the username is not equal to "me" .

As you can see the naming is confusing and I would want to turn around the booleans for both logic and future readability, however how is the opposite of negation/negated called?

I could call it something like isNotNegated , but that doesn't seem a very nice name to me.

More background: In SQL code it will be literally implemented as WHERE username = "me" respectively WHERE NOT(username = "me") so hence why I choose to call it negated here temporarily.

Regards.

To get rid of the boolean flag I would refactor the code by introducing two factory methods and making the original constructor private.

Admittedly I don't do Java stuff regularly, so forgive the probably wrong syntax :)

private Constraint(final Operation operation, final boolean negated, final T data, final String field) {
    this.operation = operation;
    this.negated = negated;
    this.data = data;
    this.field = field;
}

public static<T> Constraint<T> createConstraint(final Operation operation, final T data, final String field) {
    return new Constraint<T>(operation, false, data, field);
}

public static<T> Constraint<T> createNegativeConstraint(final Operation operation, final T data, final String field) {
    return new Constraint<T>(operation, true, data, field);
}

To use:

Constraint<String> foo = Constraint.createConstraint(Operation.IN, "hello", "field");

I would really suggest to make Constraint an interface with various implementations like And, Or, Not, In, whatever you need as suggested in your other question of improving the builder. The reasons for this suggestion are * that the negation of a constraint will never change for that constraint, so why not putting this decision into a class. Otherwise the constraint is some kind of "tagged" class which is a clear sign of a faulty hierarchy. * introducing these different implementations allows to easily compose constraints and is thus way more flexible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM