by schindlabua · 9y ago · 0 comments
result = boolean_flag
? do_this()
: do_that();
I think it's less noisy and nicer to look at then an if-else, provided the statements aren't too long. God forbid multiple side effects though, even in languages with less complicated sequencing rules than C/C++.So:
auto result = boolean_flag ?
compute_property() :
compute_another_property();
if (boolean_flag) {
do_this();
} else {
do_that();
}Seriously though, there's nothing in ternary operator that says it should only be used for side-effect free code. Especially in C++ (or Java), which doesn't even differentiate between stateless and stateful code on the language level.
IMO ternary is a perfect solution for the case where you want to assign or return a different value based on a boolean condition. Whether or not the code in branches is side-effect free is irrelevant.
Personally, where I try to avoid using ternary operator, is places where I don't care about returned value. So "yes" for:
auto foo = [condition] ? possibly_stateful_then() : possibly_stateful_else();
no for: [condition] ? then_statement() : else_statement();My primary language, Object Pascal, even goes to the length of naming functions (return values) and procedures (don't return values) differently. It really helps to focus the mind on the purpose of each: one almost always expects procedures to do something and, most likely, modify something that may introduce side effects. Functions are almost always side-effect-free in "decent" Object Pascal code.
Using ternaries only when it is free of side-effect can both helo avoid a class of unnecessary errors, and add meaningful semantic information that some code is, or should be, side-effect free.
It would be great if the compiler(s) in various languages supported enforcing it, but sometimes you take whatever you get.
As i asked below: Why though?
Personally i dislike the latter form because it requires me to spend extra time reading what is boilerplate that is functionally identical to a ternary.
Edit: Not impressed by the down-voting of an earnest question of someone trying to learn more about how other people see things, HN.
If-else is the only option if you want to have multiple statements in a {} block, instead of only one function call.
So it seems more consistent to use ternary for expressions and if-else for statement control flow.
I'm sorry, but i really don't follow your logic, can you try to explain it more clearly?
boolean_flag
? do_this()
: do_that();
It seems so terse as to be impossible to misinterpret.The only possibly feasible way i see for it doing something unexpected would be if it were the last statement in a function in a language that implicitly returns the value returned by the last statement in a function; but users of such languages usually are aware that can happen.
To me it works in short expressions that do return a value so number_of_things = some_type ? 5 : 1
x = x ? (n ? m : w) : (b ? c : x);
This doesnt seem too bad with variables, but now imagine this.
int killMe = Factory.Omg ? (isFuckedUpped ? Leet.YES : Leet.NO) : (youGetTheIdea ? yep : nope);
int killMe = Factory.Omg
? (isFuckedUpped ? Leet.YES : Leet.NO)
: (youGetTheIdea ? yep : nope);
it doesn't look bad IMO. Definitely more readable than if-else alternative.I'd first look for a way to try and avoid the whole situation, if possible.
After that, what I feel is more readable depends on what formatting the language allows, length of identifier names, what the subexpressions look like, etc.
Then, after careful consideration, I'll just follow any style guide and thus prefer if/else blocks, especially if a ternary would silently discard its result.
result = if boolean_flag
then do_this()
else do_that();
then I'm sure no one would find it as bad as they do now. The way you formatted the ternary with line-breaks helps a lot, too, I think! let result = if boolean_flag {
do_this()
} else {
do_that()
};
Rust actually doesn’t have a ternary operator. z <- if (x > y) 5 else 7
And of course this is trivial in Lisp: (setf z (> x y) 5 7)What you meant is:
(setf z (if (> x y) 5 7))
A nicer thing that I like about Lisp's "everything is an expression" is something that is considered as code smell by some: (setf foo (or var (compute-default-value) +some-default-constant+))
This works in Common Lisp, where the only value considered logically false is NIL, because or will short-circuit (as expected), returning not T, but the value of the first logically true result.So e.g.
(let ((a nil) (b 2) (c :foo))
(or a b c))
returns 2. result = if boolean_flag do_this(); else do_that(); end
Though it does have the ternary ?: operator too. result = do_this() if boolean_flag else do_that()