Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 343 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is this a reasonable use of the ternary operator?

#1
Are there any understanding / maintainability issues that result from code like

inVar1 == 0 ? NULL : v.push_back(inVar1);
inVar2 == 0 ? NULL : v.push_back(inVar2);

and so forth.

The possibly confusing idea is using the ternary operator for program flow rather than variable assignment, which is the usual explanation.

I haven't seen coding standards at work that address this usage, so while I'm comfortable doing this I'd like to find out if there is a good reason not to.
Reply

#2
I think this should be avoided. You could use a 1-line if statement in its place.

if(inVar1 != 0) v.push_back(inVar1);
Reply

#3
The ternary operator is meant to return a value.

IMO, it should not mutate state, and the return value should be used.

In the other case, use if statements. If statements are meant to execute code blocs.
Reply

#4
I think you would be better served in doing a proper if structure. I even prefer to always have braces with my if structures, in the event I have to add lines later to the conditional execution.

if (inVar != 0) {
v.push_back(inVar);
}
Reply

#5
Compilers these days will make an if as fast as a ternary operator.

You goal should be how easy is it for another software developer to read.

I vote for

if ( inVar != 0 )
{
v.push_back( inVar );
}

why the brackets...because one day you may want to put something else in there and the brackets are pre-done for you. Most editors these days will put them in anyway.
Reply

#6
While, in practice, I agree with the sentiments of those who discourage this type of writing (when reading, you have to do extra work to scan the expression for its side effects), I'd like to offer

!inVar1 ?: v.push_back(inVar1);
!inVar2 ?: v.push_back(inVar2);

...if you're going for obscure, that is. GCC allows `x ?: y` in place of `x ? x : y`. :-)
Reply

#7
The ternary is a good thing, and I generally promote it's usage.

What you're doing here however tarnishes it's credibility. It's shorter, yes, but it's needlessly complicated.
Reply

#8
As litb mentioned in the comments, this [isn't valid C++][1]. GCC, for example, will emit an error on this code:

error: `(&v)->std::vector<_Tp, _Alloc>::push_back [with _Tp = int, _Alloc =
std::allocator<int>](((const int&)((const int*)(&inVar1))))' has type `void'
and is not a throw-expression

However, that can be worked around by casting:

inVar1 == 0 ? (void)0 : v.push_back(inVar1);
inVar2 == 0 ? (void)0 : v.push_back(inVar2);

But at what cost? And for what purpose?

It's not like using the ternary operator here is any more concise than an if-statement in this situation:

inVar1 == 0 ? NULL : v.push_back(inVar1);
if(inVar1 != 0) v.push_back(inVar1);


[1]:

[To see links please register here]

Reply

#9
I use ternary operator when I need to call some function with conditional arguments - in this case it is better then `if`.

Compare:

printf("%s while executing SQL: %s",
is_sql_err() ? "Error" : "Warning", sql_msg());

with

if (is_sql_err())
printf("Error while executing SQL: %s", sql_msg());
else
printf("Warning while executing SQL: %s", sql_msg());

I find the former is more appealing. And it complies to [DRY][1] principle, unlike latter - you don't need to write two nearly identical lines.


[1]:

[To see links please register here]

Reply

#10
If you have multiple method invocations in one or both of the tenary arguments then its wrong. All lines of code regardless of what statement should be short and simple, ideally not compounded.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through