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. Converting a string to an int

Converting a string to an int

Scheduled Pinned Locked Moved The Weird and The Wonderful
rubyperformancequestion
14 Posts 6 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.
  • L Lost User

    I found this gem of a code when I was reviewing some code I am supposed to enhance. And this kind of conversion code has been used all over the project. Note that variable and method names have been changed. Did the guy who wrote this code ever have any idea how much memory and processor cycles does throwing and catching exceptions entail?

    int age;
    string strAge = GetAge();

    try {
    age = Convert.ToInt32(strAge);
    } catch {
    age = 0;
    }

    _ Offline
    _ Offline
    _Damian S_
    wrote on last edited by
    #4

    Shameel wrote:

    atrAge

    Shameel wrote:

    strAge

    I think I see the problem!! :laugh:

    Silence is golden... but duct tape is silver!! Booger Mobile - My bright green 1964 Ford Falcon - check out the blog here!! | If you feel generous - make a donation to Camp Quality!!

    L 1 Reply Last reply
    0
    • G GibbleCH

      It was VERY common prior to .Net 2.0 which introduced the TryParse methods. There really wasn't a 'good' way to do this, so if the result of GetAge() was normally a valid number, then it wouldn't hurt performance much. The performance hit is only noticable if there is actually an exception thrown.

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

      GibbleCH wrote:

      The performance hit is only noticable if there is actually an exception thrown.

      Exactly. And when I was debugging, it was indeed throwing exceptions at many places because the values were supposed to be coming from a missing configuration element.

      1 Reply Last reply
      0
      • P PIEBALDconsult

        Shameel wrote:

        how much memory and processor cycles does throwing and catching exceptions entail?

        Maybe, but perhaps you don't? Performance implications of Exceptions in .NET[^]

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

        The idea was to convey that a mere check for numbers would have been enough in this case instead of relying on .NET's exception handling to assign default value for missing items (and unnecessarily create and dispose exception objects). And when I was debugging the code, it was throwing exception all over the places because the values were supposed to come from config elements which were missing. It's a bad practise even from a debugging perspective.

        P 1 Reply Last reply
        0
        • _ _Damian S_

          Shameel wrote:

          atrAge

          Shameel wrote:

          strAge

          I think I see the problem!! :laugh:

          Silence is golden... but duct tape is silver!! Booger Mobile - My bright green 1964 Ford Falcon - check out the blog here!! | If you feel generous - make a donation to Camp Quality!!

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

          That was just a typo. I renamed the variables to save the identity of the culprit. :-)

          _ 1 Reply Last reply
          0
          • L Lost User

            That was just a typo. I renamed the variables to save the identity of the culprit. :-)

            _ Offline
            _ Offline
            _Damian S_
            wrote on last edited by
            #8

            Shameel wrote:

            That was just a typo. I renamed the variables to save the identity of the culprit. :)

            I know... it was funny though!!

            Silence is golden... but duct tape is silver!! Booger Mobile - My bright green 1964 Ford Falcon - check out the blog here!! | If you feel generous - make a donation to Camp Quality!!

            1 Reply Last reply
            0
            • L Lost User

              I found this gem of a code when I was reviewing some code I am supposed to enhance. And this kind of conversion code has been used all over the project. Note that variable and method names have been changed. Did the guy who wrote this code ever have any idea how much memory and processor cycles does throwing and catching exceptions entail?

              int age;
              string strAge = GetAge();

              try {
              age = Convert.ToInt32(strAge);
              } catch {
              age = 0;
              }

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

              This isn't bad, just outdated. Before the introduction of TryParse this is basically how you had to do it (though I would use int.Parse not Convert.ToInt32, but one calls the other I think). Although you could argue it should be in a single utility method, it's only a couple of lines so it's hardly worth it. A try block uses almost no resources, and the exception will only be thrown when the requested string isn't a valid number, so I doubt there's any noticeable performance impact from doing this. Annoying to deal with when you have a better solution available to you now, and you have debugging set to look at caught exceptions? Yeah, probably. But it's not really bad code. If you're complaining that the debugger stops on it, try adjusting your settings – otherwise you can use that as an argument against any catch blocks and I'm sure you can see how that becomes an absurd position to hold pretty quickly.

              P L 2 Replies Last reply
              0
              • L Lost User

                The idea was to convey that a mere check for numbers would have been enough in this case instead of relying on .NET's exception handling to assign default value for missing items (and unnecessarily create and dispose exception objects). And when I was debugging the code, it was throwing exception all over the places because the values were supposed to come from config elements which were missing. It's a bad practise even from a debugging perspective.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #10

                Shameel wrote:

                a mere check for numbers

                That's worse; it can still go wrong, a string of "9999999999999999999999999999999999999999" for instance. Using a built-in method is much simpler and if you have to protect against Exception, then do so, it doesn't cost anything.

                1 Reply Last reply
                0
                • B BobJanova

                  This isn't bad, just outdated. Before the introduction of TryParse this is basically how you had to do it (though I would use int.Parse not Convert.ToInt32, but one calls the other I think). Although you could argue it should be in a single utility method, it's only a couple of lines so it's hardly worth it. A try block uses almost no resources, and the exception will only be thrown when the requested string isn't a valid number, so I doubt there's any noticeable performance impact from doing this. Annoying to deal with when you have a better solution available to you now, and you have debugging set to look at caught exceptions? Yeah, probably. But it's not really bad code. If you're complaining that the debugger stops on it, try adjusting your settings – otherwise you can use that as an argument against any catch blocks and I'm sure you can see how that becomes an absurd position to hold pretty quickly.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #11

                  10!

                  BobJanova wrote:

                  one calls the other

                  Yes, Convert calls the specific class' method, Int32.Parse in this case -- that's why I tell people not to use Convert.

                  1 Reply Last reply
                  0
                  • B BobJanova

                    This isn't bad, just outdated. Before the introduction of TryParse this is basically how you had to do it (though I would use int.Parse not Convert.ToInt32, but one calls the other I think). Although you could argue it should be in a single utility method, it's only a couple of lines so it's hardly worth it. A try block uses almost no resources, and the exception will only be thrown when the requested string isn't a valid number, so I doubt there's any noticeable performance impact from doing this. Annoying to deal with when you have a better solution available to you now, and you have debugging set to look at caught exceptions? Yeah, probably. But it's not really bad code. If you're complaining that the debugger stops on it, try adjusting your settings – otherwise you can use that as an argument against any catch blocks and I'm sure you can see how that becomes an absurd position to hold pretty quickly.

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

                    BobJanova wrote:

                    you have debugging set to look at caught exceptions?

                    yes, I have to do that to handle some subtle exceptions that are caught and swallowed (empty catch blocks - now that deserves a separate post by itself).

                    BobJanova wrote:

                    try adjusting your settings

                    I hate to keep doing it every now and then to accomodate this kind of code. Unfortunately, for now, I have to live with it since I cannot change the behavior of existing "working code".

                    B 1 Reply Last reply
                    0
                    • L Lost User

                      BobJanova wrote:

                      you have debugging set to look at caught exceptions?

                      yes, I have to do that to handle some subtle exceptions that are caught and swallowed (empty catch blocks - now that deserves a separate post by itself).

                      BobJanova wrote:

                      try adjusting your settings

                      I hate to keep doing it every now and then to accomodate this kind of code. Unfortunately, for now, I have to live with it since I cannot change the behavior of existing "working code".

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

                      But my point is that 'this kind of code' is basically any code that uses exceptions. Swallowed exceptions generally deserve a post here, yes. This code handles the exception to put the variable into a valid state so it's not in the same category. Perhaps better than changing your settings would be to make the config file valid.

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Shameel wrote:

                        how much memory and processor cycles does throwing and catching exceptions entail?

                        Maybe, but perhaps you don't? Performance implications of Exceptions in .NET[^]

                        R Offline
                        R Offline
                        Rob Grainger
                        wrote on last edited by
                        #14

                        Sounds like you didn't RTFA: "Only primitive string processing was affected by raised exceptions" I'd describe a conversion from an string to an int as fitting that description. Generally, however, I agree - people are too fixated with the costs of exceptions. If there's any I/O going on at all, its negligible.

                        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