Conditional statement in if-statement?

I wrote the following if-statement in Java:

if(methodName.equals("set" + this.name) ||
    isBoolean() ? methodName.equals("is" + this.name) :
                  methodName.equals("get" + this.name)) {
    ...
}

Is it good practice to write such expressions in ifto separate state from condition? And can this expression be simplified?

+5
source share
4 answers

I would change it to

if (methodName.equals("set" + this.name)
 || methodName.equals( (isBoolean() ? "is" : "get") + this.name)) {
    ...
}
+8
source

Is this a good practice? Well, if it makes reading easier. This makes reading easier if (1) it does, and (2) the look of the person who will be embarrassed by it does not read it. Who is going to read it?

+2
source

- ?

if (methodName.equals("set" + this.name)
    || methodName.equals("get" + this.name)
    || (isBoolean() && methodName.equals("is" + this.name))) {
    ...
}

, , , , . , isBoolean ( 1, 2 4 , 1 3, / , , , ).

" ?" :

.

, , .

if. .

Notice that I have included parentheses around the expression containing '& &' for readability. They are not needed because it x && yis rated before m || n.

If you decide to use it is up to you, but I try to avoid it for readability.

+2
source

I would be inclined to change it to

if (methodName.equals(setterForThis())
   || methodName.equals(getterForThis())) {
    ...
}

with some features extracted:

private String setterForThis() {
   return "set" + this.name;
}

private String getterForThis() {
   return (isBoolean() ? "is" : "get") + this.name;
}

It's more, of course, but I still don't play golf.

+2
source

All Articles