3

I have the following code delay = (delay>200) ? delay : 200;
Java issues a warning message Can be replaced with 'Math.max' call for this.
Here I see that Math.max(a, b) is actually the same as (a > b) ? a : b so ternary operator is not worse than Math.max
So why Java issues this warning message if there are no advantages replacing the ternary operator by Math.max method call?

Prophet
  • 32,350
  • 22
  • 54
  • 79

3 Answers3

9

I doubt that this is a real compiler warning, probably some IDE inspection/warning.

Nonetheless, you are correct, there are no hard technical reasons to prefer one over the other.

But: from the point of a human reader, using Math.max() has one major advantage: it is easier to read and understand. That simple.

Besides: do not duplicate code unless you have to.

Always remember: you write your code for your human readers. Compilers accept anything that is syntactically correct. But for your human readers, there is a difference between a condition and an assignment vs a very telling "take the maximum of two numbers".

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 2
    The "soft" technical reason for preferring `Math.max()` is that it's an intrinsic, however [YMMV](https://bugs.openjdk.java.net/browse/JDK-8039104). – Kayaman Feb 25 '21 at 09:10
  • 2
    @Kayaman Honestly: when people have to worry about performance on a level that such bugs are a real problem for them, then maybe, just maybe, Java isnt the right answer for them anyway. But interesting point. – GhostCat Feb 25 '21 at 09:15
  • Yes, that was badly worded. It's not a reason to prefer `Math.max()`, it's a positive side effect of using it. – Kayaman Feb 25 '21 at 09:29
3

Math.max(a, b) is more readable than the tenary statement because:

  • the value 200 does not need to be repeated.
  • there is no need to write and understand >

In general, the ternary is more powerful because it lets you do things like this:

    delay = (delay>200) ? 200 : delay;
    delay = (delay<200) ? delay : 200;
    delay = (delay>200) ? delay: 300;

The reader of your code needs to understand which of those things you are actually doing. It takes time to parse it and understand it is a simple max().

The max shows your intention more clearly.

WW.
  • 23,793
  • 13
  • 94
  • 121
0

In addition to the existing answers, there can be a performance advantage if the lower limit (in your case, 200) is not a constant but a derived value:

delay = (delay > readLimitFromFile()) ? delay : readLimitFromFile();

This could end up doing 2 expensive disk-read operations, when one operation would be sufficient. Using Math.max:

delay = Math.max(delay, readLimitFromFile());

would use only one disk-read operation.

k314159
  • 5,051
  • 10
  • 32
  • But this answer relies on the developer to not simply use a variable for it in advance. – akuzminykh Feb 25 '21 at 09:11
  • Well. You can always intentionally make code "stupid" to "prove" a point, can't you. I just dont find that very convincing. – GhostCat Feb 25 '21 at 09:29