Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Javascript world (especially in React apps) I've seen expressions like [2]. However, in Java this is not so popular.

1.

if(isValid) {
   statement();
}
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.

like image 591
Grzegorz Gajos Avatar asked Jul 22 '16 10:07

Grzegorz Gajos


People also ask

Why do we need to condition?

Conditioner is usually the second step to hair washing. While shampoo is formulated specifically to clean off sweat, dead skin cells, and hair products, conditioner makes hair softer and easier to manage. It also protects hair shafts from damage. Most shampoos use chemicals that are rough on hair follicles.

Is conditioner good for your hair?

Conditioners smooth and detangle the hair, which, therefore, helps reduce breakage and split ends — and that's precisely why New York City-based hairstylist Chuck Bass recommends conditioning every time you shampoo in order to add moisture back, as well as to soften and detangle.

Is conditioner better than shampoo?

Shampoos cleanse your hair and conditioners moisturize it. To keep your hair healthy and strong, it is always better to use both.


2 Answers

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).

like image 119
Thomas Avatar answered Oct 11 '22 14:10

Thomas


They are not interchangeable:

isValid && statement(); is an expression and not a statement. Also it would only work when your statement() method is of type boolean .

So you could only use this where an (predicate) expression is expected by the Java-language syntax.

opinion: If you would use it when meeting the above requirements, then i would indeed find that it "Does decrease readability". (as it can only be used in certain situations, plus by your own words ("doing Java development for an almost decade now and I've never seen") nobody is expecting it)

like image 45
raphaëλ Avatar answered Oct 11 '22 13:10

raphaëλ