Fabulous Adventures In Coding
Eric Lippert is a principal developer on the C# compiler team. Learn more about Eric.
No kidding, I was just walking down a hallway in my building when I overhead the following quite loud conversational fragment through an open doorway:
Angry woman's voice: "Why are you in the ladies room?! You are the third man to... oh no."
Like Hobbes, it's the moment of dawning comprehension that I live for - the exact moment when she realized that she, not everyone else, was in the wrong room was readily apparent. (One wonders what the first two gentlemen did, since clearly they did not successfully disabuse the lady of her error.) Since the building across the courtyard from mine has a mirror-imaged layout, this is a very easy mistake to make if you are visiting from the other building.
I contrast that moment of dawning comprehension with Dr. Crusher's similar moment in that memorable 1990 episode of Star Trek: The Next Generation when she realizes that she's not crazy, it's the entire universe that is wrong. When faced with an absurd and unexpected situation - the gradual disappearance of first the crew and then the entire universe - she at least considers that she's the crazy one.
Unlike most people, I encounter compiler and library bugs all day long in my job, though mostly ones that I caused in the first place. (Sorry!) But even still, when I am writing "normal" code (rather than test cases designed to break the compiler or regress previous bugs), I try to ensure that my attitude upon encountering an unexpected situation is that I'm the crazy one. Usually it's my code that is wrong, or my misunderstanding the output, rather than a compiler or library bug.
As the authors of "The Pragmatic Programmer" point out in their third chapter, "select() isn't broken" - if you are writing perfectly normal code then odds are good you are not the first person to discover what should be an obvious problem in a well-tested product. If you think you've found a bug in the math library, maybe you have. Or maybe you've actually passed radians to a method that takes degrees, or forgotten to take floating point rounding error into account, or some other such thing. The more obvious the problem, the more likely it is that you're the crazy one. If the code doesn't compile and you think it should, it could be a bug in the compiler. But read the error message carefully; it is probably telling you what is wrong with the code.
If you think you've found a C# compiler bug, please, by all means bring it to our attention; post it on Connect, or have the community take a gander at it via StackOverflow or one of the Microsoft forums, and if you want, send me a link to the problem. (Please don't use the "contact" link to send me source code directly; the hostile-email sterilization code that filters that text is very aggressive about stripping out things that look potentially harmful. It makes code almost illegible.) There certainly are bugs in the compiler and the more we get good information on, the better. Including a small-but-complete program that reproduces the problem and the version number of the compiler you're using is a big help. But first, do stop and take a good hard look at the code and think about whether it is more likely to be a problem with the code or a problem with the compiler. Don't be one of those people who sends me angry, profane emails about a problem that you caused yourself; that's just embarrassing.
A good article, but every now and then it turns out you've been blaming your own code, banging your head against a brick wall and select WAS broken!
My recent example: The .NET microframework where double.Parse("-0.1") really does return +0.1. A pain to find when your GPS based project only gets screwy when you're near the Greenwich meridian!
What if I know I'm crazy to begin with? Am I just screwed?
About 15 years ago, I had a guy working for me who wanted to call a company not named Microsoft every week because of a bug in their C compiler. Every time, he either misspelled something or forgot a closing brace in the C code. I was ready to take his phone from him.
I agree that the compiler/library probably handles correct code correctly, and if the program doesn't work there is probably a problem with the code. But if incorrect code produces errors that don't indicate what is wrong with the code, I say the compiler/library also has a problem.
I've found very few real bugs in the C# compiler, what I have run into more often is arbitrary limitations that are actually buried in the specification that conceptually shouldn't be there, which is what Microsoft I guess refers to as a "design feature". So the universe is normally accurate I guess but that doesn't necessarily make it right.
My first problem with the universe was back in the early 90's with a file listing/manipulaiton program I wrote. At some point, it started crashing upon exit when the number of files in a directory was a multiple of 3! After bashing my head looking for the usual memory corruption problems, I swapped a couple irrelevant lines in the source code and the problem went away. I seem to recall that at some point the problem came back and I had to swap the lines again. I never did figure out if this was a codegen bug in the C++ compiler, but it seemed too strange to be a bug in my code.
The problem with a bug like this is that it's nearly impossible to get a minimal reproduction because it's brought about by random perturbations within 100,000 lines of source code.
While I've encountered several bugs in various parts of the .Net environment, I have yet to have the pleasure of finding a bug in the C# compiler.
Kudos on the Trek reference. How 'bout an article on small API changes from version to version and compare it to the episode where Worf is shifting between parallel universes every time he comes in proximity of Geordi's visor?
@Jonathan: generally speaking, good error detection and reporting is a good idea. The problem is that, at times, it can be too expensive, especially when most API clients are never going to hit the error case - yet pay the price for error checks.
Sometimes it's a trade-off. VB throws on arithmetic overflow by default on all integral types - handy for detecting those pesky overflow bugs, but you get checks sprinkled throughout your code for every arithmetic op, most of which will never ever hit it in practice. C# does unchecked arithmetic by default, and you have to use /checked+ to enable that. Which one is preferable?
@Pavel: Most of what I am complaining about is that when an error is reported, the information about what caused that error report is thrown away instead of being encoded into the error message.
See these bug reports:
Due to Microsoft's refusals to fix these problems, and rediculous use of NotSupportedException in derived classes of Type related to generics and emitted types, I am writing my own library to replace System.Reflection.Emit.
In another library that I provide, I use produce two versions, a developer version that does agressive error checking and gives informative error messages, and a release version that assumes the calling program is correct.
Please address when VS 2008 will get SP2 given that SP1 was released Aug 2008 (over 2.5 years ago). Can we also get XP SP4 since SP3 was released May 2008 (3 years ago).
It's important to us since getting a multi-man year effort approved to do a full retest of one of our larger systems is not possible. Our core systems each have from 200,000 to 750,000 lines of .NET code.
You're asking the wrong guy; I know nothing whatsoever about service pack scheduling. And even if I did, which I emphasize I do not, I can't talk about schedules of unannounced releases. Sorry! -- Eric
@Bryan That post approaches theoretical maximums for nerdiness. I salute you, sir!
It's pretty safe to assume:
VS 2008 SP2: Never
Windows XP SP4: Never
Plannning on anything different is foolhardy at best.
Interesting, the world sent you to tell me that I and not it is wrong. What is this world coming to?
Someone at my office is still trying to convince me that a bug in our product is because an uninitialised Guid field is not equal to Guid.Empty and it must be a compiler problem, and/or that he should compare the uninitialised to the string "0000-0...".
I might have to send him this article - although he may just mail you Eric with his theory - so maybe not :)
*I think the reason for his crazy assumptions is because the version the on the customer's machine is not the same as the source he's looking at.
One of the cuious traits I have always dones is to "act as if" you know and then start programming. As a manager and veteran coder I now see more and more developers who get stuck or think they are crazy. I offer a mindset trick that says act as if you have already solved the problem and proceed. That is not to say you dont do your research and try to figure things out, but often I find developers on my team who wait until the answer comes to them. Get past that and often you will see the forest for the trees.