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. I'm speechless about this piece of code.

I'm speechless about this piece of code.

Scheduled Pinned Locked Moved The Weird and The Wonderful
ruby
34 Posts 17 Posters 215 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.
  • P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #1

    I've just read an article that included this little gem

    ://it checks it contains 11 char (dd/MMM/yyyy) 
    if (Convert.ToInt16(chkdate.Length.ToString()) != 11) 
    { 
      //if it does not have 11 char then it will return false 
      return false; 
    }
    

    I really don't know where to start (and yes, chkdate is a string).

    Deja View - the feeling that you've seen this post before.

    C P L L D 9 Replies Last reply
    0
    • P Pete OHanlon

      I've just read an article that included this little gem

      ://it checks it contains 11 char (dd/MMM/yyyy) 
      if (Convert.ToInt16(chkdate.Length.ToString()) != 11) 
      { 
        //if it does not have 11 char then it will return false 
        return false; 
      }
      

      I really don't know where to start (and yes, chkdate is a string).

      Deja View - the feeling that you've seen this post before.

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      Odd that it does "actually" check for 11 characters. I wonder what the code looks like that ensures the "dd/MMM/yyyy" format. There's likely a gem or two in that as well. :)

      Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

      P 1 Reply Last reply
      0
      • C Chris Meech

        Odd that it does "actually" check for 11 characters. I wonder what the code looks like that ensures the "dd/MMM/yyyy" format. There's likely a gem or two in that as well. :)

        Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Here[^] you go. Enjoy.

        Deja View - the feeling that you've seen this post before.

        C R 2 Replies Last reply
        0
        • P Pete OHanlon

          I've just read an article that included this little gem

          ://it checks it contains 11 char (dd/MMM/yyyy) 
          if (Convert.ToInt16(chkdate.Length.ToString()) != 11) 
          { 
            //if it does not have 11 char then it will return false 
            return false; 
          }
          

          I really don't know where to start (and yes, chkdate is a string).

          Deja View - the feeling that you've seen this post before.

          P Offline
          P Offline
          Paddy Boyd
          wrote on last edited by
          #4

          It worries me that we are spiralling into some kind of recursive software hellhole:

          void WritePoorCode()
          {
          bool stuck = true;

          if (stuck)
          {
              GetHelpFromAnotherMuppet();
          }
          

          }

          void GetHelpFromAnotherMuppet()
          {
          WritePoorCode();
          }

          C P N C 4 Replies Last reply
          0
          • P Pete OHanlon

            Here[^] you go. Enjoy.

            Deja View - the feeling that you've seen this post before.

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            Great. It doesn't validate a thing. :)

            Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

            1 Reply Last reply
            0
            • P Paddy Boyd

              It worries me that we are spiralling into some kind of recursive software hellhole:

              void WritePoorCode()
              {
              bool stuck = true;

              if (stuck)
              {
                  GetHelpFromAnotherMuppet();
              }
              

              }

              void GetHelpFromAnotherMuppet()
              {
              WritePoorCode();
              }

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              :cool:

              Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

              L 1 Reply Last reply
              0
              • P Paddy Boyd

                It worries me that we are spiralling into some kind of recursive software hellhole:

                void WritePoorCode()
                {
                bool stuck = true;

                if (stuck)
                {
                    GetHelpFromAnotherMuppet();
                }
                

                }

                void GetHelpFromAnotherMuppet()
                {
                WritePoorCode();
                }

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                :laugh:Kermit the coder.

                Deja View - the feeling that you've seen this post before.

                1 Reply Last reply
                0
                • C Chris Meech

                  :cool:

                  Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  Add tail calls, and it will run forever :)

                  xacc.ide
                  IronScheme a R5RS-compliant Scheme on the DLR
                  The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    I've just read an article that included this little gem

                    ://it checks it contains 11 char (dd/MMM/yyyy) 
                    if (Convert.ToInt16(chkdate.Length.ToString()) != 11) 
                    { 
                      //if it does not have 11 char then it will return false 
                      return false; 
                    }
                    

                    I really don't know where to start (and yes, chkdate is a string).

                    Deja View - the feeling that you've seen this post before.

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Pete O`Hanlon wrote:

                    Convert.ToInt16(chkdate.Length.ToString()) != 11

                    why do you rely on an implicit Int16-to-Int32 conversion? I would recommend an explicit cast to make things more clear. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                    P M R 3 Replies Last reply
                    0
                    • L Luc Pattyn

                      Pete O`Hanlon wrote:

                      Convert.ToInt16(chkdate.Length.ToString()) != 11

                      why do you rely on an implicit Int16-to-Int32 conversion? I would recommend an explicit cast to make things more clear. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      That's right. You can't have enough casting and converting going on.

                      Deja View - the feeling that you've seen this post before.

                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Pete O`Hanlon wrote:

                        Convert.ToInt16(chkdate.Length.ToString()) != 11

                        why do you rely on an implicit Int16-to-Int32 conversion? I would recommend an explicit cast to make things more clear. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                        M Offline
                        M Offline
                        MidwestLimey
                        wrote on last edited by
                        #11

                        Rediculous .. you need to convert both to decimal to guarentee precision and get rid of that pesky implicit conversion ...

                        if(Convert.ToDecimal(Convert.ToInt16(chkdate.Length.ToString())) != Convert.ToDecimal("11")) {
                        doNoRealValidationCheck_NeverLearnRegex();
                        }


                        I'm largely language agnostic


                        After a while they all bug me :doh:


                        1 Reply Last reply
                        0
                        • P Pete OHanlon

                          I've just read an article that included this little gem

                          ://it checks it contains 11 char (dd/MMM/yyyy) 
                          if (Convert.ToInt16(chkdate.Length.ToString()) != 11) 
                          { 
                            //if it does not have 11 char then it will return false 
                            return false; 
                          }
                          

                          I really don't know where to start (and yes, chkdate is a string).

                          Deja View - the feeling that you've seen this post before.

                          L Offline
                          L Offline
                          leppie
                          wrote on last edited by
                          #12

                          He could've written:

                          if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[0]) != new String("1").ToCharArray()[0])
                          {
                          if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[1]) != new String("1").ToCharArray()[0])
                          {
                          return false;
                          }
                          }

                          xacc.ide
                          IronScheme a R5RS-compliant Scheme on the DLR
                          The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                          C X 2 Replies Last reply
                          0
                          • L leppie

                            He could've written:

                            if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[0]) != new String("1").ToCharArray()[0])
                            {
                            if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[1]) != new String("1").ToCharArray()[0])
                            {
                            return false;
                            }
                            }

                            xacc.ide
                            IronScheme a R5RS-compliant Scheme on the DLR
                            The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                            C Offline
                            C Offline
                            Chris Meech
                            wrote on last edited by
                            #13

                            Except you forgot to convert the char types to some int type to ensure it would be a numeric comparison. :rolleyes: :)

                            Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

                            1 Reply Last reply
                            0
                            • L leppie

                              He could've written:

                              if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[0]) != new String("1").ToCharArray()[0])
                              {
                              if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[1]) != new String("1").ToCharArray()[0])
                              {
                              return false;
                              }
                              }

                              xacc.ide
                              IronScheme a R5RS-compliant Scheme on the DLR
                              The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                              X Offline
                              X Offline
                              Xiangyang Liu
                              wrote on last edited by
                              #14

                              leppie wrote:

                              if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[0]) != new String("1").ToCharArray()[0]){ if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[1]) != new String("1").ToCharArray()[0]) { return false; }}

                              Not so fast, your code does not work while his does. ;P [Hint] Your code will not return false for strings of length 1, 10, 101, etc.

                              My .NET Business Application Framework My Home Page

                              L 1 Reply Last reply
                              0
                              • P Pete OHanlon

                                I've just read an article that included this little gem

                                ://it checks it contains 11 char (dd/MMM/yyyy) 
                                if (Convert.ToInt16(chkdate.Length.ToString()) != 11) 
                                { 
                                  //if it does not have 11 char then it will return false 
                                  return false; 
                                }
                                

                                I really don't know where to start (and yes, chkdate is a string).

                                Deja View - the feeling that you've seen this post before.

                                D Offline
                                D Offline
                                darkelv
                                wrote on last edited by
                                #15

                                Pete O`Hanlon wrote:

                                ://it checks it contains 11 char (dd/MMM/yyyy) if (Convert.ToInt16(chkdate.Length.ToString()).ToString() != "11") { //if it does not have 11 char then it will return false return false; }

                                Fixed :-D

                                1 Reply Last reply
                                0
                                • P Pete OHanlon

                                  Here[^] you go. Enjoy.

                                  Deja View - the feeling that you've seen this post before.

                                  R Offline
                                  R Offline
                                  Ri Qen Sin
                                  wrote on last edited by
                                  #16

                                  It's dead! =\

                                  ROFLOLMFAO

                                  P 1 Reply Last reply
                                  0
                                  • L Luc Pattyn

                                    Pete O`Hanlon wrote:

                                    Convert.ToInt16(chkdate.Length.ToString()) != 11

                                    why do you rely on an implicit Int16-to-Int32 conversion? I would recommend an explicit cast to make things more clear. :)

                                    Luc Pattyn [Forum Guidelines] [My Articles]


                                    this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


                                    R Offline
                                    R Offline
                                    Ri Qen Sin
                                    wrote on last edited by
                                    #17

                                    I think this improves the code a lot… Extra precisions and conversions to make sure we're getting exactly what we want. :) ((Decimal)Convert.ToDecimal(chkdate.Length.ToString()) != (Decimal)Convert.ToDecimal(((Decimal)(11.0000000000000000000000000000)).ToString())).ToString() == Boolean.TrueString; The optimizing JITter is going to have fun with this one…

                                    ROFLOLMFAO

                                    A 1 Reply Last reply
                                    0
                                    • R Ri Qen Sin

                                      I think this improves the code a lot… Extra precisions and conversions to make sure we're getting exactly what we want. :) ((Decimal)Convert.ToDecimal(chkdate.Length.ToString()) != (Decimal)Convert.ToDecimal(((Decimal)(11.0000000000000000000000000000)).ToString())).ToString() == Boolean.TrueString; The optimizing JITter is going to have fun with this one…

                                      ROFLOLMFAO

                                      A Offline
                                      A Offline
                                      Andy Brummer
                                      wrote on last edited by
                                      #18

                                      You left out the string.compare call with the ignore case option, and specifying the CultureInfo on the ToString calls. :doh:


                                      This blanket smells like ham

                                      1 Reply Last reply
                                      0
                                      • X Xiangyang Liu

                                        leppie wrote:

                                        if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[0]) != new String("1").ToCharArray()[0]){ if (Convert.ToChar(chkdate.Length.ToString().ToCharArray()[1]) != new String("1").ToCharArray()[0]) { return false; }}

                                        Not so fast, your code does not work while his does. ;P [Hint] Your code will not return false for strings of length 1, 10, 101, etc.

                                        My .NET Business Application Framework My Home Page

                                        L Offline
                                        L Offline
                                        leppie
                                        wrote on last edited by
                                        #19

                                        Of course not ! :) Thats an exercise for the next poor soul that works on the code :p

                                        xacc.ide
                                        IronScheme a R5RS-compliant Scheme on the DLR
                                        The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                                        1 Reply Last reply
                                        0
                                        • P Pete OHanlon

                                          I've just read an article that included this little gem

                                          ://it checks it contains 11 char (dd/MMM/yyyy) 
                                          if (Convert.ToInt16(chkdate.Length.ToString()) != 11) 
                                          { 
                                            //if it does not have 11 char then it will return false 
                                            return false; 
                                          }
                                          

                                          I really don't know where to start (and yes, chkdate is a string).

                                          Deja View - the feeling that you've seen this post before.

                                          R Offline
                                          R Offline
                                          Rajesh R Subramanian
                                          wrote on last edited by
                                          #20

                                          Is this what they call optimized code? :confused:


                                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->ßRÅhmmÃ<-·´¯`·.

                                          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