Re: Keyword Arguments

Steve Dekorte <[email protected]> Thu, 14 Jun 2012 13:46:52 -0700
Newsgroups gmane.comp.lang.io
Message-ID <[email protected]>
On 2012-06-14 Thu, at 10:52 AM, Kevin Edwards wrote:
> On 6/13/2012 3:08 PM, Steve Dekorte wrote:
>> 
>> As objects are about coupling data and logic, if you're passing a lot of arguments(data) around
>> to methods (logic), then it looks suspiciously like you've failed to couple your data and logic.
> 
> It can be suspicious to have a lot of args, but perhaps arity is not the best metric to use since you can just repackage the args, as you seem to be suggesting. 
> Every 2+ argument method call can be rewritten as a series of 1 argument method calls which configures the object/frame.  e.g. :

I agree - a better metric would be to look at how much the data "moves" around. The more it moves, the less it appears to be coupled to it's logic. 

On a side note, this is why OO/actors are the only scalable programming model. Since logic and data are localized, communication is minimized (speed of light issues) and the Von Neumann bottle neck is avoided. 

> Every 2+ argument method call can be rewritten as a series of 1 argument method calls which configures the object/frame.  e.g. :
> 
>   meth(a=1, b=2, c=3)  <->  Meth clone setA(1) setB(2) setC(3) result
> 
> Both versions can have the same massive, monolithic implementation of logic with just a difference in packaging.

Right, the difference is that the method instance is used once and discarded so the sender has to hold the data that was passed and pass it in again on each call. We are left asking if the data shouldn't belong to the receiver instead of being communicated each time.

Here's a small example of how the rule of minimizing arguments leads to power and simplicity:

When dealing with collections, the two argument pattern is often used:

	aDict atPut(key, value)

Changing this to a single argument form:

	aDict at(key) put(value)  

where at() returns a slot object, we find we can now pass it as wrapper around this part of the collection - similar to passing blocks as wrappers around particular contexts. This tends not to be done because it requires better compilers to achieve the same performance but it's AFAICS more expressive.

Steve