views:

166

answers:

7

Why can't the conditional operator be used as a statement?

I would like to do something like:

boolean isXyz = ...;
...
isXyz ? doXyz() : doAbc();

where doXyz and doAbc are return void.

Note that this is not the same as other operators, for example doXyz() + doAbc() intrinsically needs that doXyz and doAbc return a number-like something to operate (or strings to concatenate, or whatever, but the point is that + actually needs values to operate on).

Is there something deep or is it just an arbitrary decision.

Note: I come from the Java world, but I would like to know if this is possible in your favourite programming language.

+1  A: 

Because it would reduce readability and introduce a potential for errors.

Languages offer means of doing what you wish by using the keyword "if".

// Is not much longer than the line below
// but significantly more transparent
if (isXyz) doXyz() else doAbc();

isXyz ? doXyz() : doAbc();

A statement is supposed to just perform some operations.

A conditional operator is meant to return a value.

Developer Art
It's none of the language's business to force programmer into what *some* consider more readable. I'd go for the `if` way myself (and I do, even though my "native" `C++` does allow it), but the decision is *mine*.
Michael Krelin - hacker
@hacker: Readability and reduced potential for errors is exactly why Microsoft team split the "reference" keyword of C# into "ref" and "out". They took that decision and I support it.
Developer Art
New in town, you have a freedom to support whatever view or readability. But it does not necessarily coincides with mine and I don't want to be forced into your (or Microsoft's view). This is a general statement, has nothing to do with any of the examples above.
Michael Krelin - hacker
A: 

Wouldn't this be exactly the same as the if statement?

if (isXyz) doXyz(); else doAbc();

Some languages do allow you to use the conditional operator as a statement. Perl comes to mind.

Greg Hewgill
Is there something that is not allowed in Perl :) ?
flybywire
+2  A: 

What would be the point? Why not just use an if statement (which, in my opinion, looks cleaner)?

Michael Foukarakis
+5  A: 

C and C++ do allow such constructs. As long as doXyz() and doAbc() return the same type. Including void.

Michael Krelin - hacker
+1  A: 

As a novelty, mIRCscripting allows you to do this

alias canI? {
   $iif($1 == 1,doThis,doThat)
}
alias doThis echo -a this can.
alias doThat echo -a that can.

calling it with /canI? 1 will echo this can. calling it with /canI? 2 will echo that can.

Martijn Laarman
A: 

An expression, including a conditional expression, may be used on its own as a statement in Java and many other languages (I'd go as far as to say most currently-popular languages).

It's specifically ‘void-returning’ in Java that's the issue here, not anything to do with conditionals. It's sometimes considered bad taste to hide active (non-idempotent; with side-effects) code inside an expression, and active functions often return void. So because Java is a prescriptive language, it disallows using a void function in an expression. Many other languages are more permissive and will allow it.

You could get around it by having doAbc and doXyz return something — zero, a boolean, anything: it doesn't matter as long as they're the same type for both, the result will be thrown away in an ExpressionStatement. But I don't really know why you'd want to; as others have said, this is indeed a case where doing it in an expression is in poor taste and largely pointless.

bobince
A: 

I think your question is the wrong way round.... the conditional operator was added because the "IF THEN" statement couldn't be used as an evaluation statement.

In my option you should only use the conditional operator when conditionally evaluating as it is inherently less clear than using "IF THEN" constructs when purely implementing a condition.

Conditional operators cannot typically contain blocks of multiple instructions on each condition result, "IF THEN" can.

Tony Lambert