The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Returning Multiple Values

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Keith Ray

Posts: 658
Nickname: keithray
Registered: May, 2003

Keith Ray is multi-platform software developer and Team Leader
Returning Multiple Values Posted: Jan 7, 2004 5:44 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Keith Ray.
Original Post: Returning Multiple Values
Feed Title: MemoRanda
Feed URL: http://homepage.mac.com/1/homepage404ErrorPage.html
Feed Description: Keith Ray's notes to be remembered on agile software development, project management, oo programming, and other topics.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Keith Ray
Latest Posts From MemoRanda

Advertisement

One of the neato things about Python is returning a small number of multiple values from a method is easy. For example, finding the elements that are shared by two collections/lists [hope I got the syntax right]:


   beforeNotAfter, afterNotBefore = foo.findNonOverlappedElements( before, after )
   # do stuff using beforeNotAfter
   # do stuff using afterNotBefore

Alan Knight says the Smalltalk "moral equivalent" to this uses a block (my paraphrasing here):


    foo findNonOverlappedElementsOf: before and: after 
        doing: [ : beforeNotAfter : afterNotBefore
            "do stuff to beforeNotAfter".
            "do stuff to afterNotBefore" ].

Coming from a background of C/Object-Pascal/Objective-C/Java/C++, it's hard for me to think of reasons to do a Python-style multiple-return; instead of one method that returned two values, I'd write two methods, or single method that returns a data-structure or has output-parameters. And, unfortunately, Objective C, Java, etc., don't make "blocks" easy. Though there is a dialect of Objective-C that has blocks, it's not the dialect supported by gcc and Apple.

The other aspect of the Smalltalk solution here is "tell, don't ask". We tell 'foo' to find the non-overlapped elements, and we tell foo what to do with the resulting lists of elements (execute our block). This forces a certain cohesion to our code. The Python example is violating the spirit of the Law of Demeter, if not the actual Law (1) because we ask and then work the results of what we asked.

footnote 1: Law of Demeter (LoD) is really just a guideline that is particularly questionable when collections are involved.

Read: Returning Multiple Values

Topic: ET and Religion? Previous Topic   Next Topic Topic: What the customer wants

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use