Monthly Archives: November 2003

Default arguments are bad? I don’t buy it.

Although I greatly respect the opinons of my fellow Microsoft employees (and most other people, for that matter), there are times when we have honest differences of opinions. A recent blog entry by Brad on default parameters is a good example.

I understand the versioning argument that he and the C# team make about default arguments, but I just don’t buy it. I think that not having (or not ever using) default arguments because of versioning is throwing out the baby with the bathwater. Brad gives an example of a VB function with a default argument:

Function ComputeTotal(ByVal subTotal As Double, Optional ByVal salesTax As Double = 8.8) As Double
Return subTotal + subTotal * salesTax / 100
End Function

He suggests that the following would be better:

Function ComputeTotal(ByVal subTotal As Double, ByVal salesTax As Double) As Double
Return subTotal + subTotal * salesTax / 100
End Function

Function ComputeTotal(ByVal subTotal As Double) As Double
Return ComputeTotal(subTotal, 8.8)
End Function

He feels the latter example is better because you can change the default value of salesTax and automatically have all clients compiled against the method pick up the change. Which is true. But I have to ask: what class library designer in their right mind would change the default value in the first place?

One of the major areas of concern for class library designers inside of Microsoft is compatibility, and one of the cardinal rules of compatibility is that you don’t make this kind of behavioral change. Why not? Because you’re likely to break people who depend, rightly or wrongly, on the default behavior that you introduced. (If you have any doubts about this, go talk to Raymond.)

In the given example, yes, the state may have changed its sales tax and so the calculation function may need to change. But just changing the default may cause serious issues. What if your application needs to calculate sales tax for sales that occured before the sales tax rate changed? Yes, you should have explicitly stated the sales tax you want to use, but it’s just as likely you used the default because: a) it’s easier and b) at the time you wrote your code, you got the expected result. As a result, when the default changes you’ll end up willy-nilly calculating the sales tax with the wrong rate! I would argue that in this situation the problem has less to do with default arguments or overloading than it does with bad API design.

My point is that whether you use default arguments or overloading, it’s extremely unlikely that you’re ever going to be able to change default values once you’ve published an API. That is, unless the API is completely internal to your application. In which case, the versioning “problem” with default arguments doesn’t apply! So in this case, I think the prohibition against default arguments is worrying about what happens in a situation that shouldn’t occur in the first place.

I will hasten to add that this doesn’t mean that I don’t think that overloading isn’t a good thing or that default arguments can be abused. (Anyone with any doubt on that last point only has to look no further than the Office object model.) In practice, I think that proper API design trends in favor of overloading simply because it encourages simpler API calls and reduces the need for things like named arguments. But there are definitely places where default arguments can be used to avoid adding superfluous overloads with very little overhead.

It’s good to know we are not alone…

Stan Lippman, a C++ guru at Microsoft has started blogging. His inaugural entry discusses one of the issues that their language design team faces in moving C++ to the CLR. Near the end, in a section entitled “The C++.NET Design Challenge,“ he says:

Literally, for every aspect of the C++ extensions to support .NET the question always reduces to “How do we integrate this (or that) aspect of the Common Language Runtime (CLR) into C++ so that it (a) feels natural to the C++ programmer, and (b) is easy to use in its own right under .NET. I like to call this the Janus face dilemma. (Janus is a two-faced Roman diety, the one turned facing towards what has just been, the other towards what is to be.)

As my entry title says, it’s good to know that we (the VB team) are not alone. Moving VB to the CLR was a major undertaking involving many, many, many dilemmas along these lines. Even though C++ and VB are two very different languages, I have a lot of empathy for what the C++ team is going through as they struggle with these questions. It’s not an easy path to trod.

Publishing lag

One comment I’ve gotten several times over from family members this Thanksgiving is “I was at the bookstore and I looked for your book, but it wasn’t there.” Apparently, in all my grousing about getting the manuscript in, I gave the impression that as soon as I turned in my manuscript, the book would go out to the shelves. I’ve still got all the grunt work of reviewing copy edits and page proofs to go before I get something real in my hand.

The book’s going to be part of the Microsoft .NET Development Series published by Addison Wesley that John Montgomery’s been talking about. The series includes books by Don Box and Chris Sells, which is a bit scary company to be keeping. On the other hand, I made a small contribution to one of the books in the series already, and they haven’t laughed me out of the club yet…

Gobble gobble gobble

It should come as no suprise to anyone, but entries this week are going to be slow given that I am visiting family for Thanksgiving. The feed, at least, is fixed thanks to a prod by Darrell Norton. I’ve had a lot of problems with FreeTextBox not translating my particular style of writing into valid XHTML, so my feed keeps getting broken by me incorrectly fixing up the HTML. Anyway, when I get back and get some time, I’ll look into solutions, but for now…

Another interesting use of When

Ok, I think I may have done the “When” thing to death, but Sam Spencer, a PM who used to work on the compiler, pointed out another interesting use of it. Often times, applications will have a global exception handler in the Main method that catches any exceptions that weren’t handled elsewhere. Something like:

Sub Main()
Try
RunApplication()
Catch ex As Exception
HandleGlobalException(ex)
End Try
End Sub

The compiler itself uses a strategy similar to this for out of memory situations — once we’ve run out of memory, the compiler’s toast, so we throw an exception indicating “out of memory” and then catch it at the top level, giving a nice “out of memory, please restart the compiler” error. (Hopefully none of you have ever seen it.)

Anyway, when you’re debugging the application it can be convenient to not catch exceptions at the top level so that you immediately break into the debugger when you get a global exception. A simple way to do this is to use When to not catch the exception when a debugger is present:

Sub Main()
Try
RunApplication()
Catch ex As Exception When Not System.Diagnostics.Debugger.IsAttached
HandleGlobalException(ex)
End Try
End Sub

Kind of cute.