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. Am I missing Something?

Am I missing Something?

Scheduled Pinned Locked Moved The Weird and The Wonderful
cssquestion
16 Posts 9 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.
  • O outside cosmic

    Am I missing something? Why would you do this?

    protected int GetTransactionLength(string transaction)
    {
    return transaction.Length;
    }

    is this not less code

    transaction.Length

    than this?

    GetTransactionLength(transaction)

    This may seem small, but it's just pushed me over the edge. It comes from a program that should be simple and beautiful but it full if this sort of stuff, it’s driving me nutts!

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

    In case you want to overload it?

    D 1 Reply Last reply
    0
    • P PIEBALDconsult

      In case you want to overload it?

      D Offline
      D Offline
      Diego Martinez
      wrote on last edited by
      #7

      exactly, i find it very useful to create a Getter/Setter for each member (even if it is public) just in case something changes in the future and it needs to be recovered later. Example: if the length now has to adjust, for example, +5 in the current version, simply change the code on GetTranslation(...) { return object.translation+5; } and automatically all the code that refers to this translation is conveniently adjusted. This is no coding horror, but a wise implementation with future alterability. :)

      S 1 Reply Last reply
      0
      • O outside cosmic

        He left.. He was Microsoft certified, I guess there’s just something’s you can’t teach!

        N Offline
        N Offline
        Nagy Vilmos
        wrote on last edited by
        #8

        outside cosmic wrote:

        He left.. He was Microsoft certified

        ftfy


        Panic, Chaos, Destruction. My work here is done.

        1 Reply Last reply
        0
        • O outside cosmic

          Am I missing something? Why would you do this?

          protected int GetTransactionLength(string transaction)
          {
          return transaction.Length;
          }

          is this not less code

          transaction.Length

          than this?

          GetTransactionLength(transaction)

          This may seem small, but it's just pushed me over the edge. It comes from a program that should be simple and beautiful but it full if this sort of stuff, it’s driving me nutts!

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #9

          This is bizzare and overwrought. Perhaps he ment to do something like

          protected int GetTransactionLength(string transaction)
          {
          if(transaction == null)
          return 0;
          return transaction.Length;
          }

          CCC solved so far: 2 (including a Hard One!)

          0 1 Reply Last reply
          0
          • K Keith Barrow

            This is bizzare and overwrought. Perhaps he ment to do something like

            protected int GetTransactionLength(string transaction)
            {
            if(transaction == null)
            return 0;
            return transaction.Length;
            }

            CCC solved so far: 2 (including a Hard One!)

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #10

            And if you get an zero-length string? It's uncommon, but you might want to consider returning -1 instead of zero.

            OSDev :)

            K 1 Reply Last reply
            0
            • 0 0x3c0

              And if you get an zero-length string? It's uncommon, but you might want to consider returning -1 instead of zero.

              OSDev :)

              K Offline
              K Offline
              Keith Barrow
              wrote on last edited by
              #11

              This wasn't meant to be production level code, just a musing into the weird thought-processes of the original coder of this horror :-)!

              0x3c0 wrote:

              And if you get an zero-length string?

              Heavily depends upon the context, but IMO 0 is better as, the need to differentiate between null and "" when getting the length is rare (diffent for the object itself of course).

              0x3c0 wrote:

              It's uncommon

              Nope! string foo = ""; or string foo = string.Empty; Are the best ways to declare a string if you don't already have a value for it. The last time I got into a discussion like this, it really dragged on.... http://www.codeproject.com/Messages/3251798/NULLABLE-type.aspx[^]

              CCC solved so far: 2 (including a Hard One!)

              0 P 2 Replies Last reply
              0
              • K Keith Barrow

                This wasn't meant to be production level code, just a musing into the weird thought-processes of the original coder of this horror :-)!

                0x3c0 wrote:

                And if you get an zero-length string?

                Heavily depends upon the context, but IMO 0 is better as, the need to differentiate between null and "" when getting the length is rare (diffent for the object itself of course).

                0x3c0 wrote:

                It's uncommon

                Nope! string foo = ""; or string foo = string.Empty; Are the best ways to declare a string if you don't already have a value for it. The last time I got into a discussion like this, it really dragged on.... http://www.codeproject.com/Messages/3251798/NULLABLE-type.aspx[^]

                CCC solved so far: 2 (including a Hard One!)

                0 Offline
                0 Offline
                0x3c0
                wrote on last edited by
                #12

                keefb wrote:

                Heavily depends upon the context, but IMO 0 is better as, the need to differentiate between null and "" when getting the length is rare (diffent for the object itself of course).

                You're right; that requirement isn't common. But if I need it, I'd quite like to be able to get it without having to either write another method or altering the original method. It doesn't add much code, and it fits my personal coding style. Plus, if the string is null, then retrieving Length will cause an exception. I don't like blurring cases together when one of them raises an exception.

                OSDev :)

                K 1 Reply Last reply
                0
                • 0 0x3c0

                  keefb wrote:

                  Heavily depends upon the context, but IMO 0 is better as, the need to differentiate between null and "" when getting the length is rare (diffent for the object itself of course).

                  You're right; that requirement isn't common. But if I need it, I'd quite like to be able to get it without having to either write another method or altering the original method. It doesn't add much code, and it fits my personal coding style. Plus, if the string is null, then retrieving Length will cause an exception. I don't like blurring cases together when one of them raises an exception.

                  OSDev :)

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #13

                  I just made a throw-away observation that perhaps the orginal coder meant to perform some form of null checking and got it wrong (possibly by deleting it), then the error was repeated copied and pasted. Personally I work hard to eradicate the nulls in the first place. I tend to check parameters for null when they are passed. Becuase I want code where nulls are the driven out, I beleive in handling null reference exceptions (such as the one that would occur on string.length) as exceptions as a null value is an unexpected value (ie indicates a bug). Or at least I attempt this :-)! That said, if I had to, I'd wrap the string.length as per my method. This has a few of benefits: developers don't have to worry about a special -1 value; No extra code is needed to handle the -1 value (null reference exceptions will, as described above) indicate a true bug ; the result can be displayed in a UI directly as end users would assume the -1 is a bug.

                  CCC solved so far: 2 (including a Hard One!)

                  1 Reply Last reply
                  0
                  • K Keith Barrow

                    This wasn't meant to be production level code, just a musing into the weird thought-processes of the original coder of this horror :-)!

                    0x3c0 wrote:

                    And if you get an zero-length string?

                    Heavily depends upon the context, but IMO 0 is better as, the need to differentiate between null and "" when getting the length is rare (diffent for the object itself of course).

                    0x3c0 wrote:

                    It's uncommon

                    Nope! string foo = ""; or string foo = string.Empty; Are the best ways to declare a string if you don't already have a value for it. The last time I got into a discussion like this, it really dragged on.... http://www.codeproject.com/Messages/3251798/NULLABLE-type.aspx[^]

                    CCC solved so far: 2 (including a Hard One!)

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

                    keefb wrote:

                    Are the best ways to declare a string if you don't already have a value for it.

                    Of course that depends on the situation, but in general I disagree; I prefer null or uninitialized references. As a particular situation: What I was working on today involves parsing some user input. For one of the parameters it's OK for the user to not specify a value (a default value will be provided later), but an empty value is not allowed. (It's possible that I'll allow an empty value in the future.) I initialize the variable to null and only assign a value if the user provided a non-empty value. Another parameter is similar, but an empty value is allowed.

                    1 Reply Last reply
                    0
                    • D Diego Martinez

                      exactly, i find it very useful to create a Getter/Setter for each member (even if it is public) just in case something changes in the future and it needs to be recovered later. Example: if the length now has to adjust, for example, +5 in the current version, simply change the code on GetTranslation(...) { return object.translation+5; } and automatically all the code that refers to this translation is conveniently adjusted. This is no coding horror, but a wise implementation with future alterability. :)

                      S Offline
                      S Offline
                      Steven Kirk
                      wrote on last edited by
                      #15

                      PLEASE tell me you're joking!?!? ;)

                      1 Reply Last reply
                      0
                      • O outside cosmic

                        Am I missing something? Why would you do this?

                        protected int GetTransactionLength(string transaction)
                        {
                        return transaction.Length;
                        }

                        is this not less code

                        transaction.Length

                        than this?

                        GetTransactionLength(transaction)

                        This may seem small, but it's just pushed me over the edge. It comes from a program that should be simple and beautiful but it full if this sort of stuff, it’s driving me nutts!

                        J Offline
                        J Offline
                        Jorg DD
                        wrote on last edited by
                        #16

                        That's correct, I could not imagine why someone would do it like this.

                        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