473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overloading vs Optional Parameters... why?

This is more of a general question, but I didn't know where to post it. Since Java is an example of a language which does this, I thought here would work.

Coming from a C++ background, having used VBA/VB6 quite a bit, and a number of scripting languages with similar traits, like php, I had grown accustomed to optional parameters in my functions.

I recently found out that certain modern languages do not support these, namely Java and Visual C# .NET.

This threw me at first, and then I noticed that everybody was banging on about using function overloading to provide multiple combinations of possible parameter inputs.

Now, this to me seems like incredible code repetition/redundancy, and makes things far messier (substitute longer/more complex/less elegant as you see fit) than neceesary.

I've seem a few comments about how people find the definition of optional parameters confusing. I don't know if its just because I've learned to program based on a language which supports them that I do not find this to be the case.

I've also seen some comments that it is to enforce the use of overloading for the purpose of optional parameters.

My question is quite simple: why? What is the rationale behind this method over the optional parameters?

I wondered if there was some technical gain from this approach. Or perhaps it eliminates some major source of common errors, perhaps caused by ambiguities relating to optional parameters. Or maybe it is felt that nowadays you should not have functions with a lot of parameters in anyway, so that making them optional won't involve much repetition.

I haven't been able to find a straight answer to this. I cannot believe that it is to make things easier to understand, as given the choice, I would take the C++ optional parameters over having loads of variations of a single function.

If anybody has a common sense answer to this, or insight into the decisions behind modern language definition, please do let me know.

Regards,
Rob.
Oct 10 '07 #1
1 8389
r035198x
13,262 8TB
This is more of a general question, but I didn't know where to post it. Since Java is an example of a language which does this, I thought here would work.

Coming from a C++ background, having used VBA/VB6 quite a bit, and a number of scripting languages with similar traits, like php, I had grown accustomed to optional parameters in my functions.

I recently found out that certain modern languages do not support these, namely Java and Visual C# .NET.

This threw me at first, and then I noticed that everybody was banging on about using function overloading to provide multiple combinations of possible parameter inputs.

Now, this to me seems like incredible code repetition/redundancy, and makes things far messier (substitute longer/more complex/less elegant as you see fit) than neceesary.

I've seem a few comments about how people find the definition of optional parameters confusing. I don't know if its just because I've learned to program based on a language which supports them that I do not find this to be the case.

I've also seen some comments that it is to enforce the use of overloading for the purpose of optional parameters.

My question is quite simple: why? What is the rationale behind this method over the optional parameters?

I wondered if there was some technical gain from this approach. Or perhaps it eliminates some major source of common errors, perhaps caused by ambiguities relating to optional parameters. Or maybe it is felt that nowadays you should not have functions with a lot of parameters in anyway, so that making them optional won't involve much repetition.

I haven't been able to find a straight answer to this. I cannot believe that it is to make things easier to understand, as given the choice, I would take the C++ optional parameters over having loads of variations of a single function.

If anybody has a common sense answer to this, or insight into the decisions behind modern language definition, please do let me know.

Regards,
Rob.
You want to read about varargs in Java
Oct 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: John Smith | last post by:
can we overload a javascript function with different argument? example: function a(a){} function a(a,b){}
5
by: (Pete Cresswell) | last post by:
I've dabbled in "Optional" over the last few days and think I'm coming down against using it. Seems to me like it makes the code harder to read and more complicated. Instead of using Optional,...
4
by: Brian | last post by:
Does anyone have a good way to handle the following situation... public void FilterResults(int a, int b) I need to filter based on none, one, or both parameters, so they both need to be...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
4
by: Doug | last post by:
Hi I am 'returning' to the learning of C# after a change in jobs and I remember that I used to struggle with the defintion, purpose and function of 'overloading'. When I was beginning with C# I...
7
by: Sam Shrefler | last post by:
I'm working on creating a WebService / WebMethod to receive a record in real time from another system. The record contains about 20 fields. 10 of which aren't required. I was planning on just...
2
by: rn5a | last post by:
I use VB.NET to create ASP.NET apps. If I am not wrong, there is something called method overloading in VB.NET (like in C#) which is one of the features in OOP (polymorphism) but does VB.NET also...
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.