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 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.
  • E Offline
    E Offline
    Eytukan
    wrote on last edited by
    #1

    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.

    A G A L _ 14 Replies 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.

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

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

        A Offline
        A Offline
        Alberto Bar Noy
        wrote on last edited by
        #3

        More "professional" is

        void GetAllItems(list& lst)

        IMO

        Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

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

          A Offline
          A Offline
          Aamir Butt
          wrote on last edited by
          #4

          Second one for three reasons: 1. It is closer to functional programming principles. 2. Allows method chaining. 3. If your function is GetSomething it should return Something

          A year spent in artificial intelligence is enough to make one believe in God

          T T 2 Replies Last reply
          0
          • A Aamir Butt

            Second one for three reasons: 1. It is closer to functional programming principles. 2. Allows method chaining. 3. If your function is GetSomething it should return Something

            A year spent in artificial intelligence is enough to make one believe in God

            T Offline
            T Offline
            TorstenH
            wrote on last edited by
            #5

            I second to use the second one.

            regards Torsten When I'm not working

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

              It depends.

              1 Reply Last reply
              0
              • A Aamir Butt

                Second one for three reasons: 1. It is closer to functional programming principles. 2. Allows method chaining. 3. If your function is GetSomething it should return Something

                A year spent in artificial intelligence is enough to make one believe in God

                T Offline
                T Offline
                Tim Groven
                wrote on last edited by
                #7

                Agreed!

                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.

                  _ Offline
                  _ Offline
                  __yash__
                  wrote on last edited by
                  #8

                  LIST_GET_STATUS GetAllItems( LIST *pLst )

                  I would use this where LIST_GET_STATUS would either return SUCCESS or a failure code.

                  E C 2 Replies 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.

                    M Offline
                    M Offline
                    Maximilien
                    wrote on last edited by
                    #9

                    Language ? In C++ (pre 11), I'd do

                    void GetAllItems(list& lst)

                    c++11, I'd do (I think that's the good syntax):

                    list GetAllItems();

                    That will be a move operation on the list.

                    Watched code never compiles.

                    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.

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

                      Most commonly I'd use the second one (unless I've a particular need for the first, actually) BUT - - - it would be declared as list* GetAllItems(). [or in managed C++, list ^GetAllItems() ]. There's another option occasionally of use: Declaring the list at a scope higher (outside) the call. Then one can feed back different info, such as boolean as a success flag, or an int count of items in the list. This depends a lot on where I need to use the list and the type of persistence I require. This makes the call essentially an init or load method. Does anyone else ever use this type of method?

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

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

                        The latter. I try to avoid void functions; there should be something to return, at least a boolean (for success or failure) or a count of items, but returning the collection makes the most sense, even if you also pass in the collection to fill or add to: list& GetAllItems(list& lst) the caller can then use or ignore the return value.

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

                          I'd prefer the second one. 1. It requires less code (I don't even have to declare a variable to hold the results if all I have to do is to pass it on to another method - call chaining). 2. 'Get' should 'get' something, not load or fill something.

                          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.

                            J Offline
                            J Offline
                            John M Drescher
                            wrote on last edited by
                            #13

                            bool GetAllItems(list& lstItems)

                            John

                            1 Reply Last reply
                            0
                            • A Alberto Bar Noy

                              More "professional" is

                              void GetAllItems(list& lst)

                              IMO

                              Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

                              B Offline
                              B Offline
                              BobJanova
                              wrote on last edited by
                              #14

                              Any reason for that?

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

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #15

                                It depends on the language, to some extent. In most modern languages I'd definitely do the second one, and I think that a method called 'GetXxx' should always return Xxx. However, the second way almost guarantees that you are having to create a new list inside that method, and there may be situations where that is important. In that case, I'd at least allow passing in a list, but I probably wouldn't call it GetXxx in that case. If memory was critical, I'd do something like (pseudo)

                                list& GetAllItems(){
                                return FillWithAllItems(new list());
                                }

                                list& FillWithAllItems(list& input){
                                input.add(item1);
                                // etc
                                return input;
                                }

                                ... and in the positions where you want to re-use an existing list, you can do so, but if you don't care, you can still use the more user-friendly Get method. Ideologically returning the list is correct because

                                • it is closer to functional programming
                                • you are logically getting a list, so that's what the method should do
                                • you can chain method calls that way

                                ... but in certain circumstances it may be necessary to re-use memory and pass an instance in. I've actually faced exactly this situation with Vector.<Vector3D>, and maths operations on my vector class, in Flash (AS3). I started with the nice way and then added the option to reuse instances for efficiency reasons.

                                1 Reply Last reply
                                0
                                • _ __yash__

                                  LIST_GET_STATUS GetAllItems( LIST *pLst )

                                  I would use this where LIST_GET_STATUS would either return SUCCESS or a failure code.

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

                                  __yash__ wrote:

                                  LIST_GET_STATUS GetAllItems( LIST *pLst )

                                  Well, using LIST* might make the code look more manlier, but I wouldn't like the idea of doing this.

                                  LIST_GET_STATUS GetAllItems( LIST *pLst )
                                  {

                                  if(NULL!=pLst)//forgetting this would give you a bang at times!
                                  {
                                  }
                                  }

                                  Hence, & > * .

                                  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

                                    __yash__ wrote:

                                    LIST_GET_STATUS GetAllItems( LIST *pLst )

                                    Well, using LIST* might make the code look more manlier, but I wouldn't like the idea of doing this.

                                    LIST_GET_STATUS GetAllItems( LIST *pLst )
                                    {

                                    if(NULL!=pLst)//forgetting this would give you a bang at times!
                                    {
                                    }
                                    }

                                    Hence, & > * .

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

                                    _ Offline
                                    _ Offline
                                    __yash__
                                    wrote on last edited by
                                    #17

                                    It's been really long since I last worked with C++, hence posted in C. In any case my point was to have a return value also so as to know whether

                                    GetAllItems()

                                    succeeded or failed. If failed, then what was the reason.

                                    1 Reply Last reply
                                    0
                                    • B BobJanova

                                      Any reason for that?

                                      C Offline
                                      C Offline
                                      CPallini
                                      wrote on last edited by
                                      #18

                                      "More Professional" :-D

                                      Veni, vidi, vici.

                                      1 Reply Last reply
                                      0
                                      • _ __yash__

                                        LIST_GET_STATUS GetAllItems( LIST *pLst )

                                        I would use this where LIST_GET_STATUS would either return SUCCESS or a failure code.

                                        C Offline
                                        C Offline
                                        CPallini
                                        wrote on last edited by
                                        #19

                                        There's NULL for that.

                                        Veni, vidi, vici.

                                        _ 1 Reply Last reply
                                        0
                                        • C CPallini

                                          There's NULL for that.

                                          Veni, vidi, vici.

                                          _ Offline
                                          _ Offline
                                          __yash__
                                          wrote on last edited by
                                          #20

                                          You could have more than "binary" number of reasons why something failed. An error code can be used to indicate just that.

                                          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