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 230 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 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
                      • 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.

                        K Offline
                        K Offline
                        KarstenK
                        wrote on last edited by
                        #21

                        For checking whether the string contains a valid date the code is crap. (SCNR) This shows what the results of "high level" programming languages are. X|

                        Greetings from Germany

                        P 1 Reply Last reply
                        0
                        • K KarstenK

                          For checking whether the string contains a valid date the code is crap. (SCNR) This shows what the results of "high level" programming languages are. X|

                          Greetings from Germany

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

                          KarstenK wrote:

                          This shows what the results of "high level" programming languages are

                          Idiot developers who shouldn't be let anywhere near a keyboard without being wired up to the mains and given a shock everytime they produce crap like this.

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

                          C 1 Reply Last reply
                          0
                          • R Ri Qen Sin

                            It's dead! =\

                            ROFLOLMFAO

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

                            It is now. It was removed because it was so bad and about the best comment against it was that it was complete and utter rubbish.

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

                            V 1 Reply Last reply
                            0
                            • P Pete OHanlon

                              It is now. It was removed because it was so bad and about the best comment against it was that it was complete and utter rubbish.

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

                              V Offline
                              V Offline
                              Vasudevan Deepak Kumar
                              wrote on last edited by
                              #24

                              Pete O`Hanlon wrote:

                              It was removed

                              It should have been 'An Article of Horror'. It is bad that it was removed and CP deprived many of a viewing pleasure. :mad:

                              Vasudevan Deepak Kumar Personal Homepage
                              Tech Gossips
                              Regional Weblog (in Tamil) :: Voicing for the Society
                              Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.

                              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();
                                }

                                N Offline
                                N Offline
                                NormDroid
                                wrote on last edited by
                                #25

                                void Main() { if (CantAffordAProfessionalDeveloper()) { CreateACompleteMessOfASystem(); } } void CantAffordAProfessionalDeveloper() { PayPeanuts(); } void PayPeanuts() { HireSomebodyWithLittleOrNoExperience(); } void HireSomebodyWithLittleOrNoExperience() { if (LearnedFromSchool()) { GuessCode(); } if (LearnedFromInternet()) { GrapSampleFromSomePlace(); } }

                                WPF - Imagineers Wanted Follow your nose using DoubleAnimationUsingPath

                                P 1 Reply Last reply
                                0
                                • N NormDroid

                                  void Main() { if (CantAffordAProfessionalDeveloper()) { CreateACompleteMessOfASystem(); } } void CantAffordAProfessionalDeveloper() { PayPeanuts(); } void PayPeanuts() { HireSomebodyWithLittleOrNoExperience(); } void HireSomebodyWithLittleOrNoExperience() { if (LearnedFromSchool()) { GuessCode(); } if (LearnedFromInternet()) { GrapSampleFromSomePlace(); } }

                                  WPF - Imagineers Wanted Follow your nose using DoubleAnimationUsingPath

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

                                  I'm not sure that compiles... ;)

                                  N 1 Reply Last reply
                                  0
                                  • P Paddy Boyd

                                    I'm not sure that compiles... ;)

                                    N Offline
                                    N Offline
                                    NormDroid
                                    wrote on last edited by
                                    #27

                                    Try it, you'll be surprised, and even more surprised if we see the same code being used in an Indian offshore system ;)

                                    WPF - Imagineers Wanted Follow your nose using DoubleAnimationUsingPath

                                    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
                                      codemunkeh
                                      wrote on last edited by
                                      #28

                                      catch (System.OutOfMemoryException) { GetHelpFromAnotherMuppet(); }


                                      Ninja (the Nerd)
                                      Confused? You will be...

                                      1 Reply Last reply
                                      0
                                      • P Pete OHanlon

                                        KarstenK wrote:

                                        This shows what the results of "high level" programming languages are

                                        Idiot developers who shouldn't be let anywhere near a keyboard without being wired up to the mains and given a shock everytime they produce crap like this.

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

                                        C Offline
                                        C Offline
                                        codemunkeh
                                        wrote on last edited by
                                        #29

                                        Then even the lead developer would be shocking.


                                        Ninja (the Nerd)
                                        Confused? You will be...

                                        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.

                                          M Offline
                                          M Offline
                                          Michael Pauli
                                          wrote on last edited by
                                          #30

                                          Why can't not ONE of you folkes be grown up enough to simply say that the rubbish: ://it checks if it contains 11 char (dd/MMM/yyyy) if (Convert.ToInt16(chkdate.Length.ToString()) != 11) { // Do something if not. } SHOULD be: :// it checks if it contains 11 char (dd/MMM/yyyy) if (chkdate.Length != 11) { // Do something if not. } How hard can it be? It's OK to pinpoint bad code like that but it's not OK not to come up with a useable alternative.

                                          Michael M., mm it-consult dk.

                                          T 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