Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Do you want string or string?

Do you want string or string?

Scheduled Pinned Locked Moved The Weird and The Wonderful
testingbeta-testingquestion
19 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B BotCar

    OK, by Hall Of Shame standards, this isn't too bad. Still worth sharing...

    KeyValuePair lastItem;
    //Some code...
    if (lastItem.Value != null && lastItem.Value.ToString().Trim() != "")
    {
    //Some more code...

    Three things jump out at me: 1. Use of empty string literal instead of String.Empty 2. Seperate testing of null and empty string instead of String.IsNullOrEmpty() 3. Calling ToString() on something that is already a string. #1 and #2 isn't too serious, especially if you consider that you also want to trim the string, so you need to check for null before trying to trim anyway. #3, on the other hand, confuses me. Why would you want to call ToString() on a string?

    D Offline
    D Offline
    Dan Neely
    wrote on last edited by
    #5

    Refactoring Fail. To match the original functionality what you need is String.IsNullOrWhiteSpace(). String.IsNullOrEmpty(lastItem.Value) will return false if lastItem.Value equals " ". String.IsNullOrEmpty(lastItem.Value.Trim()) will exception if lastItem.Value is null. My other question would be how old the codebase is? IsNullOrEmpty() wasn't added until .net 2.0; IsNullOrWhiteSpace() wasn't added until 4.0. I wrote an IsNullOrEmpty() method when I needed one in a 1.1 app; but unless you're doing it in multiple locations there's nothing wrong with inlining.

    Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

    1 Reply Last reply
    0
    • B BotCar

      OK, by Hall Of Shame standards, this isn't too bad. Still worth sharing...

      KeyValuePair lastItem;
      //Some code...
      if (lastItem.Value != null && lastItem.Value.ToString().Trim() != "")
      {
      //Some more code...

      Three things jump out at me: 1. Use of empty string literal instead of String.Empty 2. Seperate testing of null and empty string instead of String.IsNullOrEmpty() 3. Calling ToString() on something that is already a string. #1 and #2 isn't too serious, especially if you consider that you also want to trim the string, so you need to check for null before trying to trim anyway. #3, on the other hand, confuses me. Why would you want to call ToString() on a string?

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #6

      1. A wtf? There is no difference according to MSDN[^] I actually prefer the "" instead of String.Empty, but that's personal taste. I agree to two and three though, but not everyone knows the IsNullOrEmpty method. :)

      V.

      OriginalGriffO B B 3 Replies Last reply
      0
      • V V 0

        1. A wtf? There is no difference according to MSDN[^] I actually prefer the "" instead of String.Empty, but that's personal taste. I agree to two and three though, but not everyone knows the IsNullOrEmpty method. :)

        V.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #7

        In .NET 4, there is IsNullOrWhiteSpace[^] as well, which is even more useful!

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "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

        L 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          In .NET 4, there is IsNullOrWhiteSpace[^] as well, which is even more useful!

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #8

          Did you scroll down to the end?

          VB Programmers: Null means Nothing in this method name

          If it means nothing, then why did they put it into the name? :)

          I'm invincible, I can't be vinced

          OriginalGriffO K 2 Replies Last reply
          0
          • L Lost User

            Did you scroll down to the end?

            VB Programmers: Null means Nothing in this method name

            If it means nothing, then why did they put it into the name? :)

            I'm invincible, I can't be vinced

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #9

            Yep! And I have no problem with that at all... :-D

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            "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

            K 1 Reply Last reply
            0
            • V V 0

              1. A wtf? There is no difference according to MSDN[^] I actually prefer the "" instead of String.Empty, but that's personal taste. I agree to two and three though, but not everyone knows the IsNullOrEmpty method. :)

              V.

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #10

              I was told that string.Empty doesn't create a whole bunch of new string instances. I will keep that quote from the MSDN guy to hand for next time I'm told using "" is wrong. (I prefer it too because two characters is almost always better then 11 and it's immediately obvious what "" is without actually having to read a word.)

              1 Reply Last reply
              0
              • B BotCar

                OK, by Hall Of Shame standards, this isn't too bad. Still worth sharing...

                KeyValuePair lastItem;
                //Some code...
                if (lastItem.Value != null && lastItem.Value.ToString().Trim() != "")
                {
                //Some more code...

                Three things jump out at me: 1. Use of empty string literal instead of String.Empty 2. Seperate testing of null and empty string instead of String.IsNullOrEmpty() 3. Calling ToString() on something that is already a string. #1 and #2 isn't too serious, especially if you consider that you also want to trim the string, so you need to check for null before trying to trim anyway. #3, on the other hand, confuses me. Why would you want to call ToString() on a string?

                Z Offline
                Z Offline
                ZurdoDev
                wrote on last edited by
                #11

                Sometimes you just have to make absolutely sure it is a string. And since it is OO programming they could have done this:

                lastItem.Value.ToString().ToString().ToString().Trim()

                just to make really, really sure.

                B 1 Reply Last reply
                0
                • V V 0

                  1. A wtf? There is no difference according to MSDN[^] I actually prefer the "" instead of String.Empty, but that's personal taste. I agree to two and three though, but not everyone knows the IsNullOrEmpty method. :)

                  V.

                  B Offline
                  B Offline
                  BotCar
                  wrote on last edited by
                  #12

                  Yes, I know there's no difference and that it's a matter of personal taste, so the worst that can happen is that it might mildly annoy a maintenance programmer that expected a different coding style. That said, I probably should have mentioned earlier that it happens to be in a codebase in which things like this is discouraged. In fact, string literals in general are discouraged: there should be a separate class full of constants. This means that the code above sticks out like a sore thumb around here.

                  1 Reply Last reply
                  0
                  • Z ZurdoDev

                    Sometimes you just have to make absolutely sure it is a string. And since it is OO programming they could have done this:

                    lastItem.Value.ToString().ToString().ToString().Trim()

                    just to make really, really sure.

                    B Offline
                    B Offline
                    BotCar
                    wrote on last edited by
                    #13

                    I like that! But you know, sometimes you want to be a little more sure than other times. Therefore, I propose the that following method be added:

                    public static string ConvertToString(object objectToConvert, ulong howSureDoYouWantToBeThatItIsString)
                    {
                    string temp = objectToConvert.ToString();
                    for(ulong n = 0; n < howSureDoYouWantToBeThatItIsString; n++)
                    {
                    temp = temp.ToString();
                    }
                    return temp;
                    }

                    Now ain't that a piece of beauty!

                    K 1 Reply Last reply
                    0
                    • F fjdiewornncalwe

                      If the codebase originated from a dotNet1.1 codebase, then the statement makes complete sense because string.IsNullOrEmpty did not exist at that time. Something that would definitely get updated in a code review: yes, Horror: close, but not quite.

                      I wasn't, now I am, then I won't be anymore.

                      J Offline
                      J Offline
                      Julien Villers
                      wrote on last edited by
                      #14

                      +1 plus they wouldn't know generics, thus the need to cast to String.

                      'As programmers go, I'm fairly social. Which still means I'm a borderline sociopath by normal standards.' Jeff Atwood 'I'm French! Why do you think I've got this outrrrrageous accent?' Monty Python and the Holy Grail

                      1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Yep! And I have no problem with that at all... :-D

                        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                        K Offline
                        K Offline
                        KP Lee
                        wrote on last edited by
                        #15

                        Shouldn't you have said "And I see Nothing wrong with that at all..."? :-D

                        1 Reply Last reply
                        0
                        • L Lost User

                          Did you scroll down to the end?

                          VB Programmers: Null means Nothing in this method name

                          If it means nothing, then why did they put it into the name? :)

                          I'm invincible, I can't be vinced

                          K Offline
                          K Offline
                          KP Lee
                          wrote on last edited by
                          #16

                          Just think about the poor SQL programmer where the standard setting is that "value != null" is always false and as a comparison, "value = null" is also always false. :-D

                          1 Reply Last reply
                          0
                          • B BotCar

                            OK, by Hall Of Shame standards, this isn't too bad. Still worth sharing...

                            KeyValuePair lastItem;
                            //Some code...
                            if (lastItem.Value != null && lastItem.Value.ToString().Trim() != "")
                            {
                            //Some more code...

                            Three things jump out at me: 1. Use of empty string literal instead of String.Empty 2. Seperate testing of null and empty string instead of String.IsNullOrEmpty() 3. Calling ToString() on something that is already a string. #1 and #2 isn't too serious, especially if you consider that you also want to trim the string, so you need to check for null before trying to trim anyway. #3, on the other hand, confuses me. Why would you want to call ToString() on a string?

                            K Offline
                            K Offline
                            KP Lee
                            wrote on last edited by
                            #17

                            I didn't know about IsNullOrEmpty, and apparently we both didn't know about IsNullOrWhiteSpace which removes the need for Trim. :)

                            1 Reply Last reply
                            0
                            • B BotCar

                              I like that! But you know, sometimes you want to be a little more sure than other times. Therefore, I propose the that following method be added:

                              public static string ConvertToString(object objectToConvert, ulong howSureDoYouWantToBeThatItIsString)
                              {
                              string temp = objectToConvert.ToString();
                              for(ulong n = 0; n < howSureDoYouWantToBeThatItIsString; n++)
                              {
                              temp = temp.ToString();
                              }
                              return temp;
                              }

                              Now ain't that a piece of beauty!

                              K Offline
                              K Offline
                              KP Lee
                              wrote on last edited by
                              #18

                              I like that! I wanted to see how many times that is possible to be looped: 18,446,744,073,709,551,615. That's a few loops. I figure, on my 2GH machine, that's approximately 272.19712525667353 years. (probably double that because 1 flop per loop and 1 flop per string conversion)

                                      double years = (double)((ulong.MaxValue / int.MaxValue) / (3600 \* 24)) / 365.25;
                              

                              flop = "FLoating point OPeration" Is it 1 or 2000 flops per string conversion for a 2000 character string? ;) PS Yes, I'm having fun with people without an understanding of precision too.

                              1 Reply Last reply
                              0
                              • B BotCar

                                OK, by Hall Of Shame standards, this isn't too bad. Still worth sharing...

                                KeyValuePair lastItem;
                                //Some code...
                                if (lastItem.Value != null && lastItem.Value.ToString().Trim() != "")
                                {
                                //Some more code...

                                Three things jump out at me: 1. Use of empty string literal instead of String.Empty 2. Seperate testing of null and empty string instead of String.IsNullOrEmpty() 3. Calling ToString() on something that is already a string. #1 and #2 isn't too serious, especially if you consider that you also want to trim the string, so you need to check for null before trying to trim anyway. #3, on the other hand, confuses me. Why would you want to call ToString() on a string?

                                B Offline
                                B Offline
                                Brisingr Aerowing
                                wrote on last edited by
                                #19

                                Maybe he was thinking of the 'String' theory! Yeah, I know, it's a groaner.

                                Attempting to load signature... A NullSignatureException was unhandled. Message: "No signature exists"

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups