Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is Method Overloading a good or bad thing?
Message
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00919869
Message ID:
00921151
Views:
17
>Bully for Bertrand Meyer! I firmly believe that method overloading is the single biggest disservice ever done to the programming
>community by the developers of VS.Net (and yes, I realize they didn't come up with it on their own). Why? For one reason:
>CLARITY. I can easily picture a careless developer proudly coming up with a single monstrous class with heavily overloaded
>methods to do everything in an application!

Yes, but then that same developer likely will create a POC (Pile o' Cr*p) system with or without overloads, won't he/she? IMO (just for contrast), I see little difference in code clarity between the following two (simple, yes) examples. Yet, there is a nice difference during development, in that the overloads provide an easy way for the (good) developer to see which parameters are required and which are optional (via IntelliSense, when coding a call to the overloaded method).

Without overloads
PROCEDURE CallMe( tcPhone, tcName )
* NOTE: tcPhone is required, tcName is optional
IF PCOUNT() < 2
  tcName = ""
ENDIF

MESSAGEBOX( "Calling " + IIF( EMPTY( m.tcName ), "", m.tcName + " at " ) + m.tcPhone )
RETURN DialPhoneNumber( m.tcPhone)
With overloads (if VFP allowed them):
PROCEDURE CallMe( tcPhone )
* NOTE: This overload accepts the phone # only and calls the full-signature overload with a default value for name.
RETURN CallMe( m.tcPhone, "" )

PROCEDURE CallMe( tcPhone, tcName )
* NOTE: This overload accepts both phone # and name -- it is the full-signature overload.
MESSAGEBOX( "Calling " + IIF( EMPTY( m.tcName ), "", m.tcName + " at " ) + m.tcPhone )
RETURN DialPhoneNumber( m.tcPhone)
Clearly, best practices need to be applied, as in the use of any other programming language feature. OTTOMH, in this case I'd say (1) place overloaded methods together in source code, (2) indicate in comments that the method is an overload (3) put only overload-specific code in each overload and (4) delegate the main logic to the full-signature overload (my term, AFAIK). If any overload contains more than a few lines of overload-specific code, it would appear to me to be a candidate for a separate method (i.e., not an overload of some other method).

My $0.02.

Kelly
Previous
Reply
Map
View

Click here to load this message in the networking platform