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. The Lounge
  3. Your choice?

Your choice?

Scheduled Pinned Locked Moved The Lounge
jsonquestion
31 Posts 17 Posters 1 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.
  • E Eytukan

    When you define an API like

    GetAllItems()

    You'll write

    void GetAllItems(list& lst)

    or

    list& GetAllItems()

    What's your choice? Which one looks more API-consumer friendly?

    Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

    T Offline
    T Offline
    TheGreatAndPowerfulOz
    wrote on last edited by
    #22

    Assuming this is C++, then certainly not the second since it returns a reference to something that I assume would be local (on the stack -- upon return the stack is destroyed) or a member of the class (violation of data hiding). In modern C++, I'd use

    list GetAllItems()

    because it's a move operation (no copy ctor is used), and throw an exception if there were an error. In pre-modern C++, I'd use

    void GetAllItems(list& items)

    because returning an object causes a copy to be made. (And throw an exception if there were an error.)

    If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
    You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

    W 1 Reply Last reply
    0
    • G GParkings

      to me a 'Getfoo()' should always return a foo. if you want something to populate a collection argument it should be 'FillFoo(foo fooToFill)' choice of option depends on the details of how you expect the method to be used. if you expect the user to be working with a pre-existing collection or to call the method several times for the same collection then the latter makes sense, if you expect a one-shot 'gimme the stuffs' useage (used only to create the collection) then the former is better

      Pedis ex oris Quidquid latine dictum sit, altum sonatur

      W Offline
      W Offline
      wizardzz
      wrote on last edited by
      #23

      Wow, the tribe has spoken and you are correct!

      "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

      G 1 Reply Last reply
      0
      • W wizardzz

        Wow, the tribe has spoken and you are correct!

        "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

        G Offline
        G Offline
        GParkings
        wrote on last edited by
        #24

        had to happen at least once, eh?

        Pedis ex oris Quidquid latine dictum sit, altum sonatur

        1 Reply Last reply
        0
        • T TheGreatAndPowerfulOz

          Assuming this is C++, then certainly not the second since it returns a reference to something that I assume would be local (on the stack -- upon return the stack is destroyed) or a member of the class (violation of data hiding). In modern C++, I'd use

          list GetAllItems()

          because it's a move operation (no copy ctor is used), and throw an exception if there were an error. In pre-modern C++, I'd use

          void GetAllItems(list& items)

          because returning an object causes a copy to be made. (And throw an exception if there were an error.)

          If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
          You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun

          W Offline
          W Offline
          W Balboos GHB
          wrote on last edited by
          #25

          Interesting. My habits in C++ (new or old) would be to return a pointer to the list. If managed, then it will persist. If unmanaged, then it will stay until I delete it. Returning non-pointers is usually relegated to either native types or structures, but 'never' to an array. For array-returns, null for failures (particularly if they're an expected result) possibly via an exception caught in the callee, and throwing an exception to the caller only if I need to diagnose/handle true error conditions.

          "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

          "As far as we know, our computer has never had an undetected error." - Weisert

          "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

          1 Reply Last reply
          0
          • E Eytukan

            When you define an API like

            GetAllItems()

            You'll write

            void GetAllItems(list& lst)

            or

            list& GetAllItems()

            What's your choice? Which one looks more API-consumer friendly?

            Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

            C Offline
            C Offline
            Chris Maunder
            wrote on last edited by
            #26

            VuNic wrote:

            void GetAllItems(list& lst)

            Why call it "Get..." when you're actually setting the ref value? A Get should return something.

            cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

            E 1 Reply Last reply
            0
            • C Chris Maunder

              VuNic wrote:

              void GetAllItems(list& lst)

              Why call it "Get..." when you're actually setting the ref value? A Get should return something.

              cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #27

              It's like pushing a mug to the bar attender & saying "get" me some beer. Though there's some "set" it's actually a Get. :)

              Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

              C 1 Reply Last reply
              0
              • E Eytukan

                It's like pushing a mug to the bar attender & saying "get" me some beer. Though there's some "set" it's actually a Get. :)

                Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

                C Offline
                C Offline
                Chris Maunder
                wrote on last edited by
                #28

                I always ask my bartender to instantiate a new Mug(Beer);.

                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                E L 2 Replies Last reply
                0
                • C Chris Maunder

                  I always ask my bartender to instantiate a new Mug(Beer);.

                  cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                  E Offline
                  E Offline
                  Eytukan
                  wrote on last edited by
                  #29

                  Hidden implementation of your optimized bartender.

                  void* operator new(size_t size)
                  {
                  static Mug m;
                  return &m;
                  }

                  Hope there were less people before you :rolleyes:

                  Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

                  1 Reply Last reply
                  0
                  • E Eytukan

                    When you define an API like

                    GetAllItems()

                    You'll write

                    void GetAllItems(list& lst)

                    or

                    list& GetAllItems()

                    What's your choice? Which one looks more API-consumer friendly?

                    Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #30

                    The first( passing a reference in) allows the caller to control how memory is allocated especially when using stl collections which allow a custom allocator.

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      I always ask my bartender to instantiate a new Mug(Beer);.

                      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #31

                      Chris Maunder wrote:

                      I always ask my bartender to instantiate a new Mug(Beer);.

                      I expect to see this instantiate sequence in action, live in June.

                      Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

                      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