Why 'condition && statement()' is not popular in Java world?

Grzegorz Gajos :

I've been doing Java development for almost a decade now and I've never seen an expression like [2] (see below). I have seen this for the first time in a JavaScript library where it was used a lot.

1.

if(isValid) {
   statement();
}

2.

isValid && statement();

The outcome of [1] and [2] is identical as the statement() is not going to be evaluated if isValid == false.

  • Why syntax like [2] not popular? I mean, I literally never encounter this before in any Java project I've seen.
  • Are there any implications of approach [2]?
  • Is it considered to be harmful?
  • Are there any surprises I might come along using this as the alternative for simple if statements?
  • Does it decrease readability?

Edit:

I was testing this in Groovy console. I guess it invalidates this question quite much. Thanks for your responses anyways.

Thomas :

if( isValid && statement() ) only works if statement() returns a boolean otherwise the condition wouldn't be compilable. Doing that might not be considered good design (depends on what statement actually does) and might reduce readability or your ability to use the return value of statement().

Besides that just isValid && statement(); isn't supported by the language - and don't make the mistake to compare a language like Java to a language like JavaScript, they follow different paradigms.

You could do something like boolean b = isValid && statement(); but you'd normally only do that if you want to use the value of b somehow.

Edit: As requested by KRyan I'll also add a paragraph on the meaning of a && b and what it is used for in Java as well as JavaScript (although I'm no expert here).

Java

In general you can combine two boolean expressions using & (and) or | (or). Thus true & true will result in true as does true | false, but true & false will result in falseetc.

The problem with the single character operators is that every argument is evaluated even if you already know the result. As an example when you see false & ... you already know that the result is false when looking at the first argument and any other arguments won't make a difference.

Now if you have an expression that calls some methods which return a boolean value you might not want to execute those methods if they aren't needed, e.g. when the methods themselves are quite expensive. As an example consider needsCheckWebsite & isValueOnWebsite( "test" ). isValueOnWebsite( "test" ) might take a while and if needsCheckWebsite was false you'd not want to execute that method.

To fix that you can use the double-character operators && and ||, aka short-circuit operators. That means that if you write needsCheckWebsite && isValueOnWebsite( "test" ) and if needsCheckWebsite is false, the expensive execution of isValueOnWebsite( "test" ) won't happen since we already know that the result doesn't change a thing.

Hence isValid && statement() would mean that statement() should only be executed if isValid.

JavaScript

AFAIK in JavaScript the short-circuit operators work in a similar way in that it evaluates the first argument and if the result is still unknown the second argument is evaluated and returned. However, since JavaScript is a dynamically typed language (in constrast to Java which is statically typed) you can mix different types, e.g. var value = null || "hello" which would result in value = "hello".

This allows the developer to use && and || for things other than boolean logic, like supplying a default value, which is not possible in Java due to the static typing (in Java you could use the ternary operator ? for that, e.g. String s = input != null ? input : defaultValue but as you can see that doesn't look as elegant as JavaScript's var s = input || defaultValue).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=455406&siteId=1