• monkeyPushButton (unregistered)
  • (cs)

    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.

  • (cs)

    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.

  • CornedBee (unregistered)

    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.

  • Sean (unregistered) in reply to Steve The Cynic
    Steve The Cynic:
    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.

    +1

  • some pony (unregistered)

    you mean the original name of the space variable was myspace?

  • Rageous (unregistered)

    It's a null check effectively :)

  • (cs)

    I know budgets are tight, but storing strings in an intern, that's cheap.

  • JM (unregistered) in reply to Rageous

    Exactly. It's not a NOP -- a NOP would be safer and less buggy.

  • (cs)

    AddSpace? More like AdSpace. Wtf?

  • Production code (unregistered)
    foreach (char c in MyString)
    {
      char Space = ' ';
      if (c.ToString().Equals(Space.ToString()))
      {
        MyString2 += AddSpace(Space.ToString());
      } 
      else if (!c.ToString().Equals(Space.ToString()))
      {
        MyString2 += AddSpace(c.ToString);
      }
      else
      {
        MyString2 += c.ToString;
      }
    }
    MyString = MyString2;
  • Mike D. (unregistered)

    I'm particularly fond of the space in front of the variable declaration. Seems so apropos.

  • Ben (unregistered)

    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

  • OldCoder (unregistered) in reply to Steve The Cynic
    Steve The Cynic:
    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.
    Huh? What advertising? What network buttons? Are you sure you are on the same site?
  • Croakers (unregistered)

    public string AddComment(string value) { string comment = "Frist"; if (value.Equals(comment)) { return comment; } return value; }

  • (cs)

    Interns? I like my interns as long as they wash their dresses afterwards.

  • Nemo (unregistered)

    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?

  • (cs) in reply to OldCoder

    I think he's talking about all that addspace[sic] blocked out in the middle.

  • Rodnas (unregistered)

    In space no one can hear you scream. Which is exactly what I did when I saw this little masterpiece of uselessness.

  • iToad (unregistered)

    This function is simple enough that you can actually use SPIN and Alloy to prove it correct.

  • Pista (unregistered) in reply to Nemo
    Nemo:
    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?

    ROFL :D

  • (cs)

    It looks particularly technically worthless, to me.

  • drake (unregistered)

    Space... the final frontier

  • Dale (unregistered)

    Bzzzt. This is not a NOP. A NOP wouldn't throw a NullReferenceException if passed null.

  • (cs) in reply to Ben
    Ben:
    Bzzzzzz! Nope.
    This post is basically what I was going to say. Since you covered it all...
    TGV:
    I know budgets are tight, but storing strings in an intern, that's cheap.
    My binder is full of interns!
  • (cs) in reply to TGV
    TGV:
    I know budgets are tight, but storing strings in an intern, that's cheap.

    Hopefully not strings of DNA. That gets expensive.

  • T.R. (unregistered)

    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; { }

           return value;
       }
    
  • (cs)

    FTFY:

    public string AddSpace(string value)
           {
                string space = " ";
               if (value.Equals(space))
               {
                   // and it's a space!
                   return space;
               }
               // and we're done!
               return value;
           }
    
    
  • (cs)

    I've located the author of this code:

    [image]
  • (cs)

    Truly worthy of floccinaucinihilipilification.

  • Mike C (unregistered) in reply to Rageous

    It's not even that, because passing in a null will create an object reference exception when value.Equals() is evaluated.

  • (cs) in reply to Mike C
    Mike C:
    It's not even that, because passing in a null will create an object reference exception when value.Equals() is evaluated.

    while I was reading the code, I could only imagine that something like this exists somewhere in the world:

    public string AddSpace(string value)
           {
                string space = " ";
               if (string.isNullOrEmpty(value))
               {
                   return space;
               }
    
               return value;
           }
    

    used as such:

    if(AddSpace(strVar).substring(0, 1) == " ") {
       // do stuff
    }
    

    maybe even with the space variable declared as a constant. then space would really be the "final" frontier.

  • WvS (unregistered)

    Nice, the identity function for strings.

  • jay (unregistered)

    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

    if (amount2!=0)
    {
      amount1=amount1+amount2;
    }
    

    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:

    if (flag==true) flag=false;
    

    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.)

  • AGray (unregistered)

    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.

  • User (unregistered) in reply to Ben
    Ben:
    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.

    Ding ding ding!

    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.

    AGray:
    I know a way that if(flag) flag = false is a good idea - token parsing.
    Oo oo or if you're like me and are too lazy to use conditional breakpoints!
  • AGray (unregistered) in reply to User
    User:
    AGray:
    I know a way that if(flag) flag = false is a good idea - token parsing.
    Oo oo or if you're like me and are too lazy to use conditional breakpoints!

    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.

  • (cs) in reply to AGray
    AGray:
    ...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.
    So wouldn't that be

    if(!flag) throw new Exception()

    I fail to see how what you said relates to what he said.

  • AGray (unregistered) in reply to Sutherlands
    Sutherlands:
    AGray:
    ...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.
    So wouldn't that be

    if(!flag) throw new Exception()

    I fail to see how what you said relates to what he said.

    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!

  • Simon (unregistered) in reply to Steve The Cynic
    Steve The Cynic:
    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.
    I agree. I want this site FREE and Ad-FREE. YOu have no right whatsoever to make money.
  • Simon (unregistered) in reply to Simon
    Simon:
    Steve The Cynic:
    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.
    I agree. I want this site FREE and Ad-FREE. YOu have no right whatsoever to make money.
    Also, just in case you couldn't detect the sarcasm, at least they have clearly marked the advertisement as an advertisement - so you can ignore it if you wish!!
  • sd (unregistered) in reply to Croakers
    Croakers:
    public string AddComment(string value) { string comment = "Frist"; if (value.Equals(comment)) { return comment; } return value; }
    Nope. There is no "Frist" is no value. ever.
  • John (unregistered) in reply to jay
    jay:
    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

    if (amount2!=0)
    {
      amount1=amount1+amount2;
    }
    

    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:

    if (flag==true) flag=false;
    

    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.)

    Future proofing. As you point out, booleans may also be File_Not_Found one day. Additionally, we might want to divide instead of add sometime in the future, so the check is future-proofing us

  • Hugh (unregistered) in reply to AGray
    AGray:
    User:
    AGray:
    I know a way that if(flag) flag = false is a good idea - token parsing.
    Oo oo or if you're like me and are too lazy to use conditional breakpoints!

    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.

    I didn't understand the game writers example, but I think what the OP originally meant is that:

    if(flag) flag = false;
    

    is identical to

    flag=false;
    

    unless there's an else clause on the original if.....(which you'd hope isn't:

    else flag = true;
    

    )

  • (cs)

    What about an incorrect implementation of lazy initialization (something I've had to deal with)?

    if(flag == null) { loadFlag(); }
    

    If this comes after "flag = false;", it won't call loadFlag, but with the other version, it might.

  • Gemima (unregistered) in reply to chubertdev
    chubertdev:
    What about an incorrect implementation of lazy initialization (something I've had to deal with)?
    if(flag == null) { loadFlag(); }
    

    If this comes after "flag = false;", it won't call loadFlag, but with the other version, it might.

    I think your mixing multiple languages.

  • (cs) in reply to Gemima

    I normally speak in Frenglish. :)

  • (cs)

    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.

  • A. Einstein (unregistered) in reply to Ben
    Ben:
    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.

    No, it's wasting space.

  • setter (unregistered) in reply to bgodot
    bgodot:
    what if 'flag' has a setter with overhead? like triggering a flagChanged event?
    Then it is the job of the setter to fire the event only if flag has really changed.

    bool changed = flag != _flag; _flag = flag; if(changed) flagChanged(...);

Leave a comment on “Space: The Final NOP”

Log In or post as a guest

Replying to comment #393133:

« Return to Article