473,729 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are optional parameters really an advisable thing to use ?


Hi guys,

In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.

I'd like to hear your various opinions on this matter.

Thanks,

-P

Sep 28 '06 #1
12 2669

<pa***********@ libero.itwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>
Hi guys,

In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.

I'd like to hear your various opinions on this matter.

Thanks,

-P
Personally I've never used them. I usually implement methods with varying
numbers of parameters (one for each), that call into a "generalize d" method
that takes all of the parameters, using suitable defaults. I find this is
easier to extend. It's more code to write, but less potential for
hazards/confusion I think Still, they are particularly useful when dealing
with Office Interop and COM I have to say.

Public Sub Foo ( ByVal Variable As Integer )

Foo ( Variable, -1, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer )

Foo ( Variable1, Variable2, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer,
ByVal Variable3 As Integer )

.......

End Sub
Sep 28 '06 #2
<pa***********@ libero.itschrie b:
In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.
I wonder how you come to the perception that optional parameters are
dangerous.

Personally I use optional parameters whereever it makes sense. On the other
hand I use overloading when it makes more sense than optional parameters.
Optional parameters' default values must not be changed once a library has
been published (well, method signatures must not be changed too), they can
be seen as part of the interface.

Thus optional parameters should only be used if the optional parameter's
default value won't change in future and if the default value is not an
implementation detail. If the latter is the case, using overloads is the
better solution. If the first is the case, use a normal parameter, for
example.

Note that C# does not support optional parameters. Methods having optional
parameters can be defined in C# but they cannot be consumed the way this is
done in VB. Always all parameters must be specified in the method calls.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 28 '06 #3
In the past I have used several time optional parameters in my
function.
But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.
I'd like to hear your various opinions on this matter.
1. Fxcop says to avoid them with the rule "Default parameters should not be
used." Optional parameters, it is said, can lead to interoperabilit y
problems even within .net (VB supports optional parameters and C# does not).
The preferred alternative is overloading. So, what is this about? Does VB
overreach or is C# deficient? Is this a problem in .net implementation or do
optional parameters pose theoretically unsolvable problems? Interesting
questions in general, but not relevant to me, because I use VB only. In the
VB only world, I don't think optional parameters pose any known technical
problems.

2. As to the question of "more dangerous than useful", ie the utility of
optional parameters.... Well, I like them, and I use them often. I also use
overloads. I estimate that my code uses optional parameters versus overloads
in proportions of about 80-20. As you would expect, I use optional
parameters when the calling sequences are very similar, and I use overloads
when the calling sequences are very different. I even have a case of two
overloads, and each has optional parameters. I usually code a default value
of an optional parameter is Nothing (or for value types, "", 0, or similar)
which indicates something like 'not applicable' or 'do nothing'. In these
newsgroups, I recently read an example where an optional parameter was
SalesTax and the default value was 6 (indicating 6%) - I never use optional
parameters in this way. I do have some cases of a nonzero numerical optional
parameters, for example a string length parameter defaulted to the worst
case, or a multiplicative scaling factor defaulted to one (meaning multiply
by one, hence no scaling), but these nonzero parameters are quite different
from the sales tax example. I also have a few classes that implement one
constructor having all parameters optional - I have found this to be
convenient but not necessary. In short, I am in favor of judicious use of
optional parameters.

Sep 28 '06 #4
"AMercer" <AM*****@discus sions.microsoft .comschrieb:
> In the past I have used several time optional parameters in my
function.
But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.
I'd like to hear your various opinions on this matter.

1. Fxcop says to avoid them with the rule "Default parameters should not
be
used." Optional parameters, it is said, can lead to interoperabilit y
problems even within .net (VB supports optional parameters and C# does
not).
The preferred alternative is overloading.
The problem with overloading is that it cannot always be used to archieve
the same behavior possible with optional parameters and that overloading has
different characteristics . Overloading hides the value of the parameter
passed to the method, optional parameters don't. If the value is actually
an implementation detail, overloading is the way to go, otherwise using
overloading is nonsense.

Additionally I wonder if FxCop also barked on C# code which contained
operator overloading before VS 2005 came out. There are no valid arguments
against supporting optional parameters but for some reason the C# team
refuses to support them although it would strengthen the link between C# and
..NET.
In these newsgroups, I recently read an example where an optional
parameter was
SalesTax and the default value was 6 (indicating 6%) - I never use
optional
parameters in this way.
That's a really bad sample because of the function is exposed by a library
functionaliy will break if the default percentage changes. Taxes are very
likely to change over time. Better solution:

\\\
Public Class Bla
Public ReadOnly DefaultSalesTax As Double = 6

Public Sub Foo(ByVal SalesTax As Double)
...
End Sub

' Performs 'Foo' operation using the default sales tax.
Public Sub Foo()
Foo(Me.DefaultS alesTax)
End Sub
End Class
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 28 '06 #5

in response to the 6% sales tax ... default optional parameter ... wow...

everybody has their reasons for using optional parameters ... or function
overloading.

I can not agree or disagree with either ... use what you like and what you
are comfortable with ...

I like to use overloading ... for the following reasons...

- many functions have 'regional' / 'divisional' / 'office' defaults ... that
are stored in the database - sales tax being one ;-)
- One function name can have many different parameter lists and parameter
sequences depending on where it is being called from ... I find overloading
gives me the flexibility to easily identify when a parameter is not
included, and lookup its value in the database ... and have the default
value reflect the current user's selected office, division, region and so on
.... and allow the user to easily switch between offices without having to
reset a whole bunch of global / default values...

- i think people are misguided by the statement ... more dangerous than
useful ... come from the following situation .. I have encountered it before
.... u write a function ... another programmer uses it, who does not
understand optional parameters ... thinks he needs to fill in all the
parameters with what he thinks are the default values ... problem ... done.
Also, the dangerous could be encountered when you have 5 optional parameters
and you want to populate the fifth one ... but forget to include the right
number of comma's and parameter 5 is actually passed into parameter 4 - same
datatype.

Again...it is your choice ... and both ways are right and both ways are
wrong - it is personal choice.

Jeff.

PS: VB.Net support for optional parameters ... is this due to the fact that
traditional VB did not support 'overloading' and many VB programs used
'optional parameters' in an attempt to support OOP principles? So, is VB
deficient and C# overreach?



"AMercer" <AM*****@discus sions.microsoft .comwrote in message
news:29******** *************** ***********@mic rosoft.com...
> In the past I have used several time optional parameters in my
function.
But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.
I'd like to hear your various opinions on this matter.

1. Fxcop says to avoid them with the rule "Default parameters should not
be
used." Optional parameters, it is said, can lead to interoperabilit y
problems even within .net (VB supports optional parameters and C# does
not).
The preferred alternative is overloading. So, what is this about? Does
VB
overreach or is C# deficient? Is this a problem in .net implementation or
do
optional parameters pose theoretically unsolvable problems? Interesting
questions in general, but not relevant to me, because I use VB only. In
the
VB only world, I don't think optional parameters pose any known technical
problems.

2. As to the question of "more dangerous than useful", ie the utility of
optional parameters.... Well, I like them, and I use them often. I also
use
overloads. I estimate that my code uses optional parameters versus
overloads
in proportions of about 80-20. As you would expect, I use optional
parameters when the calling sequences are very similar, and I use
overloads
when the calling sequences are very different. I even have a case of two
overloads, and each has optional parameters. I usually code a default
value
of an optional parameter is Nothing (or for value types, "", 0, or
similar)
which indicates something like 'not applicable' or 'do nothing'. In these
newsgroups, I recently read an example where an optional parameter was
SalesTax and the default value was 6 (indicating 6%) - I never use
optional
parameters in this way. I do have some cases of a nonzero numerical
optional
parameters, for example a string length parameter defaulted to the worst
case, or a multiplicative scaling factor defaulted to one (meaning
multiply
by one, hence no scaling), but these nonzero parameters are quite
different
from the sales tax example. I also have a few classes that implement one
constructor having all parameters optional - I have found this to be
convenient but not necessary. In short, I am in favor of judicious use of
optional parameters.

Sep 28 '06 #6
Also (correct me if I'm wrong) VB is the only .Net language that
supports optional parameters, the other languages use overloading. Like
Robinson said, Overloading methods provides the same functionality as
optional params but is safer, easier to maintain, and is compatible
with the other .Net languages.
I usually implement methods with varying numbers of parameters (one for each), that call into a "generalize d" method that takes all of the parameters, using suitable defaults.
Why not just overload the "generalize d" method? - Just curious

Thanks,

Seth Rowe

Robinson wrote:
<pa***********@ libero.itwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .

Hi guys,

In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.

I'd like to hear your various opinions on this matter.

Thanks,

-P

Personally I've never used them. I usually implement methods with varying
numbers of parameters (one for each), that call into a "generalize d" method
that takes all of the parameters, using suitable defaults. I find this is
easier to extend. It's more code to write, but less potential for
hazards/confusion I think Still, they are particularly useful when dealing
with Office Interop and COM I have to say.

Public Sub Foo ( ByVal Variable As Integer )

Foo ( Variable, -1, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer )

Foo ( Variable1, Variable2, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer,
ByVal Variable3 As Integer )

.......

End Sub
Sep 28 '06 #7
IronPython (the newest .NET language) allows optional parameters.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
"rowe_newsgroup s" wrote:
Also (correct me if I'm wrong) VB is the only .Net language that
supports optional parameters, the other languages use overloading. Like
Robinson said, Overloading methods provides the same functionality as
optional params but is safer, easier to maintain, and is compatible
with the other .Net languages.
I usually implement methods with varying numbers of parameters (one for each), that call into a "generalize d" method that takes all of the parameters, using suitable defaults.

Why not just overload the "generalize d" method? - Just curious

Thanks,

Seth Rowe

Robinson wrote:
<pa***********@ libero.itwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>
Hi guys,
>
In the past I have used several time optional parameters in my
function.
>
But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.
>
I'd like to hear your various opinions on this matter.
>
Thanks,
>
-P
>
Personally I've never used them. I usually implement methods with varying
numbers of parameters (one for each), that call into a "generalize d" method
that takes all of the parameters, using suitable defaults. I find this is
easier to extend. It's more code to write, but less potential for
hazards/confusion I think Still, they are particularly useful when dealing
with Office Interop and COM I have to say.

Public Sub Foo ( ByVal Variable As Integer )

Foo ( Variable, -1, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer )

Foo ( Variable1, Variable2, -1 )

End Sub

Public Sub Foo ( ByVal Variable1 As Integer, ByVal Variable2 As Integer,
ByVal Variable3 As Integer )

.......

End Sub

Sep 28 '06 #8
Why not just overload the "generalize d" method? - Just curious
Yes, that is what I'm doing. Overloading provides variations in
functionality, which you could equate with variations in "default"
parameters in this instance. Really I suppose a lot depends on how many
parameters your method has. If it has lots then overloading for each
variation is going to be more difficult to maintain and a lot more code.
Once again, it doesn't pay to be religious about it, just pick and mix ;).
Sep 28 '06 #9

pa***********@l ibero.it wrote:
Hi guys,

In the past I have used several time optional parameters in my
function.

But Now I am more inclined to think that they are more dangerous than
useful, and probably better to be avoided.

I'd like to hear your various opinions on this matter.

Thanks,

-P
Since were talking opinions, I guess I'll give my 2 pennies worth...

I would not classify optional parameters as dangerous, though I believe
they should be avoided when used in publicly exposed interfaces.
Mainly, because as Herfried indicated, their default values become part
of the interface. This can lead to maintainence issues if a default
value changes after the class is published. Further, it can lead to
interoperabilit y problems if you plan to use the code from other .NET
languages (even if you don't plan on it now, I think it is always a
good idea to take this into consideration anyway).

So, my rule is basically - if it is part of a publicly exposed
interface, use overloading exclusively. If the class is part of the
internal implementation, then I don't really think it makes much
difference - though, I would tend to overloading simply for
consistancy.

--
Tom Shelton

Sep 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2514
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. I've done this before with Modules, and saw that <gasp> they behave just like sealed classes with only static members. Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in...
16
4060
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
12
2296
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without a good reason.
14
3270
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.