What is this line of C code doing ?

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
What is this line of code doing ?

Code:
lim=prev_calculated==0?rates_total-N_Bar_Fr-10:N_Bar_Fr+2;

lim is an int
prev_calculated is a const int
rates_total is a const int
N_Bar_Fr is an int

I haven't used C in years so am struggling to figure out what the above mess is short hand for. Online code beautifiers do not seem to change it.
I know it is shorthand for a single line if statement.
 

SteveGrabowski

Diamond Member
Oct 20, 2014
7,131
6,001
136
Assuming lim has been declared previously it's

Code:
if (prev_calculated == 0) {
   lim = rates_total - N_Bar_Fr - 10;
} else {
   lim = N_Bar_Fr + 2;
}

Though the line would be better written as

Code:
if (prev_calculated) {
   lim = N_Bar_Fr + 2;
} else {
   lim = rates_total - N_Bar_Fr - 10;
}

or

Code:
lim = prev_calculated? N_Bar_Fr + 2 : rates_total - N_Base_Fr - 10;

whichever you find easier to read
 

SteveGrabowski

Diamond Member
Oct 20, 2014
7,131
6,001
136
Basically
Code:
somevar = some_boolean? somevalue : othervalue;
is shorthand for

Code:
if (some_boolean) {
   somevar = somevalue;
} else {
   somevar = othervalue;
}
 

purbeast0

No Lifer
Sep 13, 2001
52,933
5,803
126
Whoever wrote that code needs to be fired. That is one of the worst formatted lines of code I've ever seen, spacing and variable names included.
 
Reactions: NTMBK and Cogman

mxnerd

Diamond Member
Jul 6, 2007
6,799
1,101
126
Looks more likely a C language instructor teaching students C's syntax .
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Whoever wrote that code needs to be fired. That is one of the worst formatted lines of code I've ever seen, spacing and variable names included.

Yeah, it is really pretty bad.

OP. Generally speaking, ternary operators should only pick between two values. They should not pick between two value modifications. So

Code:
 val x = isModified ? modified : unmodified

That is an ok use of the ternary operator. It is very clear that "if isModified is true then modified is the returned value"

What you have here is something that is really hard to parse.

It is semi-legible but could be a lot better.

First off, there is almost no reason now-a-days not to write out full variable names. write limit. Write previous_value (or previousValue). Where this totally breaks down is
"N_Bar_Fr"... What? I can't even guess at what the author means by that variable name. Why are you adding 2 subtracting 10 from it?

This code would be much clearer if that variable had a more descriptive name. Further, replacing 2 and 10 with something more descriptive... even if it something like "NEGATIVE_FUDGE_FACTOR" would make this much more maintainable.
 

Bulldog13

Golden Member
Jul 18, 2002
1,655
1
81
Whoever wrote that code needs to be fired. That is one of the worst formatted lines of code I've ever seen, spacing and variable names included.

LOL. Now I don't feel bad for staring at it for 5 minutes and thinking "Screw it, throwing it on the forum."
 

purbeast0

No Lifer
Sep 13, 2001
52,933
5,803
126
LOL. Now I don't feel bad for staring at it for 5 minutes and thinking "Screw it, throwing it on the forum."
Yeah, I mean, I would at least write it like this to be more readable.

Code:
lim = prev_calculated == 0 ? rates_total - N_Bar_Fr - 10 : N_Bar_Fr + 2;

But even then the variable names are terribly named too. This would be a little cleaner, without knowing the names.

Code:
limit = previouslyCalculated == 0 ? totalRates - foobar - 10 : foobar + 2;
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |