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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. null vs String.Empty

null vs String.Empty

Scheduled Pinned Locked Moved The Lounge
c++visual-studiocomarchitecturequestion
49 Posts 23 Posters 3 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.
  • C Chris Maunder

    I love it how a discussion on programming techniques invariable leads to a general slamming of VB. Poor VB...

    cheers, Chris Maunder

    CodeProject.com : C++ MVP

    S Offline
    S Offline
    Shog9 0
    wrote on last edited by
    #34

    Chris Maunder wrote:

    Poor VB...

    I have a meeting next week to discuss the redesign of a library, currently implemented in VB.NET and Excel. This is gonna cast a shadow over my entire weekend. No sympathy.

    ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

    1 Reply Last reply
    0
    • Z Zac Howland

      Chris Losinger wrote:

      C++ doesn't have String.Empty.

      Returning a string("") or CString("") would be almost equivalent to String.Empty.

      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #35

      Zac Howland wrote:

      Returning a string("") or CString("") would be almost equivalent to String.Empty.

      Unless your return value is defined as LPCTSTR... at that point, it becomes a logic error. ;P

      ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

      1 Reply Last reply
      0
      • C Chris Maunder

        LOL. Yeah, I could definitely do public bool GetAString(out string aString) but, well... X|

        cheers, Chris Maunder

        CodeProject.com : C++ MVP

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

        Yeah, I really don't like that either, but it does signify that it is a performance hack optimization when you see it in code. Then again I only really care if it is a public method that is going to get reused all over the place. I much prefer string GetString() because it is so much more readable, and I normally don't worry about it too much because I've never run into a situation where throwing exceptions has been a performance problem. 95% of the performance problems I've run into have been lame database code that gets too friendly with the databse server.


        I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

        1 Reply Last reply
        0
        • S Shog9 0

          If by "fails" you mean "encounters a situation where it is completely unable to fulfill the requirements for which it was designed", then i go with Nemanja's technique: throw an exception. Otherwise, it depends on the situation. If the routine is designed such that the caller will need to take specific action in the event of failure, i'll return null. However, if i intend the return value to be used in the same manner regardless of whether the routine is able to actually produce anything interesting (retrieve a column value, format a date, etc.), then i'll return an empty string. In nearly all cases, this is a decision i would make prior to actually implementing the routine. In C++, it would effect my decision to use a return value of [CString|std::string|_bstr_t] vs. [LPCTSTR|char*|BSTR|etc.]. In a .NET language, it would merely change how i documented and called the routine.

          ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

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

          Great, now I have to amend my statement. Yeah, if returning an empty string makes the general case for the client easier then yeah that is the way to go. I'd only do that in user interface code, because functions in other parts of an app should pretend they don't know how they are going to get used.


          I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

          S 1 Reply Last reply
          0
          • A Andy Brummer

            Great, now I have to amend my statement. Yeah, if returning an empty string makes the general case for the client easier then yeah that is the way to go. I'd only do that in user interface code, because functions in other parts of an app should pretend they don't know how they are going to get used.


            I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

            S Offline
            S Offline
            Shog9 0
            wrote on last edited by
            #38

            Andy Brummer wrote:

            I'd only do that in user interface code, because functions in other parts of an app should pretend they don't know how they are going to get used.

            Right. :) I write a lot of UI code, so that tends to be the first scenario that occurs to me.

            ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

            1 Reply Last reply
            0
            • J Jeremy Falcon

              Judah Himango wrote:

              If the function fails for any reason (say, bad input, maybe specifying a negative number as the index), then you throw an exception.

              Even in that instance, I'd still return NULL or true/false. It's the goal of the function do what it's supposed to do and if it can't, even if it gets bad params, let the caller know rather than just throw an exception (unless that exception is used to let the caller [programmer] know rather than just dump text in a message box and hope to the computer gods it fixes itself). The idea is program flow. I wouldn't want mine left up to the UI unless I wanted the UI to hang it. And if that's the case the caller would know to do on a per call basis. This also helps me to reuse the function in a more generic manner. And of course, I do a lot of C programming too. So, try/catch is out the window for me anyway. :-D

              Jeremy Falcon A multithreaded, OpenGL-enabled application.[^]

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #39

              Jeremy Falcon wrote:

              even if it gets bad params

              I'm a believer that once you start tolerating bad input, your whole system gets messed up. In my experience, it's best to stop when you get bad input, otherwise all sorts of things start going wrong. Of course, since you're writing C, exceptions are out of the question for you. The best you can do is return soon as you get bad input, and hope the caller checks your error code.

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Dumbest. Movie. Title. Evaaar. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

              J 1 Reply Last reply
              0
              • C Chris Maunder

                Zac Howland wrote:

                I take it this is in Java?

                <looks around at all the C# code lying about the place, then looks under books and desks for signs of Java code> err, no :D

                cheers, Chris Maunder

                CodeProject.com : C++ MVP

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #40

                Chris Maunder wrote:

                looks around at all the C# code lying about the place

                :sigh: I've used C# so much that I completely forgot it also had a String.Empty ... :doh:

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                1 Reply Last reply
                0
                • C Chris Maunder

                  If you have a function that returns a string, and that function fails, do you prefer to return null or String.Empty? Vote 1 = null Vote 5 = String.Empty

                  cheers, Chris Maunder

                  CodeProject.com : C++ MVP

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

                  Either depending on how critical it is.

                  My responsibility ends where your ability to think for yourself begins.

                  1 Reply Last reply
                  0
                  • J Judah Gabriel Himango

                    Jeremy Falcon wrote:

                    even if it gets bad params

                    I'm a believer that once you start tolerating bad input, your whole system gets messed up. In my experience, it's best to stop when you get bad input, otherwise all sorts of things start going wrong. Of course, since you're writing C, exceptions are out of the question for you. The best you can do is return soon as you get bad input, and hope the caller checks your error code.

                    Tech, life, family, faith: Give me a visit. I'm currently blogging about: Dumbest. Movie. Title. Evaaar. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                    J Offline
                    J Offline
                    Jeremy Falcon
                    wrote on last edited by
                    #42

                    Judah Himango wrote:

                    I'm a believer that once you start tolerating bad input, your whole system gets messed up.

                    Um, nobody said to tolerate it. I said to let the caller know something was bogus. And, letting the caller know a value is messing up a system, I'd wager there were really other aspects in that system(s) that lead to the issues.

                    Judah Himango wrote:

                    Of course, since you're writing C, exceptions are out of the question for you. The best you can do is return soon as you get bad input, and hope the caller checks your error code.

                    Most people tend to use exceptions in a poor way. It's about program flow really.

                    Jeremy Falcon A multithreaded, OpenGL-enabled application.[^]

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      If you have a function that returns a string, and that function fails, do you prefer to return null or String.Empty? Vote 1 = null Vote 5 = String.Empty

                      cheers, Chris Maunder

                      CodeProject.com : C++ MVP

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #43

                      String.Empty, of course. Just like I'd always use Environment.NewLine, even though I know what it is always going to be. Any sort of constant defined by the framework is there to be used, it documents your code and it means that you're conforming to the framework, assuming that one day it may exist on anothe platform where any of those values may concievably change. Just like I'd always use SW_HIDE and not 0, I know it won't change, but it doesn't hurt to do things properly.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                      S 1 Reply Last reply
                      0
                      • C Christian Graus

                        String.Empty, of course. Just like I'd always use Environment.NewLine, even though I know what it is always going to be. Any sort of constant defined by the framework is there to be used, it documents your code and it means that you're conforming to the framework, assuming that one day it may exist on anothe platform where any of those values may concievably change. Just like I'd always use SW_HIDE and not 0, I know it won't change, but it doesn't hurt to do things properly.

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        S Offline
                        S Offline
                        Shog9 0
                        wrote on last edited by
                        #44

                        Christian Graus wrote:

                        Just like I'd always use SW_HIDE and not 0

                        Uh, i think the choice was between String.Empty (== "") and null (== null)...

                        ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

                        C 1 Reply Last reply
                        0
                        • S Shog9 0

                          Christian Graus wrote:

                          Just like I'd always use SW_HIDE and not 0

                          Uh, i think the choice was between String.Empty (== "") and null (== null)...

                          ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #45

                          Oh - that's what I get for responding first thing in the morning. I *always* use string.IsNullOrEmpty to check strings, so the end result for me is the same.

                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                          1 Reply Last reply
                          0
                          • C Chris Maunder

                            If you have a function that returns a string, and that function fails, do you prefer to return null or String.Empty? Vote 1 = null Vote 5 = String.Empty

                            cheers, Chris Maunder

                            CodeProject.com : C++ MVP

                            P Offline
                            P Offline
                            Paul Watson
                            wrote on last edited by
                            #46

                            Null in the Maunderverse but normally an exception in mine. I guess if it was in the context of nullable columns in a database then you could return null for a string return type. But I think a failure should be indicated by an exception, not a null that subsequent code will trip over without additional checking. Then again you could just start using Ruby and stop worrying... ;)

                            regards, Paul Watson Ireland FeedHenry needs you

                            Shog9 wrote:

                            eh, stop bugging me about it, give it a couple of days, see what happens.

                            1 Reply Last reply
                            0
                            • C Chris Maunder

                              I love it how a discussion on programming techniques invariable leads to a general slamming of VB. Poor VB...

                              cheers, Chris Maunder

                              CodeProject.com : C++ MVP

                              J Offline
                              J Offline
                              Jeremy Falcon
                              wrote on last edited by
                              #47

                              It was just too tempting. I don't know. It's like, bashing VB is just there for the taking. :laugh:

                              Jeremy Falcon A multithreaded, OpenGL-enabled application.[^]

                              1 Reply Last reply
                              0
                              • L led mike

                                Jeremy Falcon wrote:

                                Anyone that uses an empty string needs to lay down the VB books IMO

                                That's just foolish.

                                led mike

                                J Offline
                                J Offline
                                Jeremy Falcon
                                wrote on last edited by
                                #48

                                led mike wrote:

                                That's just foolish.

                                Unlike your foolish crap in the SB, mine was also a joke. I suspect in your attempt to "get back at me", reality escaped you. Now, so you don't seem crazy, this is where you reply saying yours was also a joke. But, we both know the reason you said it. And to that I say, next time try understanding what I'm saying when I say you're acting a fool rather than get all butt hurt and learn nothing.

                                Jeremy Falcon A multithreaded, OpenGL-enabled application.[^]

                                L 1 Reply Last reply
                                0
                                • J Jeremy Falcon

                                  led mike wrote:

                                  That's just foolish.

                                  Unlike your foolish crap in the SB, mine was also a joke. I suspect in your attempt to "get back at me", reality escaped you. Now, so you don't seem crazy, this is where you reply saying yours was also a joke. But, we both know the reason you said it. And to that I say, next time try understanding what I'm saying when I say you're acting a fool rather than get all butt hurt and learn nothing.

                                  Jeremy Falcon A multithreaded, OpenGL-enabled application.[^]

                                  L Offline
                                  L Offline
                                  led mike
                                  wrote on last edited by
                                  #49

                                  :laugh: Not at all. I never figured out what yours meant so the next time I saw you post I replied "That's just foolish", it doesn't mean anything. :-D

                                  led mike

                                  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