|
You should not write off the compiler so soon. A C++ compiler probably generates the same code for either statement. So you can have cosmetic and efficient.
|
|
|
|
|
In ASM/C/C++, efficiency and performance always outweigh readability. If a Padawan cannot read it, he is not ready to become a Jedi.
|
|
|
|
|
This looks like a script function not in C/C++. As for this example in VC++
void whatEver(int inVal) { <br />
<br />
if(inVal==0)<br />
inVal = internalDefault;<br />
<br />
inVal = (inVal==0)?internalDefault:inVal;<br />
}
See disassembly code in Debug build as simply:
; 16 :
; 17 : if(inVal==0)<br />
<br />
cmp DWORD PTR _inVal$[ebp], 0<br />
jne SHORT $LN2@whatEver<br />
<br />
; 18 : inVal = internalDefault;<br />
<br />
mov DWORD PTR _inVal$[ebp], 123 ; 0000007bH<br />
$LN2@whatEver:
While the other just
; 21 : inVal = (inVal==0)?internalDefault:inVal;<br />
<br />
cmp DWORD PTR _inVal$[ebp], 0<br />
jne SHORT $LN4@whatEver<br />
mov DWORD PTR tv66[ebp], 123 ; 0000007bH<br />
jmp SHORT $LN5@whatEver<br />
$LN4@whatEver:<br />
mov eax, DWORD PTR _inVal$[ebp]<br />
mov DWORD PTR tv66[ebp], eax<br />
$LN5@whatEver:<br />
mov ecx, DWORD PTR tv66[ebp]<br />
mov DWORD PTR _inVal$[ebp], ecx
But you can't see both in Release build because both optimized in compilation.
|
|
|
|
|
You are overthinking this.
First - it is script-like. The C++ was just a posting choice for formatting - apparently a poor choice on my part as many others also though I meant in the C++ context.
Second - this was really a question on preferences. Aside from your disassembly, the clear difference is one would (as written) always MOVe a value and the other only conditionally - both doing the same conditional test. The question is, for most applications, which would you rather see - in your own code and someone else's your stuck looking at.
The script style of the function, very PHP-like, was to illustrate the function declaration and content all in one small location. In a PHP script roughly like this it acts in a similar manner to a C++ function overload (one with an arg, one without).
On the other hand, I appreciate your thoughts and dis-assembly. I've not disassembled code ( once upon a time I had a x86 commenting disassembler) in a very many years.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
Yea, I see your point here on preferences. When saw the subject like More Efficient, it often made me think about actual implementation details. But fine, thanks for your good explanation
|
|
|
|
|
Keep in mind that modern optimizers are *very* good. I wouldn’t assume that your two examples are actually going to result in different compiled code. Definitely lean toward readability.
|
|
|
|
|
It was always about readability although I did mention that it saved a step. sometimes, in what I think is the more readable version.
Spread across all languages where it's a possibilitya, however, includes scripting languages and compiled versions.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
if(inVal==NULL)
{
inVal = internalDefault;
}
Ternary operators have their place, but aren't mean to replace "if" blocks. Also using that since the input-validation exceptions follow the same pattern, makes code nicely readable. And yes, always as blocks. Typing two chars extra isn't gonna kill anyone.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I would never want to be in a situation where I had to justify something that was redundant but looked better.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
It's all in the subject my child.
|
|
|
|
|
Help me, Obi-Ken - you're my only hope!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
1977 i saw it in the movie theaters it was awesome, and i have been a fan ever since
|
|
|
|
|
I had the opposite reaction. I realized that I was not a Sci-Fi fan.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Why does Skywalker always use chopsticks – just use the fork Luke.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Might it not depend upon how Chewy the food is? It might also come down to just talking about being a for user Obi Wan. You know; does he just Jabba on about it or does Ewok the walk?
With rebels, you can never be sure about 'why?'
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
modified 4-May-21 12:40pm.
|
|
|
|
|
Find or find not. There is no trie.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
If Skywalker uses a fork he could hurt himself during a anger attack.
|
|
|
|
|
Only to eat Luke warm Tauntaun.
If you can't laugh at yourself - ask me and I will do it for you.
|
|
|
|
|
There's no menu. You get what you deserve!
Ba-tish!
Anything that is unrelated to elephants is irrelephant Anonymous
- The problem with quotes on the internet is that you can never tell if they're genuine Winston Churchill, 1944
- Never argue with a fool. Onlookers may not be able to tell the difference. Mark Twain
|
|
|
|
|
But do they sell Chameleon?[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
is it just deserts?
To err is human to really elephant it up you need a computer
|
|
|
|
|
Thar is no truth to that.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Yes - that's the one where, when you Karma in they Suture you down.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
Union given electronic food? (10)
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
FEDERATION - union
FED E RATION - given e-food
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|