method that returns either MatchCollection or nothing?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    method that returns either MatchCollection or nothing?

    Is it safe to make a method that returns a match collection or nothing? or
    is it better to just return a match collection and have the code outside the
    method validate that match collection is empty or not?



  • Tom Shelton

    #2
    Re: method that returns either MatchCollection or nothing?

    On 2008-10-15, Andy B <a_borka@sbcglo bal.netwrote:
    Is it safe to make a method that returns a match collection or nothing? or
    is it better to just return a match collection and have the code outside the
    method validate that match collection is empty or not?
    Personally, I prefere when a method returns an empty collection then if it
    returns nothing. Returning a null collection (Nothing in VB), usually
    complicates the client code - because now you need to do checks to avoid a
    NullReferenceEx ception. For example (Warning Air Code!):

    ' method returns a null (nothing) collection
    Dim theCollection As List<SomeEntity = GetCollection()
    If Not theCollection Is Nothing Then
    For Each e As SomeEntity In theCollection
    ' do stuff
    Next
    End If

    ' method returns an empty collection
    For Each e As SomeEntity In GetCollection()
    ' do stuff
    Next

    Anyway, there you have it :)

    --
    Tom Shelton

    Comment

    • Andy B

      #3
      Re: method that returns either MatchCollection or nothing?

      So I shouldn't be doing something like:

      dim matches as MatchCollection = ReturnMatches(s tuff)

      if matches is nothing
      return nothing
      else
      return matches


      Instead I should:

      return (matches as MatchCollection = ReturnMatches(s tuff))
      "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
      news:QJWdnZjqW4 4g3mvVnZ2dnUVZ_ obinZ2d@comcast .com...
      On 2008-10-15, Andy B <a_borka@sbcglo bal.netwrote:
      >Is it safe to make a method that returns a match collection or nothing?
      >or
      >is it better to just return a match collection and have the code outside
      >the
      >method validate that match collection is empty or not?
      >
      Personally, I prefere when a method returns an empty collection then if it
      returns nothing. Returning a null collection (Nothing in VB), usually
      complicates the client code - because now you need to do checks to avoid a
      NullReferenceEx ception. For example (Warning Air Code!):
      >
      ' method returns a null (nothing) collection
      Dim theCollection As List<SomeEntity = GetCollection()
      If Not theCollection Is Nothing Then
      For Each e As SomeEntity In theCollection
      ' do stuff
      Next
      End If
      >
      ' method returns an empty collection
      For Each e As SomeEntity In GetCollection()
      ' do stuff
      Next
      >
      Anyway, there you have it :)
      >
      --
      Tom Shelton

      Comment

      • Jack Jackson

        #4
        Re: method that returns either MatchCollection or nothing?

        On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_borka@sbcglo bal.net>
        wrote:
        >Is it safe to make a method that returns a match collection or nothing? or
        >is it better to just return a match collection and have the code outside the
        >method validate that match collection is empty or not?
        Always returning a collection (empty if nothing found) may make it
        less likely that calling code will be written incorrectly. If you
        return Nothing and a caller fails to check for Nothing an exception
        will occur.

        If you always return a collection, then depending on how the method
        works, returning an empty collection might mean that it will be
        necessary to create an empty collection that would not have to be
        created if Nothing was returned.

        So there might be a trade-off between safer code and creating more
        objects.

        If it were me I would probably return Nothing, and define the method
        as returning either Nothing or a non-empty collection so that callers
        don't have to make two tests to check if anything was returned.

        Comment

        • Tom Shelton

          #5
          Re: method that returns either MatchCollection or nothing?

          On 2008-10-16, Jack Jackson <jjackson@cinno vations.netwrot e:
          On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_borka@sbcglo bal.net>
          wrote:
          >
          >>Is it safe to make a method that returns a match collection or nothing? or
          >>is it better to just return a match collection and have the code outside the
          >>method validate that match collection is empty or not?
          >
          Always returning a collection (empty if nothing found) may make it
          less likely that calling code will be written incorrectly. If you
          return Nothing and a caller fails to check for Nothing an exception
          will occur.
          >
          If you always return a collection, then depending on how the method
          works, returning an empty collection might mean that it will be
          necessary to create an empty collection that would not have to be
          created if Nothing was returned.
          >
          So there might be a trade-off between safer code and creating more
          objects.
          >
          If it were me I would probably return Nothing, and define the method
          as returning either Nothing or a non-empty collection so that callers
          don't have to make two tests to check if anything was returned.
          How often do you check to see if a collection is empty? Really... In most
          cases, your going to loop over it and do stuff. You don't have to check for
          an empty collection to loop, but you do have to check for a Nothing. Nope,
          returning the empty collection is usually the right thing.

          In general, returning the empty collection is going to make the calling code,
          simpler and more maintainable.

          --
          Tom Shelton

          Comment

          • Tom Shelton

            #6
            Re: method that returns either MatchCollection or nothing?

            On 2008-10-16, Andy B <a_borka@sbcglo bal.netwrote:
            So I shouldn't be doing something like:
            >
            dim matches as MatchCollection = ReturnMatches(s tuff)
            >
            if matches is nothing
            return nothing
            else
            return matches
            >
            >
            Instead I should:
            >
            return (matches as MatchCollection = ReturnMatches(s tuff))
            ReturnMatches(s tuff) should be returning a MatchCollection - even if there are
            no matches.

            --
            Tom Shelton

            Comment

            • Andy B

              #7
              Re: method that returns either MatchCollection or nothing?

              How do you define it to return MatchCollection or nothing?


              "Jack Jackson" <jjackson@cinno vations.netwrot e in message
              news:mokdf417uf d84vb08hbdd018q bvf18gob1@4ax.c om...
              On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_borka@sbcglo bal.net>
              wrote:
              >
              >>Is it safe to make a method that returns a match collection or nothing? or
              >>is it better to just return a match collection and have the code outside
              >>the
              >>method validate that match collection is empty or not?
              >
              Always returning a collection (empty if nothing found) may make it
              less likely that calling code will be written incorrectly. If you
              return Nothing and a caller fails to check for Nothing an exception
              will occur.
              >
              If you always return a collection, then depending on how the method
              works, returning an empty collection might mean that it will be
              necessary to create an empty collection that would not have to be
              created if Nothing was returned.
              >
              So there might be a trade-off between safer code and creating more
              objects.
              >
              If it were me I would probably return Nothing, and define the method
              as returning either Nothing or a non-empty collection so that callers
              don't have to make two tests to check if anything was returned.

              Comment

              • Jack Jackson

                #8
                Re: method that returns either MatchCollection or nothing?

                I meant that you document the method as operating that way.

                However, Tom has a good point and I think always returning a
                collection is probably better.

                On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_borka@sbcglo bal.net>
                wrote:
                >How do you define it to return MatchCollection or nothing?
                >
                >
                >"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                >news:mokdf417u fd84vb08hbdd018 qbvf18gob1@4ax. com...
                >On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_borka@sbcglo bal.net>
                >wrote:
                >>
                >>>Is it safe to make a method that returns a match collection or nothing? or
                >>>is it better to just return a match collection and have the code outside
                >>>the
                >>>method validate that match collection is empty or not?
                >>
                >Always returning a collection (empty if nothing found) may make it
                >less likely that calling code will be written incorrectly. If you
                >return Nothing and a caller fails to check for Nothing an exception
                >will occur.
                >>
                >If you always return a collection, then depending on how the method
                >works, returning an empty collection might mean that it will be
                >necessary to create an empty collection that would not have to be
                >created if Nothing was returned.
                >>
                >So there might be a trade-off between safer code and creating more
                >objects.
                >>
                >If it were me I would probably return Nothing, and define the method
                >as returning either Nothing or a non-empty collection so that callers
                >don't have to make two tests to check if anything was returned.
                >

                Comment

                • Andy B

                  #9
                  Re: method that returns either MatchCollection or nothing?

                  How do you return null collections?


                  "Jack Jackson" <jjackson@cinno vations.netwrot e in message
                  news:2pnef4pqev p2tpnug1vvhh65n b4i0dkpct@4ax.c om...
                  >I meant that you document the method as operating that way.
                  >
                  However, Tom has a good point and I think always returning a
                  collection is probably better.
                  >
                  On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_borka@sbcglo bal.net>
                  wrote:
                  >
                  >>How do you define it to return MatchCollection or nothing?
                  >>
                  >>
                  >>"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                  >>news:mokdf417 ufd84vb08hbdd01 8qbvf18gob1@4ax .com...
                  >>On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_borka@sbcglo bal.net>
                  >>wrote:
                  >>>
                  >>>>Is it safe to make a method that returns a match collection or nothing?
                  >>>>or
                  >>>>is it better to just return a match collection and have the code outside
                  >>>>the
                  >>>>method validate that match collection is empty or not?
                  >>>
                  >>Always returning a collection (empty if nothing found) may make it
                  >>less likely that calling code will be written incorrectly. If you
                  >>return Nothing and a caller fails to check for Nothing an exception
                  >>will occur.
                  >>>
                  >>If you always return a collection, then depending on how the method
                  >>works, returning an empty collection might mean that it will be
                  >>necessary to create an empty collection that would not have to be
                  >>created if Nothing was returned.
                  >>>
                  >>So there might be a trade-off between safer code and creating more
                  >>objects.
                  >>>
                  >>If it were me I would probably return Nothing, and define the method
                  >>as returning either Nothing or a non-empty collection so that callers
                  >>don't have to make two tests to check if anything was returned.
                  >>

                  Comment

                  • Jack Jackson

                    #10
                    Re: method that returns either MatchCollection or nothing?

                    I don't think "null collection" has any meaning.

                    You can return Nothing or you can return an empty collection (create a
                    collection but don't add anything to it).


                    On Thu, 16 Oct 2008 18:26:00 -0400, "Andy B" <a_borka@sbcglo bal.net>
                    wrote:
                    >How do you return null collections?
                    >
                    >
                    >"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                    >news:2pnef4pqe vp2tpnug1vvhh65 nb4i0dkpct@4ax. com...
                    >>I meant that you document the method as operating that way.
                    >>
                    >However, Tom has a good point and I think always returning a
                    >collection is probably better.
                    >>
                    >On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_borka@sbcglo bal.net>
                    >wrote:
                    >>
                    >>>How do you define it to return MatchCollection or nothing?
                    >>>
                    >>>
                    >>>"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                    >>>news:mokdf41 7ufd84vb08hbdd0 18qbvf18gob1@4a x.com...
                    >>>On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_borka@sbcglo bal.net>
                    >>>wrote:
                    >>>>
                    >>>>>Is it safe to make a method that returns a match collection or nothing?
                    >>>>>or
                    >>>>>is it better to just return a match collection and have the code outside
                    >>>>>the
                    >>>>>method validate that match collection is empty or not?
                    >>>>
                    >>>Always returning a collection (empty if nothing found) may make it
                    >>>less likely that calling code will be written incorrectly. If you
                    >>>return Nothing and a caller fails to check for Nothing an exception
                    >>>will occur.
                    >>>>
                    >>>If you always return a collection, then depending on how the method
                    >>>works, returning an empty collection might mean that it will be
                    >>>necessary to create an empty collection that would not have to be
                    >>>created if Nothing was returned.
                    >>>>
                    >>>So there might be a trade-off between safer code and creating more
                    >>>objects.
                    >>>>
                    >>>If it were me I would probably return Nothing, and define the method
                    >>>as returning either Nothing or a non-empty collection so that callers
                    >>>don't have to make two tests to check if anything was returned.
                    >>>
                    >

                    Comment

                    • Andy B

                      #11
                      Re: method that returns either MatchCollection or nothing?

                      Ok...


                      "Jack Jackson" <jjackson@cinno vations.netwrot e in message
                      news:pmqff4pm7r pmn4rr4agfvmskq mjqnjb24b@4ax.c om...
                      >I don't think "null collection" has any meaning.
                      >
                      You can return Nothing or you can return an empty collection (create a
                      collection but don't add anything to it).
                      >
                      >
                      On Thu, 16 Oct 2008 18:26:00 -0400, "Andy B" <a_borka@sbcglo bal.net>
                      wrote:
                      >
                      >>How do you return null collections?
                      >>
                      >>
                      >>"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                      >>news:2pnef4pq evp2tpnug1vvhh6 5nb4i0dkpct@4ax .com...
                      >>>I meant that you document the method as operating that way.
                      >>>
                      >>However, Tom has a good point and I think always returning a
                      >>collection is probably better.
                      >>>
                      >>On Thu, 16 Oct 2008 06:38:56 -0400, "Andy B" <a_borka@sbcglo bal.net>
                      >>wrote:
                      >>>
                      >>>>How do you define it to return MatchCollection or nothing?
                      >>>>
                      >>>>
                      >>>>"Jack Jackson" <jjackson@cinno vations.netwrot e in message
                      >>>>news:mokdf4 17ufd84vb08hbdd 018qbvf18gob1@4 ax.com...
                      >>>>On Wed, 15 Oct 2008 14:11:17 -0400, "Andy B" <a_borka@sbcglo bal.net>
                      >>>>wrote:
                      >>>>>
                      >>>>>>Is it safe to make a method that returns a match collection or
                      >>>>>>nothing ?
                      >>>>>>or
                      >>>>>>is it better to just return a match collection and have the code
                      >>>>>>outside
                      >>>>>>the
                      >>>>>>method validate that match collection is empty or not?
                      >>>>>
                      >>>>Always returning a collection (empty if nothing found) may make it
                      >>>>less likely that calling code will be written incorrectly. If you
                      >>>>return Nothing and a caller fails to check for Nothing an exception
                      >>>>will occur.
                      >>>>>
                      >>>>If you always return a collection, then depending on how the method
                      >>>>works, returning an empty collection might mean that it will be
                      >>>>necessary to create an empty collection that would not have to be
                      >>>>created if Nothing was returned.
                      >>>>>
                      >>>>So there might be a trade-off between safer code and creating more
                      >>>>objects.
                      >>>>>
                      >>>>If it were me I would probably return Nothing, and define the method
                      >>>>as returning either Nothing or a non-empty collection so that callers
                      >>>>don't have to make two tests to check if anything was returned.
                      >>>>
                      >>

                      Comment

                      Working...