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. Clever Code
  4. Cutesy string code I wrote today

Cutesy string code I wrote today

Scheduled Pinned Locked Moved Clever Code
csharpcomhelp
15 Posts 8 Posters 9 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
    peterchen
    wrote on last edited by
    #1

    It's not really a bug, just something weird that did flow out my fingers today. It looks so bad I want to declare it as a "coding horror", but it may arguably be useful.

    m_string = s.ToLowerInvariant();
    if (m_string == s)
    m_string = s;

    The language is C#.


    We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
    My first real C# project | Linkify!|FoldWithUs! | sighist

    C R D A 4 Replies Last reply
    0
    • P peterchen

      It's not really a bug, just something weird that did flow out my fingers today. It looks so bad I want to declare it as a "coding horror", but it may arguably be useful.

      m_string = s.ToLowerInvariant();
      if (m_string == s)
      m_string = s;

      The language is C#.


      We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
      My first real C# project | Linkify!|FoldWithUs! | sighist

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

      If you can find a use for that, I'd be worried. Unless you're trying to reprogram Vista...:laugh: I see what you mean about the horror. Talk about pointless. If x=y, then set x=y. Why not set y=x at the same time? Just to make sure...:-P


      Yet another spam post on yet another forum! I am the lazy one, who sleeps as it suits him, codes what he wishes, and has many years to look forward to. I love being a student.

      P D 2 Replies Last reply
      0
      • P peterchen

        It's not really a bug, just something weird that did flow out my fingers today. It looks so bad I want to declare it as a "coding horror", but it may arguably be useful.

        m_string = s.ToLowerInvariant();
        if (m_string == s)
        m_string = s;

        The language is C#.


        We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
        My first real C# project | Linkify!|FoldWithUs! | sighist

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        peterchen wrote:

        it may arguably be useful.

        I am not familiar enough with C# to understand why this can be useful. Unless you modify the type of m_string with the assignment m_string=s or something like that. Or you set a breakpoint on the last line and then catch the cases when requiring to "ToLowerInvariant()" was not necessary. :confused:

        Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

        D 1 Reply Last reply
        0
        • P peterchen

          It's not really a bug, just something weird that did flow out my fingers today. It looks so bad I want to declare it as a "coding horror", but it may arguably be useful.

          m_string = s.ToLowerInvariant();
          if (m_string == s)
          m_string = s;

          The language is C#.


          We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
          My first real C# project | Linkify!|FoldWithUs! | sighist

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          peterchen wrote:

          m_string = s.ToLowerInvariant(); if (m_string == s) m_string = s;

          Other way is

          m_string = string.Intern(s.ToLowerInvariant());
          

          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          P 1 Reply Last reply
          0
          • R Rage

            peterchen wrote:

            it may arguably be useful.

            I am not familiar enough with C# to understand why this can be useful. Unless you modify the type of m_string with the assignment m_string=s or something like that. Or you set a breakpoint on the last line and then catch the cases when requiring to "ToLowerInvariant()" was not necessary. :confused:

            Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #5

            Strings are immutable. String.ToLowerInvariant(), just like everything else, creates new instance of string. You can try this: string s = "testme"; string s2 = s.ToLowerInvariant(); Console.Write(s==s2); Console.Write(string.ReferenceEquals(s,s2)); First is true, second is false. If you do what peterchen did, you will get true in both cases. By assigning original string to m_string, you'll get same references. In same cases this could be usefull, I think string.Equals(string) compares strings byte by byte, comparing references is much faster.


            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

            R 1 Reply Last reply
            0
            • D DavidNohejl

              peterchen wrote:

              m_string = s.ToLowerInvariant(); if (m_string == s) m_string = s;

              Other way is

              m_string = string.Intern(s.ToLowerInvariant());
              

              "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

              P Offline
              P Offline
              peterchen
              wrote on last edited by
              #6

              That at least looks better than what I wrote! You are right, there are two reasons, both performance related: First, I expect string.operator== to do a "reference equals" comparison first. Second, the "outer" string might easily outlive the local one (belong to an older generation, referenced by multiple string instances) etc. But I wouldn't necessary want to add the string ot the intern pool. But both ways optimize for a specific case, so our milage may differ.


              We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
              My first real C# project | Linkify!|FoldWithUs! | sighist

              S M 2 Replies Last reply
              0
              • C codemunkeh

                If you can find a use for that, I'd be worried. Unless you're trying to reprogram Vista...:laugh: I see what you mean about the horror. Talk about pointless. If x=y, then set x=y. Why not set y=x at the same time? Just to make sure...:-P


                Yet another spam post on yet another forum! I am the lazy one, who sleeps as it suits him, codes what he wishes, and has many years to look forward to. I love being a student.

                P Offline
                P Offline
                peterchen
                wrote on last edited by
                #7

                It's faster ;) (at least, it may be, see dnh and my reply below)


                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                My first real C# project | Linkify!|FoldWithUs! | sighist

                1 Reply Last reply
                0
                • P peterchen

                  That at least looks better than what I wrote! You are right, there are two reasons, both performance related: First, I expect string.operator== to do a "reference equals" comparison first. Second, the "outer" string might easily outlive the local one (belong to an older generation, referenced by multiple string instances) etc. But I wouldn't necessary want to add the string ot the intern pool. But both ways optimize for a specific case, so our milage may differ.


                  We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                  My first real C# project | Linkify!|FoldWithUs! | sighist

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #8

                  string.operator== do a content comparision but I am not sure about object o1 = "unun"; object o2 = "un" + "un"; o1 == o2 .. result?

                  P 1 Reply Last reply
                  0
                  • S Super Lloyd

                    string.operator== do a content comparision but I am not sure about object o1 = "unun"; object o2 = "un" + "un"; o1 == o2 .. result?

                    P Offline
                    P Offline
                    peterchen
                    wrote on last edited by
                    #9

                    I just meant that it would make sense to make a reference equality comparison first, before doing the contents comparison. (I'd actually be surprised if it wouldn't. since the reference comparison is so much cheaper). For your example: both objects are identical, since "un" + "un" gets evaluated at compile time, and so bot reference the interned instance of "unun". if you change that slightly, however: object o1 = "unun"; object o2 = "un"; o2 = (string)o2 + "un"; which forces the concatenation to be one at runtime. Since operator== isn't virtual, all it does here is a reference comparison - in which the objects are not equal.


                    We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                    My first real C# project | Linkify!|FoldWithUs! | sighist

                    1 Reply Last reply
                    0
                    • D DavidNohejl

                      Strings are immutable. String.ToLowerInvariant(), just like everything else, creates new instance of string. You can try this: string s = "testme"; string s2 = s.ToLowerInvariant(); Console.Write(s==s2); Console.Write(string.ReferenceEquals(s,s2)); First is true, second is false. If you do what peterchen did, you will get true in both cases. By assigning original string to m_string, you'll get same references. In same cases this could be usefull, I think string.Equals(string) compares strings byte by byte, comparing references is much faster.


                      "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                      R Offline
                      R Offline
                      Rage
                      wrote on last edited by
                      #10

                      That makes plenty of sense. Thank you.

                      Constantly "Saving the day" should be taken as a sign of organizational dysfunction rather than individual skill - Ryan Roberts[^]

                      1 Reply Last reply
                      0
                      • C codemunkeh

                        If you can find a use for that, I'd be worried. Unless you're trying to reprogram Vista...:laugh: I see what you mean about the horror. Talk about pointless. If x=y, then set x=y. Why not set y=x at the same time? Just to make sure...:-P


                        Yet another spam post on yet another forum! I am the lazy one, who sleeps as it suits him, codes what he wishes, and has many years to look forward to. I love being a student.

                        D Offline
                        D Offline
                        dave dolan
                        wrote on last edited by
                        #11

                        thou shalt think about thy response before thou flameth. reference comparison is MUCH faster, as in O(1) vs O(n)...

                        1 Reply Last reply
                        0
                        • P peterchen

                          That at least looks better than what I wrote! You are right, there are two reasons, both performance related: First, I expect string.operator== to do a "reference equals" comparison first. Second, the "outer" string might easily outlive the local one (belong to an older generation, referenced by multiple string instances) etc. But I wouldn't necessary want to add the string ot the intern pool. But both ways optimize for a specific case, so our milage may differ.


                          We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                          My first real C# project | Linkify!|FoldWithUs! | sighist

                          M Offline
                          M Offline
                          Member 96
                          wrote on last edited by
                          #12

                          Not to jump in with the usual anal programmer response or anything, but since everyone else seems to...why is this a string in the first place? It looks in context like the string is being used as a token of some kind which could be more efficiently done with an enum or int.


                          "110%" - it's the new 70%

                          P 1 Reply Last reply
                          0
                          • M Member 96

                            Not to jump in with the usual anal programmer response or anything, but since everyone else seems to...why is this a string in the first place? It looks in context like the string is being used as a token of some kind which could be more efficiently done with an enum or int.


                            "110%" - it's the new 70%

                            P Offline
                            P Offline
                            peterchen
                            wrote on last edited by
                            #13

                            It's a token to be replaced in a string, but tokens should be ignore case. (i.e. I have a map token-replacement, and a very suboptimal routine doing the replacement). So yes, even though I might end up with lots of tokens, the optimization is irrelevant for this use case, it probably was some mental acrobatics only :)


                            We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                            My first real C# project | Linkify!|FoldWithUs! | sighist

                            1 Reply Last reply
                            0
                            • P peterchen

                              It's not really a bug, just something weird that did flow out my fingers today. It looks so bad I want to declare it as a "coding horror", but it may arguably be useful.

                              m_string = s.ToLowerInvariant();
                              if (m_string == s)
                              m_string = s;

                              The language is C#.


                              We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                              My first real C# project | Linkify!|FoldWithUs! | sighist

                              A Offline
                              A Offline
                              Anna Jayne Metcalfe
                              wrote on last edited by
                              #14

                              *** Insufficent Caffeine Error *** ;P Any decent static analyser should spot that one, though! If it were C or C++ you'd see PC-Lint issue 774 ("Boolean within 'expression' always evaluates to [True/False]"); OTTOMH* I couldn't tell you what the corresponding issue in FxCop is though. * Guess

                              Anna :rose: Linting the day away :cool: Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"

                              P 1 Reply Last reply
                              0
                              • A Anna Jayne Metcalfe

                                *** Insufficent Caffeine Error *** ;P Any decent static analyser should spot that one, though! If it were C or C++ you'd see PC-Lint issue 774 ("Boolean within 'expression' always evaluates to [True/False]"); OTTOMH* I couldn't tell you what the corresponding issue in FxCop is though. * Guess

                                Anna :rose: Linting the day away :cool: Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"

                                P Offline
                                P Offline
                                peterchen
                                wrote on last edited by
                                #15

                                Anna-Jayne Metcalfe wrote:

                                *** Insufficent Caffeine Error ***

                                Not really - it was intentional, it relies on a subtle difference between value equality (tested with ==) and reference equality ('enforced' with =)


                                We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
                                My first real C# project | Linkify!|FoldWithUs! | sighist

                                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