Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wasatch!cs.utexas.edu!uunet!mfci!hsi!wright
From: wright@hsi.UUCP (Gary Wright)
Newsgroups: comp.lang.c++
Subject: Re: Named arguments?
Message-ID: <554@hsi86.hsi.UUCP>
Date: 16 Aug 89 13:57:49 GMT
References: <612@windy.dsir.govt.nz> <2179@uw-entropy.ms.washington.edu>
Reply-To: wright@hsi.com (Gary Wright)
Organization: Health Systems Intl., New Haven, CT.
Lines: 91

In article <2179@uw-entropy.ms.washington.edu> adrianb@castor.ms.washington.edu writes:

>	If we ever get a C++ interpreter, we could have problems
>	in calling functions with lots of arguments.

	Having procedures or functions with many parameters is probably
	an indication that you need to redesign your classes.
>	
>	Interactive statistical packages (like 'S') use
>	named arguments, e.g.
>
>		z <- scatplot(x, y, scale=3)
>
>	Here `scatplot' could be a function that makes a scatter plot object
>	from the vectors x and y; it would be sensible to have zillions
>	of options, 
>		scatplot(x,y,aspect,scale,xtitle,ytitle,xmargin,ymargin,etc..)
>	the options need sensible defaults of course.
>
>	Is there a better solution??

I have admittedly little experience in object oriented programming
but I have been reading quite a bit lately so I'll take a stab
at a better solution.

It seems to me that the solution is to use the OO paradigm when
designing the scatplot object.  The scatplot object should be
defined with all the necessary attributes.  When the object is created,
the attributes get appropriate default values.   The attributes
are changed by what Booch in _Software_Components_with_Ada_ calls "constructors"
(in C++ terminology, member functions that change the state of an object).

Using an Eiffel-like syntax:

	plot : SCATTER_PLOT;
	
	plot.Create;		
	-- plot object is created with possibly default values for
	-- for attributes.  For example scale would default to 1.
	
	plot.set_points(x, y);
	plot.set_scale(3);
	plot.set_xtitle("The is the title for the X axis");
	
	-- more attributes can be modified here
	
	plot.display;-- And now display the object

While some may say that this is just too verbose.  I would make the same
argument for:
	
	scatplot( xvector => x ,
		  yvector => y,
		  aspect => aspect,
		  scale => 2,
		  xtitle => "The X axis",
		  ytitle => "The Y axis",
		  ...
		  );

If you just wanted to change the scale from the default then we have:

	scatplot( xvector => x, yvector => y, scale => 3);

versus

	plot.Create;
	plot.set_points( x, y);
	plot.set_scale( 3 );

What if you want to change the scale and redisplay the object?

	plot.display;
	plot.set_scale(3);
	plot.display;

The other method does not provide a solution because all the unnamed
arguments will be set to defaults.  In other words if you use the first
method, than you are going to need seperate procedures to set
individual attributes anyway.  

I think the second method is simpler (no need to introduce named
arguments for one).  It also allows the compiler to be smart and to
inline the appropriate functions unlike the more complex constructor
which is not a good candidate for inlining.

Well, now that I have mentioned C++, Eiffel, and Ada in one article, I
should be in for some interesting mail...
-- 
Gary Wright 					...!uunet!hsi!wright
Health Systems International                    wright@hsi.com