.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Best practice for writing overloaded functions

8 REPLIES 8
Reply
Message 1 of 9
Anonymous
583 Views, 8 Replies

Best practice for writing overloaded functions

Here is a programmers question regarding writing overloaded functions.
In many cases this means there will be some similar code in each version of
the function.

Is there a preferable approach?
A. Write each from scratch.
B. Write the most complicated first. Then copy and edit.
C. Write the simplest first. Then copy and edit.

Why?
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

It depends on what the overloaded functions are doing.

In many cases, I overload only as a means of providing
optional arguments, like this:

public void Foo(int x)
{
// ..
}

public void Foo()
{
Foo(0); // default value when argument is not supplied
}

That's one scenario (and the most common). There are others.
In cases where there is a lot of replication between different
versions of the same overloaded function, I often write one 'main'
overload that does most of the work, and have the others call it
internally.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Mark Johnston" wrote in message news:4898926@discussion.autodesk.com...
Here is a programmers question regarding writing overloaded functions.
In many cases this means there will be some similar code in each version of
the function.

Is there a preferable approach?
A. Write each from scratch.
B. Write the most complicated first. Then copy and edit.
C. Write the simplest first. Then copy and edit.

Why?
Message 3 of 9
Anonymous
in reply to: Anonymous

Thanks Tony. That makes sense. The optional arguments scenario is exactly
what I'm doing. Calling one main overload should do nicely.

"Tony Tanzillo" wrote in message
news:4899263@discussion.autodesk.com...
It depends on what the overloaded functions are doing.

In many cases, I overload only as a means of providing
optional arguments, like this:

public void Foo(int x)
{
// ..
}

public void Foo()
{
Foo(0); // default value when argument is not supplied
}

That's one scenario (and the most common). There are others.
In cases where there is a lot of replication between different
versions of the same overloaded function, I often write one 'main'
overload that does most of the work, and have the others call it
internally.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com

"Mark Johnston" wrote in message
news:4898926@discussion.autodesk.com...
Here is a programmers question regarding writing overloaded functions.
In many cases this means there will be some similar code in each version of
the function.

Is there a preferable approach?
A. Write each from scratch.
B. Write the most complicated first. Then copy and edit.
C. Write the simplest first. Then copy and edit.

Why?
Message 4 of 9
Anonymous
in reply to: Anonymous

I have very little knowledge of overloaded functions, but it seems to me
that your situation could be handled with an Optional parameter instead
of an overloaded function (not true?):

Sub Foo (Optional x as Int32 = "0")

I thought Overloaded functions were meant to handle situations (in
addition to optional parameters) where parameters were of a different type:

Sub FreezeLayer (LayerName As String)

Sub FreezeLayer (Layer As AcadLayer)

This prevents the need for using Objects as parameters, then determining
if the Object is of a certain type within the function. Am I on the
right track here? Is the use of Optional parameters not encouraged?

Thanks,
Danny Polkinhorn
WATG
Honolulu


Tony Tanzillo wrote:
> It depends on what the overloaded functions are doing.
>
> In many cases, I overload only as a means of providing
> optional arguments, like this:
>
> public void Foo(int x)
> {
> // ..
> }
>
> public void Foo()
> {
> Foo(0); // default value when argument is not supplied
> }
>
> That's one scenario (and the most common). There are others.
> In cases where there is a lot of replication between different
> versions of the same overloaded function, I often write one 'main'
> overload that does most of the work, and have the others call it
> internally.
>
Message 5 of 9
Anonymous
in reply to: Anonymous

Hey Danny,
Not all languages support optional parameters.
--
Bobby C. Jones
http://www.acadx.com


"Danny P." wrote in message
news:4899508@discussion.autodesk.com...
I have very little knowledge of overloaded functions, but it seems to me
that your situation could be handled with an Optional parameter instead
of an overloaded function (not true?):

Sub Foo (Optional x as Int32 = "0")

I thought Overloaded functions were meant to handle situations (in
addition to optional parameters) where parameters were of a different type:

Sub FreezeLayer (LayerName As String)

Sub FreezeLayer (Layer As AcadLayer)

This prevents the need for using Objects as parameters, then determining
if the Object is of a certain type within the function. Am I on the
right track here? Is the use of Optional parameters not encouraged?

Thanks,
Danny Polkinhorn
WATG
Honolulu


Tony Tanzillo wrote:
> It depends on what the overloaded functions are doing.
>
> In many cases, I overload only as a means of providing
> optional arguments, like this:
>
> public void Foo(int x)
> {
> // ..
> }
>
> public void Foo()
> {
> Foo(0); // default value when argument is not supplied
> }
>
> That's one scenario (and the most common). There are others.
> In cases where there is a lot of replication between different
> versions of the same overloaded function, I often write one 'main'
> overload that does most of the work, and have the others call it
> internally.
>
Message 6 of 9
Anonymous
in reply to: Anonymous

Bobby,

Thanks. That's what I was thinking.

So in VB is there any reason not to use Optional parameters?

Thanks again,
-Danny

Bobby C. Jones wrote:
> Hey Danny,
> Not all languages support optional parameters.
Message 7 of 9
Anonymous
in reply to: Anonymous

"Danny P." wrote

>> I have very little knowledge of overloaded functions, but
>> it seems to me that your situation could be handled with
>> an Optional parameter instead of an overloaded function
>> (not true?):

Provided the language supports them, which is not
the case in C#.

For me that's pretty frustrating since both of my most
often used languages (Delphi and C++) both support
optional args, and that makes for a lot less typing.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com
Message 8 of 9
Anonymous
in reply to: Anonymous

"Danny P." wrote

>> So in VB is there any reason not to use Optional parameters?

There may be, but I've not researched it.

It depends on whether you want to allow your code
to be called from other languages, and therefore be
CLR-compliant. I'm not even sure if optional arguments
breaks CLR compliance (my guess is that it does), so
it might be something you may want to research. If
for example, you want to transition from VB to C# and
keep some of your VB code that you can call from C#,
you may run into a problem with optional arguments.

CLR compliant essentially means that your code can
be called/used from other .NET languages.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com
Message 9 of 9
Anonymous
in reply to: Anonymous

Ok, thanks for the replies.

-Danny

Tony Tanzillo wrote:
> "Danny P." wrote
>
>
>>>So in VB is there any reason not to use Optional parameters?
>
>
> There may be, but I've not researched it.
>
> It depends on whether you want to allow your code
> to be called from other languages, and therefore be
> CLR-compliant. I'm not even sure if optional arguments
> breaks CLR compliance (my guess is that it does), so
> it might be something you may want to research. If
> for example, you want to transition from VB to C# and
> keep some of your VB code that you can call from C#,
> you may run into a problem with optional arguments.
>
> CLR compliant essentially means that your code can
> be called/used from other .NET languages.
>

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost