Re: Why default parameter values where not included in Java

From: Roland (roland_at_phony.biz)
Date: 02/04/05


Date: Fri, 04 Feb 2005 20:21:48 +0100

On 4-2-2005 16:00, Adam Maass wrote:

> "axer" <temp@axter.com> wrote:
>
>>
>>//The C/C++ way using defautl parameters
>>int ShowMessage(string MainMsg, string Title = "Info", int MessageType
>>= FooWarning, int MessageButtons = IDOK)
>>{
>> //Do message
>>}
>>
>>The above is much easier to understand, easier to maintain, and easier
>>to find what you're looking for then the following Java overloaded
>>method:
>>
>>int ShowMessage(string MainMsg, string Title, int MessageType, int
>>MessageButtons)
>>{
>> //Do message
>>}
>>
>>int ShowMessage(string MainMsg, string Title, int MessageType)
>>{
>> return ShowMessage(MainMsg, Title, MessageType, IDOK);
>>}
>>
>>int ShowMessage(string MainMsg, string Title)
>>{
>> return ShowMessage(MainMsg, Title, FooWarning, IDOK);
>>}
>>
>>int ShowMessage(string MainMsg)
>>{
>> return ShowMessage(MainMsg, "Info", FooWarning, IDOK);
>>}
>>
>
>
> I don't disagree with you. And the client of the class in question would
> have the same interface using either approach.
>
> My reference to "less syntax" does not equate to "less verbose" or even
> "more clear." I think the default parameter syntax was left out of Java
> because -- from the perspective of a client of the class -- the same effect
> can be achieved with method overloading, and method overloading was going to
> be incorporated into Java anyway for other reasons, and the syntax for
> default parameters in C++ just complicates the parsing rules of the language
> for very little perceived benefit. Again, let me emphasize: from the
> perspective of a client of the class, not an author or maintainer of the
> class.
>
> In addition, the way default parameters are implemented in (most flavors of)
> C++ would be difficult to achieve in Java given its dynamic class loading
> scheme. So even if default parameters had been carried over into Java from
> C++, it is likely that the language spec would have required Java compilers
> to treat any default parameter syntax as a shorthand for method overloading.
>
> -- Adam Maass
>
>
Adam, your reasoning matches strikingly well with the Java design goals
written down by James Gosling and Henry McGilton in the "The Java
Language Environment" whitepaper (1996).

A quote from Chapter 2 "Java -- Simple and Familiar", section 2
[emphasis added]:
<quote>
The first step was to eliminate redundancy from C and C++. In many ways,
the C language evolved into a collection of overlapping features,
providing too *many ways to say the same thing*, while in many cases not
providing needed features. C++, in an attempt to add "classes in C",
merely added more redundancy while retaining many of the inherent
problems of C.
</quote>

I could not find any direct reference to default parameter values in the
whitepaper. IOW and as I interpret it: default parameter values have
been not been included in Java, because it can be equally expressed by
other means (method overloading).

The whitepaper can be found at Sun's Java site:
<http://java.sun.com/docs/white/langenv/>.

It's fun to read why the didn't include enums (section 2.2.3). While
other sections have a title starting with "No *more* ...", section 2.2.3
is called "No Enums". Somehow I get the feeling they already consider
enums for a future version (and now, as we all know, enums have made it
in Java 5).

-- 
Regards,
Roland de Ruiter
   ___      ___
  /__/ w_/ /__/
/  \ /_/ /  \

Quantcast