|
The really sad thing is that most programmers don't know that i-- is the fastest way of the four...
I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...
-----
"The conversations he was having with himself were becoming ominous."-.. On the radio...
|
|
|
|
|
Is there some subtle sarcasm going on here? I've never seen "++i" used (in a for loop) - ever.
EDIT ==========
Just to see if you guys are chain yankin' here, I tried it both ways :
1 million iterations = both completed the iteration in 0.0040002 seconds
10 million iterations = i++ took 0.0350020, and ++i took 0.0340019 seconds
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
modified on Sunday, January 2, 2011 9:42 AM
|
|
|
|
|
Does this mean the wife is not up yet as is the case for me and that it is best to let her sleep in on Sunday morning?
(Might be a good idea considering she's quick on the draw)...
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
John Simmons / outlaw programmer wrote: "Is there some subtle sarcasm going on here? I've never seen "++i" used (in a for loop) - ever."
Somewhat subtle. The increment operator is slightly more efficient in its prefix version (++i) because it just increments 'i' and returns its value. The postfix version has to do more work because it returns the *previous* version of 'i', so it has to store that value somewhere before incrementing i.
If you're just incrementing 'i', it makes more sense to use the more efficient prefix version. It's just better form, and encourages good habits. (For example, applying it to something more complex than an integer, say an iterator to an STL container class, might produce a more significant performance hit than the miniscule one your test found.)
Yet you're right, most of the instances of '++' or' --' I've seen in code, particularly in 'for' loops, is the postfix version. There's no reason for it, yet somehow that has become a near-universal meme in programming. Perhaps because the most common example beginning programmers encounter is the classic C-string copy code snippet, where you really *do* want to use the postfix version:
while (*dst++ = *src++)
;
So I was making a joke that the real coding outrage in the original poster's message was using 'i++' instead of '++i', as if I had completely missed the real outrage of sleeping the thread in each pass through the 'for' loop.
(Getting off my soapbox now.)
|
|
|
|
|
Isn't there a version 2.5? Thread.Sleep(0);
|
|
|
|
|
another version may come up with something like
for ( int i = 0; i < data.Count; container.Add(data[i].Items,i++ )
|
|
|
|
|
improved version of above can be like:
for ( int i = 0; i < data.Count; container.Add(data[i++].Items)
|
|
|
|
|
I can improve even that code
for ( int i = 0; i < data.Count; container.Add(data[++i].Items)
|
|
|
|
|
Adrian0, unfortunately, your solution was invalid.
As ++i will be calculated(increased) before accessing the element of data collection.
container will never have data[0], plus you will receive an out of index exception at the final loop
of this for expression. Say data collection has 4 elements.
Each iteration looks like:
Loop 1, i initialised = 0:
container.Add(data[1]).Items
Loop 2, i initialised = 1:
container.Add(data[2]).Items
Loop 3, i initialised = 2:
container.Add(data[3]).Items
Loop 3, i initialised = 3:
container.Add(data[4]).Items <<< Exception here
When i++ or ++i is within an expression, ++i has the highest priority to get executed bofere the reset of
the expression. In contrast, i++ is get executed after the whole expression has been executed.
container.Add(data[i++].Items) //Get the ith element of data collection, append to container and then increase i by 1
container.Add(data[++i].Items) //Increase i by 1, then get the 1th element of data collection to append to the container.
|
|
|
|
|
Good work yxhu. Didn't recognize that.
But if you are that fussy watch your parenthesis
Loop 3, i initialised = 3:
container.Add(data[4]).Items <<< Exception here
Your code wouldn't compile because .Add() is usually of type void.
greets
Adrian
|
|
|
|
|
Lol... that was a typo. Thanks Adrian0.
|
|
|
|
|
A common one in this forum is functions used that don't actually do anything. Well here is one I have just found too...
public static bool ToBoolean(bool value);
...forget where I seen this now (no wonder it is seen so often, people obviously read too much documentation lol)
PS. I am assuming most no exactly where I found this Convertion function
return 5;
|
|
|
|
|
It not really a horror, is it? It does say it does no conversion in MSDN[^]
I would say it is just there to complete the set: Convert.ToInt32 and so forth are there too.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
As I said. People post these 'Do Nothing' functions all the time. I was simple pointing this one out as these are obviously OK if MS says so
return 5;
|
|
|
|
|
Actually very handy if you are passing a object type which has an underlying value of type bool. Good code should always be strongly typed but maybe when dealing with COM objects, this overloaded method could be handy.
|
|
|
|
|
Robert Croll wrote: Actually very handy if you are passing a object type which has an underlying value of type bool
But this specific function does not take an object. It takes a bool and returns a bool. Just wasted code in any usage
return 5;
|
|
|
|
|
For those who desperately need self-validation.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
|
The horror here is not only that these methods do not do what they are supposed to do. The fact is that the implementation itself is really poor. Seems that the programmer got paid according to the number of lines of code he wrote.
|
|
|
|
|
Well... Actually I (the new developer) has to pay :P
|
|
|
|
|
Hopefully the programmer was so called "Senior". That's a tough call for a junior programmer.
|
|
|
|
|
The root of the problem is that it's done in VB...
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Found this in the daily WTF.
private IAlertDocument CompareObjectAsIAlertDocumentOrNullIfNotCastable(object compareObject) {
return compareObject as IAlertDocument;
}
What do you think? I can follow this pattern:
string GetAStringThatRepresentsTheObjectPassedAsAnObjectParameterSoItWorksForAnyObject(object obj)
{
return obj.ToString();
}
|
|
|
|
|
_Erik_ wrote: string GetAStringThatRepresentsTheObjectPassedAsAnObjectParameterSoItWorksForAnyObject(object obj)
{
return obj.ToString();
}
You forgot part of the method name...
It should be... GetAStringThatRepresentsTheObjectPassedAsAnObjectParameterSoItWorksForAnyObjectAsLongAsTheObjectIsNotNull
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Damn it!! I thought I could follow the pattern!!
|
|
|
|