|
The compiler will optimize an If/Then/Else in a manner that makes it impossible to accurately predict which is more efficiently executed. See Code Complete 2nd edition for lots and lots of examples of this. Additionally, since a conditional evaluation is short-circuited, the construction of the conditional itself probably has significantly more impact on efficiency.
Thus, my view is that the conditional should be coded in a manner that optimizes clarity, readability, and maintainability of the code.
Tom
|
|
|
|
|
Compiler optimization of the code is related to static code analysis. While probabilty to hit (most of time); which block of code; programmer can better predict.
|
|
|
|
|
This is exactly what I do as well.
|
|
|
|
|
After all MS has a tool that lets you click, drag, and not need to think about such trivalities.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
I tend to like to code the if so that it diverts the code into a side condition and allows the flow straight through for the most common condition.
|
|
|
|
|
I agree, especially for early exiting a function. It reduces the levels of indentation.
|
|
|
|
|
We Don't Need No Stinkin' "THEN".
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to stop bothering them and just go away?" - Balboos HaGadol
"It's a sad state of affairs, indeed, when you start reading my tag lines for some sort of enlightenment. Sadder still, if that's where you need to find it." - Balboos HaGadol
|
|
|
|
|
|
Personally, I try to avoid putting statements in my conditionals...
if (expression is true) then...
if (expression is false) then...
are both acceptable though.
|
|
|
|
|
Which leaves you with:
<br />
if()<br />
;<br />
else()<br />
;
Saves some typing, but I would personally tire of it after a bit. You might want to, at least, use simple statements in your conditionals.
Just so that they actually (1) make up their mind about something, and (2) do something after they decide.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"How do you find out if you're unwanted if everyone you try to ask tells you to stop bothering them and just go away?" - Balboos HaGadol
"It's a sad state of affairs, indeed, when you start reading my tag lines for some sort of enlightenment. Sadder still, if that's where you need to find it." - Balboos HaGadol
|
|
|
|
|
No, according to most languages (Ruby and functional languages being notable exceptions) an expression is something that returns a value - a boolean expression, for example, returns true of false. While a statement is almost anything: a declaration, an if statement, a while statement.
Placing such a beast within an if's conditional expression yields...
if (if (x==3) y = 4; ) y = 5;
Which really doesn't make much sense.
|
|
|
|
|
I was taught and have taught that your code should test for the most likely condition first, thus avoiding unneccessary excution of code. If the False state is the more likely, then test for false and avoid testing for True - use the else clause for that. The case should be never used to test for just true/false. Speaking of Case, test in descending order of expected frequency of occurance.
Been doin' this for too long!
|
|
|
|
|
|
if always tests for true , never false .
Whether I do if ( x == 0 ) or if ( x != 0 ) I'm still testing for true.
|
|
|
|
|
|
PIEBALDconsult wrote: if always tests for true
But the survey doesn't ask how if statement works. it asks how do you use it:
In which order do you test conditionals?
"In the end it's a little boy expressing himself." Yanni
|
|
|
|
|
Quite right Sir.
Regards,
Rob Philpott.
|
|
|
|
|
My point is that (some of) the provided answers make no sense and I can therefore not give a meaningful answer.
|
|
|
|
|
PIEBALDconsult wrote: My point is that (some of) the provided answers make no sense and I can therefore not give a meaningful answer.
Your point is understood and my point is that if someone is clever enough to understand that even not in a conditional statement is compared to true, should easily understand the point of this survey question which is, again, what you use.
Actually this if will probably be translated to some jumps after some cmp instructions which in fact are not compared to neither true nor false but to 1 or 0.
Also one might say that cmp is not an instruction; it's nothing more than a number in binary form or even not that but some high or low voltages waiting for being processed in next CPU cycle. Oh wait they're not waiting there just a flipflop is... The survey question isn't talking about any of these. It's what you type in your programming editor. Based on the question I believe choices are relevant and make sense.
Just my 2 cents.
"In the end it's a little boy expressing himself." Yanni
|
|
|
|
|
PIEBALDconsult wrote: if ( x != 0 ) I'm still testing for true.
You're still testing for false. It'll catch if the false condition is true, but you're testing for false with that.
|
|
|
|
|
x could be a count or something.
|
|
|
|
|
PIEBALDconsult wrote: x could be a count or something.
Um, what? You're still testing for a false condition. The does not equal part is your false test. The value of X is really irrelevant in the scope of your point.
|
|
|
|
|
Me: Say prithy, be there no holes in my flagon?
He: Aye, sir, there be no holes in thy flagon.
|
|
|
|
|
always check for true first is the best way to code (example)
if true then (do something)
if false then( do something else)
has been my way of coding in that order
so really the question is misleading.
|
|
|
|
|
Byte Magazine (semi-lost and lamented) once carried an article by John McCarthy (sic?) on Logic Gem. He showed how the production of if then elses could be done with a logic table and speed coding and reduce errors. Logic Gem is still available. I wouldn't code complex flow of control without it. Generates code in readable languages and c varieties too.
I have no commercial or personal relationship with Logic Gem, Byte or John McCarthy. And, my opinions are my personal ones.
|
|
|
|