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. Try Fail...

Try Fail...

Scheduled Pinned Locked Moved The Weird and The Wonderful
comxmlquestion
9 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.
  • V Offline
    V Offline
    VallarasuS
    wrote on last edited by
    #1

    Guess Microsoft failed to mention in XML documentation TryPare != CanParse...

            string\[\] rgb = values.Split(',');
            if (rgb.Length == 3)
            {
                byte r, g, b; // <- Why is this here anyway?!
                if (byte.TryParse(rgb\[0\], out r) && byte.TryParse(rgb\[0\], out g) && byte.TryParse(rgb\[0\], out b))
                {
                    color = Color.FromRgb(byte.Parse(rgb\[0\]), byte.Parse(rgb\[1\]), byte.Parse(rgb\[2\]));
                    return true;
                }
    

    Oups! wait! what are you doing?! Why are those type converters exists then?

    Regards Vallarasu S | FSharpMe.blogspot.com

    L J K 3 Replies Last reply
    0
    • V VallarasuS

      Guess Microsoft failed to mention in XML documentation TryPare != CanParse...

              string\[\] rgb = values.Split(',');
              if (rgb.Length == 3)
              {
                  byte r, g, b; // <- Why is this here anyway?!
                  if (byte.TryParse(rgb\[0\], out r) && byte.TryParse(rgb\[0\], out g) && byte.TryParse(rgb\[0\], out b))
                  {
                      color = Color.FromRgb(byte.Parse(rgb\[0\]), byte.Parse(rgb\[1\]), byte.Parse(rgb\[2\]));
                      return true;
                  }
      

      Oups! wait! what are you doing?! Why are those type converters exists then?

      Regards Vallarasu S | FSharpMe.blogspot.com

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

      r, g and b are declared because you can't call TryParse() without providing an out parameter for the result. Still, a CanParse() method would not be very much faster or more efficient than misusing TryParse() for that purpose. Anyway, I have seen much code where some rookie tried to put everything into strings (instead of proper types) and then was trying to parse (no pun intended) all over the code.

      At least artificial intelligence already is superior to natural stupidity

      B 1 Reply Last reply
      0
      • V VallarasuS

        Guess Microsoft failed to mention in XML documentation TryPare != CanParse...

                string\[\] rgb = values.Split(',');
                if (rgb.Length == 3)
                {
                    byte r, g, b; // <- Why is this here anyway?!
                    if (byte.TryParse(rgb\[0\], out r) && byte.TryParse(rgb\[0\], out g) && byte.TryParse(rgb\[0\], out b))
                    {
                        color = Color.FromRgb(byte.Parse(rgb\[0\]), byte.Parse(rgb\[1\]), byte.Parse(rgb\[2\]));
                        return true;
                    }
        

        Oups! wait! what are you doing?! Why are those type converters exists then?

        Regards Vallarasu S | FSharpMe.blogspot.com

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

        Is it really trying to parse three times rgb[0]? Then, it's an even greater WTF...

        '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

        V 1 Reply Last reply
        0
        • L Lost User

          r, g and b are declared because you can't call TryParse() without providing an out parameter for the result. Still, a CanParse() method would not be very much faster or more efficient than misusing TryParse() for that purpose. Anyway, I have seen much code where some rookie tried to put everything into strings (instead of proper types) and then was trying to parse (no pun intended) all over the code.

          At least artificial intelligence already is superior to natural stupidity

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

          You're missing the point, I think: after calling TryParse, you already have the values in the r, g and b variables, so TRWTF is why the author then calls Parse again inside the if.

          L 1 Reply Last reply
          0
          • B BobJanova

            You're missing the point, I think: after calling TryParse, you already have the values in the r, g and b variables, so TRWTF is why the author then calls Parse again inside the if.

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

            True, but that has also become a common mistake. Many people are not used to having to use the CPU sparingly. They (edit: the CPUs, not the people :)) are now powerful enough to forgive a certain amount of wastefulness. Depending on how often this code here is executed, an optimization may make little or no observable difference. In most cases the learning effect only sets in when their practices have come back and bitten them in the rear side.

            At least artificial intelligence already is superior to natural stupidity

            1 Reply Last reply
            0
            • J Julien Villers

              Is it really trying to parse three times rgb[0]? Then, it's an even greater WTF...

              '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

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

              Good Spot Julien, To be honest, All I felt wrong about this piece of code is #1. This can be done with ColorConverter.ConvertFromString(string)or With TypeDescriptor.GetConverter... #2. The author invokes try parse twice, first in the predicate then within the if block I didn't notice that until now. I never thought of making it to the author but now it has to be... Thanks.

              Regards Vallarasu S | FSharpMe.blogspot.com

              J Richard DeemingR 2 Replies Last reply
              0
              • V VallarasuS

                Good Spot Julien, To be honest, All I felt wrong about this piece of code is #1. This can be done with ColorConverter.ConvertFromString(string)or With TypeDescriptor.GetConverter... #2. The author invokes try parse twice, first in the predicate then within the if block I didn't notice that until now. I never thought of making it to the author but now it has to be... Thanks.

                Regards Vallarasu S | FSharpMe.blogspot.com

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

                Of course you are right, if the String is a standard format, ie from serialization or sanitized user input, you should use the standard parser. If not, a TryParse done right would be the way to go.

                '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
                • V VallarasuS

                  Good Spot Julien, To be honest, All I felt wrong about this piece of code is #1. This can be done with ColorConverter.ConvertFromString(string)or With TypeDescriptor.GetConverter... #2. The author invokes try parse twice, first in the predicate then within the if block I didn't notice that until now. I never thought of making it to the author but now it has to be... Thanks.

                  Regards Vallarasu S | FSharpMe.blogspot.com

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  VallarasuS wrote:

                  This can be done with ColorConverter.ConvertFromString

                  Or it could, if that class didn't have a nasty bug in it:

                  var converter = new ColorConverter();
                  Color color = converter.ConvertFromString("#XXXa");
                  Console.WriteLine(color);

                  Given this code, you might expect a System.FormatException or a System.ArgumentException. Instead, you get a System.Exception wrapping a System.FormatException! :wtf: So your code now becomes:

                  var converter = new ColorConverter();
                  try
                  {
                  Color color = converter.ConvertFromString("#XXXa");
                  Console.WriteLine(color);
                  }
                  catch (FormatException)
                  {
                  // Handle format exception
                  }
                  catch (Exception ex)
                  {
                  if (ex.InnerException == null) throw;
                  if (!(ex.InnerException is FormatException)) throw;
                  // Handle format exception again
                  }

                  Apparently, this can't be fixed because "it may result in a breaking change in deployed applications"! https://connect.microsoft.com/VisualStudio/feedback/details/94064/colortranslator-fromhtml-throws-a-system-exception[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • V VallarasuS

                    Guess Microsoft failed to mention in XML documentation TryPare != CanParse...

                            string\[\] rgb = values.Split(',');
                            if (rgb.Length == 3)
                            {
                                byte r, g, b; // <- Why is this here anyway?!
                                if (byte.TryParse(rgb\[0\], out r) && byte.TryParse(rgb\[0\], out g) && byte.TryParse(rgb\[0\], out b))
                                {
                                    color = Color.FromRgb(byte.Parse(rgb\[0\]), byte.Parse(rgb\[1\]), byte.Parse(rgb\[2\]));
                                    return true;
                                }
                    

                    Oups! wait! what are you doing?! Why are those type converters exists then?

                    Regards Vallarasu S | FSharpMe.blogspot.com

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

                    That's cute, set up values, don't use them and then blow up when the if test passes and rgb[1] == "G" :laugh: Well, if it does return true, you know you "CanParse" rgb[0] :) There are a lot of things I see in code that don't make sense. Interesting, "int i = 350; byte b = (byte)i;" works fine (sorta), but if you byte.TryParse(i.ToString(), out b); it will return false while b changes from 94 to 0.

                    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