- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
It's like an e-mail where you forget the attachment?
The function is called AddSpace and they forgot to add it? My guess is that the function was supposed to return a new string (as they are immutable) that is the same as the old one with a space appended, unless the old one was already a space.
Admin
Dudes! Any chance you could evict the advertising from the middle of the article? It's bad enough we have antisocial network buttons all over the place without having all that guff as well.
Admin
Call this function "InternIfSpace" and it's useless, but does what it says. Not all strings are automatically interned, only string literals. So this code gives you the interned reference if the argument is a space, and the original argument otherwise.
Admin
+1
Admin
you mean the original name of the space variable was myspace?
Admin
It's a null check effectively :)
Admin
I know budgets are tight, but storing strings in an intern, that's cheap.
Admin
Exactly. It's not a NOP -- a NOP would be safer and less buggy.
Admin
AddSpace? More like AdSpace. Wtf?
Admin
Admin
I'm particularly fond of the space in front of the variable declaration. Seems so apropos.
Admin
Bzzzzzz! Nope.
""" the runtime takes advantage of that by storing each unique string only once, in an intern """
Strings are not always interned. Constant strings are interned, and strings are interned if you ask for them to be. But the overhead of checking if every string is identical to a previous string is obviously too high to do it all the time.
It could be that the application was accidentally creating lots of strings consisting of a single space, and profiling demonstrated that this was causing memory fragmentation..
But it is more likely that someone just THOUGHT it would, and it is wasting time.
http://blogs.msdn.com/b/ericlippert/archive/2009/09/28/string-interning-and-string-empty.aspx
Admin
Admin
public string AddComment(string value) { string comment = "Frist"; if (value.Equals(comment)) { return comment; } return value; }
Admin
Interns? I like my interns as long as they wash their dresses afterwards.
Admin
Memory is going for a few bucks per GB these days. So a byte of memory is going for on the order of a billionth of a dollar. Interns are still free, so why would you use memory to store a space character, when you can get a dumb look from any intern for free?
Admin
I think he's talking about all that addspace[sic] blocked out in the middle.
Admin
In space no one can hear you scream. Which is exactly what I did when I saw this little masterpiece of uselessness.
Admin
This function is simple enough that you can actually use SPIN and Alloy to prove it correct.
Admin
ROFL :D
Admin
It looks particularly technically worthless, to me.
Admin
Space... the final frontier
Admin
Bzzzt. This is not a NOP. A NOP wouldn't throw a NullReferenceException if passed null.
Admin
Admin
Hopefully not strings of DNA. That gets expensive.
Admin
This function covers too few cases. I have some improvement :
public string AddSpace(string value) { string space = " "; if (value.Equals(space)) { return space; } else if (value.equals("F***ck off")) { return value; } else if (value.equals("Empty vessels make the most noise")) { return value; } else return value; { }
Admin
FTFY:
Admin
I've located the author of this code:
[image]Admin
Truly worthy of floccinaucinihilipilification.
Admin
It's not even that, because passing in a null will create an object reference exception when value.Equals() is evaluated.
Admin
while I was reading the code, I could only imagine that something like this exists somewhere in the world:
used as such:
maybe even with the space variable declared as a constant. then space would really be the "final" frontier.
Admin
Nice, the identity function for strings.
Admin
I'm routinely amazed at code that goes out of its way to do nothing.
I couldn't count how many times I've seen
I can only imagine that the programmer is saying to himself, That way if amount2 is zero, I don't waste time to do the add! Except, of course, that if amount2 is not zero, you do the test and then you still do the add; and if amount2 is zero, on most systems it takes longer to do the compare than it would take to do the add.
Another favorite:
Because if the flag was already false, we certainly wouldn't want to set it to false. (Okay, maybe that's a bad example. If flag was FILE_NOT_FOUND, we don't necessarily want to change it to false.)
Admin
I know a way that if(flag) flag = false is a good idea - token parsing.
I have a datafile for a game project I'm working on. I define my data in plain-text (due to a Unity limitation) as such:
Item:Potion Stat:HP Value:100 Icon:flask Target:single End Item
...often, I'll have a loop where I parse individual tokens. A key one would be "Item Started"...if I encounter an End Item token, but I haven't started an item, I throw an exception! Otherwise, I compile the item, add it to the repository, and clear everything out...including the "Item Started" flag.
...That dosen't detract from the article, which is quite a WTF... :(
CAPTCHA: damnum - The opposite of 42.
Admin
The "most" in "Not only does this code always return a reference to the same value, it actually returns the same reference in most cases" is important.
Oo oo or if you're like me and are too lazy to use conditional breakpoints!Admin
No, I do use conditional breakpoints for debugging, although I'm not above a Debug.Log(message) bit in Unity, if I'm suspicious about some bit of code.
Admin
if(!flag) throw new Exception()
I fail to see how what you said relates to what he said.
Admin
Dang it. When you put it that way, it totally dosen't, though to my mind it related to the 'if(flag) flag = false' bit. I guess. But apparently not.
I'm going to go untwist my mind now.
CAPTCHA: modo - My brain was in the wrong modo!
Admin
Admin
Admin
Admin
Admin
is identical to
unless there's an else clause on the original if.....(which you'd hope isn't:
)
Admin
What about an incorrect implementation of lazy initialization (something I've had to deal with)?
If this comes after "flag = false;", it won't call loadFlag, but with the other version, it might.
Admin
Admin
I normally speak in Frenglish. :)
Admin
what if 'flag' has a setter with overhead? like triggering a flagChanged event?
if (flag) flag = false; //avoids triggering the flagChanged event.
Now, if the getter has a side effect, you've got a different problem.
Admin
Admin
bool changed = flag != _flag; _flag = flag; if(changed) flagChanged(...);