142 Views
Default parameters have been around forever both in C++ and in VB. When Java was introduced, however, they were eschewed as being a problematic source of potential code-safety issues. Having been bitten by default parameters before in less strongly-typed languages, I can understand this to an extent. But in many ways this forced us to write heavier code (using overloads) where a default parameter would have been easier to maintain. Java seemed to throw out the baby with the bathwater, just in my opinion, instead of letting the developer decide when using default parameters was the most appropriate choice. C# 1.0, having descended from Java and C++ roots, took the Java approach originally of not allowing default parameters. With the advent of C# 4.0 compiler, however, default parameters are now a part of the C# language. They are a great tool for reducing armies of overloads created to get around the lack of default parameters in early C#. That said, they do have a few things to watch out for so you don’t get bit…
- Is specifying a default value in an interface a sane thing to do? This seems like a code smell, especially if the developer plans on subclassing any classes implementing the interface
- Does it make any sense to supply default values for functions that are designed to be inherited? It seems like it's meant to be a convenience feature and not a design pattern. It makes much more sense to use a protected variable or property to supply the default, then the role of the default value in inheritance is clear.