I was speaking with a colleague (names omitted to protect the innocent) earlier about a new project he’s working on; like myself he sticks with C# whenever possible…but we all at some point get stuck maintaining a project that isn’t written in our language of choice…in this case, VB.NET. Like most .Net developers (hopefully), we both know enough of VB.NET syntax/keywords to stumble through a project and pick up stuff as we need to. He brought up an interesting point that (until today) I had never thought about; he thought it a bit strange that as VB.NET supports the Optional keywords for method parameters that the system he’s working on still used overloaded methods (in essence with Optional params you could just define one method signature and declare all params as optional (and with default values), then from calling code just feed whatever parameters your routine needs processed). At first glance, this makes sense…but if you think about it, it lends itself to lazy programming, doesn’t follow correct OOP concepts…but most of all, not all languages targeting the CLR understand the notion of Optional params (namely, C#). It’s also a breaking rule in FXCop.
The only thing I can think of offhand that optional params might come in handy is if you’re dealing with legacy COM code where there are numerous optional params in a method (I’ve seen some methods that take literally dozens of optional params, passing in null to all the optionals looks pretty ugly); so if you’re simply wrapping up some legacy COM code (and you know that your assembly won’t be referenced by another language, which is never guaranteed), I can perhaps see some advantage to this.
In the case of C#, if a VB.NET assembly is referenced with members containing optional params, they will simply be ignored; you must supply a value for the parameter regardless, even if there is a default value specified in the VB.NET member signature. Overloading members provides a more terse, declarative way to describe exactly which member to use in whatever scenario…if the assembly follows good design rules and exposes overloads correctly, this is a huge boon to the developer using the component. Optional parameters is one of those programming constructs that seems beneficial at first glance (seemingly cutting down on code typing…leading to less unit tests, less documentation, etc), but in the OO world they are no substitue for properly overloaded type members.



No comments yet