5

I've written the following if-statement in Java:

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

Is this a good practice to write such expressions in if, to separate state from condition? And can this expression be simplified?

Pindatjuh
  • 10,550
  • 1
  • 41
  • 68
  • Are you aware of existing Javabean mapping tools? There are **a lot** :) – BalusC May 12 '10 at 23:35
  • I don't have anything particularly substantial to contribute, but even as someone who loves the ternary operator, I must note that some people strongly believe it should be used very rarely. Those people would probably pooh-pooh your usage. – David M May 12 '10 at 23:36
  • @BalusC: Great guess of my JavaBean activity. I'm implementing it just for the hobby and personal use, cleaning up syntax inside Beans by just `super.property(newValue)` in each setter. ;-) but I am aware of them, thanks for mentioning. – Pindatjuh May 12 '10 at 23:37
  • 3
    Hobbying is always good :) Don't get scared when you come to a point that you realize that a similar and widely used 3rd party API has done its job tremendously good ;) – BalusC May 12 '10 at 23:48
  • ternary if operator to the rescue:) – Gabriel Ščerbák May 13 '10 at 00:24
  • 2
    No one's going to point out that it's the *conditional* operator which just happens to be the only *ternary* operator in Java??? – Lawrence Dol May 13 '10 at 00:51
  • @Software Monkey: I have to restrain myself every time. :) – Bill the Lizard May 13 '10 at 00:56

4 Answers4

8

I would change it to

if (methodName.equals("set" + this.name)
 || methodName.equals( (isBoolean() ? "is" : "get") + this.name)) {
    ...
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Is it good practice? It's good if it makes it easier to read. It makes it easier to read if (1) it does and (2) the sort of person who'd be confused by it won't be reading it. Who's going to read it?

amara
  • 2,216
  • 2
  • 20
  • 28
2

Wouldn't something like the following work?

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

It's more readable than the way in which you used the ternary operator and certainly easier to understand. It also has the advantage that it can avoid an unnecessary method call to the isBoolean method (it has either 1, 2 or 4 method calls whereas yours always has either 1 or 3; the performance gain/loss is probably too minute to notice).

Also there's a similar question here titled "Is this a reasonable use of the ternary operator?" One user had the following to say:

The ternary operator is meant to return a value.

IMO, it should not mutate state, and the return value should be used.

In the other case, use if statements. If statements are meant to execute code blocs.

Do note that I included parentheses around the expression containing '&&' for readability. They aren't necessary because x && y is evaluated before m || n.

Whether you choose to use it is up to you, but I tend to avoid it in favour of readability.

Community
  • 1
  • 1
Dustin
  • 1,956
  • 14
  • 14
  • Your rewrite is very practical, as the method "getSomeBooleanProperty" will also pass. Though, it's not as readable as SLaks's (subjective). – Pindatjuh May 13 '10 at 00:10
2

I would be inclined to change it to

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

with some functions extracted:

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

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

It's longer of course, but I'm not really into golf anyway.

Don Roby
  • 40,677
  • 6
  • 91
  • 113