Uses and misuses of implicit typing

One of the most controversial features we’ve ever added was implicitly typed local variables, aka “var”. Even now, years later, I still see articles debating the pros and cons of the feature. I’m often asked what my opinion is, so here you go. Let’s first establish what the purpose of code is in the first…

54

Danger, Will Robinson!

As long-time readers of this blog know, I am often asked why a particular hunk of bad-smelling code does not produce a compiler warning. “Why not?” questions are inherently hard to answer because they turn causation on its head; normally we ask what caused a particular thing to happen, not what caused a particular thing…

47

All your base do not belong to you

People sometimes ask me why you can’t do this in C#: class GrandBase{  public virtual void M() { Console.WriteLine(“GB”); }}class Base : GrandBase{  public override void M() { Console.WriteLine(“B”); }}class Derived : Base{  public override void M()   {     Console.WriteLine(“D”);    base.base.M(); // illegal!   }} The author of the most-derived class here wishes to…

9

Graph Colouring With Simple Backtracking, Part One

As regular readers know, I’m interested in learning how to change my C# programming style to emphasize more concepts from functional programming, like use of immutable rather than mutable data structures and use of declarative control flow like LINQ queries instead of imperative control flow in the form of loops. I thought I’d solve a fairly…

23

Making the code read like the spec

As I mentioned a while back, there are some bugs in the compiler code which analyzes whether a set of classes violates the “no cycles” rules for base classes. (That is, a class is not allowed to inherit from itself, directly or indirectly, and not allowed to inherit from one of its own nested classes,…

68

Style follows semantics

Which is better style? bool abc;if (Foo())  abc = Bar();else  abc = false; vs bool abc = Foo() && Bar(); ? To me, this comes down to the question “is Bar useful solely for obtaining its value, or also for its side effects?” The stylistic choices should typically be driven by a desire to clearly…

44

Continuing to an outer loop

When you have a nested loop, sometimes you want to “continue” the outer loop, not the inner loop. For example, here we have a sequence of criteria and a sequence of items, and we wish to determine if there is any item which matches every criterion: match = null;foreach(var item in items){  foreach(var criterion in…

39

Always write a spec, Part Two

Upon submitting that specification for review, even before seeing my code, Chris found a bug and an omission. The omission is that I neglected to say what happens when the ref variable is an index into a fixed-size array buffer. As it turns out, that case is also rewritten as a pointer dereference by the…

20

Always write a spec, part one

Joel had a great series of articles many years ago about the benefits of writing functional specifications, that is, specifications of how the product looks to its users. I want to talk a bit about technical specifications, that is, a specification of how something actually works behind the scenes. A while back, I described how…

27

Events and Races

Here’s a question similar to one I saw on stackoverflow the other day. Suppose you have an event: public event Action Foo; The standard pattern for firing this event is: Action temp = Foo;if (temp != null)      temp(); What the heck is up with that? Why not just call “Foo()” ? First off, this pattern…

30