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

    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

    N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #5

    Chris Maunder wrote:

    do you prefer to return null or String.Empty?

    Neither. If a function "fails", I simply throw an exception.

    Programming Blog utf8-cpp

    D 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

      D Offline
      D Offline
      Dario Solera
      wrote on last edited by
      #6

      I'd say it could be more correct to return String.Empty, but I use null. Actually I don't know what is "phisically" String.Empty, but I guess it's an instance. If you return a null, at least you save memory and therefore less GC calls are needed (especially because you're using strings). IMHO.

      ________________________________________________ Tozzi is right: Gaia is getting rid of us. Personal Blog [ITA] - Tech Blog [ENG] Developing ScrewTurn Wiki 1.0 final, now in English, Italian and German.

      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
        Chris Losinger
        wrote on last edited by
        #7

        C++ doesn't have String.Empty.

        image processing | blogging

        N Z 2 Replies Last reply
        0
        • N Nemanja Trifunovic

          Chris Maunder wrote:

          do you prefer to return null or String.Empty?

          Neither. If a function "fails", I simply throw an exception.

          Programming Blog utf8-cpp

          D Offline
          D Offline
          Dario Solera
          wrote on last edited by
          #8

          Nemanja Trifunovic wrote:

          Neither. If a function "fails", I simply throw an exception.

          It could be correct, if the method actually fails. A null indicates some "void" result, I guess, and not necessarily a failure or error. :)

          ________________________________________________ Tozzi is right: Gaia is getting rid of us. Personal Blog [ITA] - Tech Blog [ENG] Developing ScrewTurn Wiki 1.0 final, now in English, Italian and German.

          K N 2 Replies 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

            G Offline
            G Offline
            Graham Bradshaw
            wrote on last edited by
            #9

            Neither. I would never mix the return value and the success/fail. In MFC/C++, I'd do that as a function that returned a BOOL, indicating whether it worked or not, and a reference to a CString as a parameter.

            1 Reply Last reply
            0
            • Z Zac Howland

              I take it this is in Java? Or are you talking about just in general?

              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

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

              String.Empty is in the .NET framework

              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

              N 1 Reply Last reply
              0
              • C Chris Losinger

                C++ doesn't have String.Empty.

                image processing | blogging

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #11

                Chris Losinger wrote:

                C++ doesn't have String.Empty.

                It does if you turn on /clr - and then you have String::Empty :rolleyes:

                Regards, Nish


                Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                1 Reply Last reply
                0
                • C Chris Losinger

                  C++ doesn't have String.Empty.

                  image processing | blogging

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

                  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 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

                    D Offline
                    D Offline
                    Douglas Troy
                    wrote on last edited by
                    #13

                    Not sure it matters, since you can just use the following check:

                    if (!string.IsNullOrEmpty(MyReturnStringValue)) 
                    { 
                        ... 
                    }
                    

                    But I generally return empty strings ...


                    :..::. Douglas H. Troy ::..
                    Bad Astronomy |VCF|wxWidgets|WTL

                    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

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

                      If the function truely failed, that's what exceptions are for. If the function has functioned properly (has not failed) but the there is no result, that's when you return null. Say you had a function like this: public string GetNodeTextAtIndex(int index) { ... } If the function fails for any reason (say, bad input, maybe specifying a negative number as the index), then you throw an exception. If the function otherwise succeeds, but doesn't find any node at that index, you return null.

                      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

                      K J 2 Replies Last reply
                      0
                      • D Dario Solera

                        Nemanja Trifunovic wrote:

                        Neither. If a function "fails", I simply throw an exception.

                        It could be correct, if the method actually fails. A null indicates some "void" result, I guess, and not necessarily a failure or error. :)

                        ________________________________________________ Tozzi is right: Gaia is getting rid of us. Personal Blog [ITA] - Tech Blog [ENG] Developing ScrewTurn Wiki 1.0 final, now in English, Italian and German.

                        K Offline
                        K Offline
                        Kevin McFarlane
                        wrote on last edited by
                        #15

                        If the method actually fails then throwing an exception is appropriate. But if the method doesn't fail then returning String.Empty or null could be valid depending on the context. However, in general, I like to interpret null as either "not initialised" or indicative of an error. Ideally the source code should describe exactly what it means when it returns an empty string or or a null.

                        Kevin

                        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
                          Colin Angus Mackay
                          wrote on last edited by
                          #16

                          It depends. It may be valid to return an empty string, in which case you can't use it to signify a failure, in which case null is the best option.


                          Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | 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

                            A Offline
                            A Offline
                            Anish M
                            wrote on last edited by
                            #17

                            May be null, but for sure not an Empty String

                            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

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

                              Chris Maunder wrote:

                              and that function fails

                              Based on the responses so far you need to define "fails". IMHO failure indicates an exceptional condition and therefore an exception should be thrown. However if you are referring to a valid outcome of an operation I would likely choose null over an empty string for two reasons: 1) it is more efficient 2) Since the caller must "check" the return value to determine the outcome, it is part of the contract and documented, and therefore is not significant in ways other than efficiency.

                              led mike

                              1 Reply Last reply
                              0
                              • J Judah Gabriel Himango

                                If the function truely failed, that's what exceptions are for. If the function has functioned properly (has not failed) but the there is no result, that's when you return null. Say you had a function like this: public string GetNodeTextAtIndex(int index) { ... } If the function fails for any reason (say, bad input, maybe specifying a negative number as the index), then you throw an exception. If the function otherwise succeeds, but doesn't find any node at that index, you return null.

                                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

                                K Offline
                                K Offline
                                Kevin McFarlane
                                wrote on last edited by
                                #19

                                Good summary, Judah.:)

                                Kevin

                                1 Reply Last reply
                                0
                                • J Judah Gabriel Himango

                                  String.Empty is in the .NET framework

                                  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

                                  N Offline
                                  N Offline
                                  Nemanja Trifunovic
                                  wrote on last edited by
                                  #20

                                  Judah Himango wrote:

                                  String.Empty is in the .NET framework

                                  I know that. I usually don't criticize things I don't know :) [EDIT} So you answered to Chris L. not me. Another CP forum joke[/EDIT]

                                  Programming Blog utf8-cpp

                                  1 Reply Last reply
                                  0
                                  • D Dario Solera

                                    Nemanja Trifunovic wrote:

                                    Neither. If a function "fails", I simply throw an exception.

                                    It could be correct, if the method actually fails. A null indicates some "void" result, I guess, and not necessarily a failure or error. :)

                                    ________________________________________________ Tozzi is right: Gaia is getting rid of us. Personal Blog [ITA] - Tech Blog [ENG] Developing ScrewTurn Wiki 1.0 final, now in English, Italian and German.

                                    N Offline
                                    N Offline
                                    Nemanja Trifunovic
                                    wrote on last edited by
                                    #21

                                    Dario Solera wrote:

                                    It could be correct, if the method actually fails.

                                    Chris explicitelly said "fails", whatever he meant by that :)

                                    Programming Blog utf8-cpp

                                    D 1 Reply Last reply
                                    0
                                    • N Nemanja Trifunovic

                                      Dario Solera wrote:

                                      It could be correct, if the method actually fails.

                                      Chris explicitelly said "fails", whatever he meant by that :)

                                      Programming Blog utf8-cpp

                                      D Offline
                                      D Offline
                                      Dario Solera
                                      wrote on last edited by
                                      #22

                                      Nemanja Trifunovic wrote:

                                      Chris explicitelly said "fails", whatever he meant by that

                                      Yes, sorry, I noticed it too late... :)

                                      ________________________________________________ Tozzi is right: Gaia is getting rid of us. Personal Blog [ITA] - Tech Blog [ENG] Developing ScrewTurn Wiki 1.0 final, now in English, Italian and German.

                                      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

                                        realJSOPR Offline
                                        realJSOPR Offline
                                        realJSOP
                                        wrote on last edited by
                                        #23

                                        I return an empty string.

                                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                        -----
                                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                                        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

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

                                          I would definitely never use String.Empty. Using a valid string as an error code is just crazy talk. I would normally throw an exception, unless "failure" is common or profiling warranted removing the exception. Then I would return a flag and make the string a ref or an out parameter. I'm concerned that the question itself might be a lingering symptom of your VBScript hacking days. Just open an old C++ COM app and breathe in the putrid stench of all those HRESULTS and the path ahead should become clear. :-D


                                          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

                                          C 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