.NET

Reply
*Mark Johnston
Message 1 of 9 (104 Views)

Best practice for writing overloaded functions

104 Views, 8 Replies
07-12-2005 07:24 AM
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?
*Tony Tanzillo
Message 2 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 11:16 AM in reply to: *Mark Johnston
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?
*Mark Johnston
Message 3 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 11:42 AM in reply to: *Mark Johnston
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?
*Danny P.
Message 4 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 01:52 PM in reply to: *Mark Johnston
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.
>
*Bobby C. Jones
Message 5 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 02:10 PM in reply to: *Mark Johnston
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.
>
*Danny P.
Message 6 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 03:33 PM in reply to: *Mark Johnston
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.
*Tony Tanzillo
Message 7 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 04:23 PM in reply to: *Mark Johnston
"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
*Tony Tanzillo
Message 8 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 04:43 PM in reply to: *Mark Johnston
"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
*Danny P.
Message 9 of 9 (104 Views)

Re: Best practice for writing overloaded functions

07-12-2005 07:53 PM in reply to: *Mark Johnston
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.
>

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community