Murky Research

No computers today, but some interesting – and important – math. (And, happy Canada Day, Canadians!) “Car Talk” is a popular weekly phone-in program that has been on National Public Radio for several decades now, in which Bostonian brothers Tom and Ray crack wise and diagnose car (and relationship) problems. On many programs they feature a “puzzler”….

18

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

Simple names are not so simple, Part Two, plus, volcanoes and fried foods

I’ve returned from a brief vacation, visiting friends on the island of Maui. I’d never been to that part of the world before. Turns out, it’s a small island in the middle of the Pacific Ocean, entirely made out of volcanoes. Weird! But delightful. The most impressive thing about the Hawaiian Islands for me was just how obvious were…

9

Simple names are not so simple

C# has many rules that are designed to prevent some common sources of bugs and encourage good programming practices. So many, in fact, that it is often quite confusing to sort out exactly which rule has been violated. I thought I might spend some time talking about what the different rules are. We’ll finish up…

14

Color Color

Pop quiz: What does the following code do when compiled and run? class C{    public static void M(string x)    {        System.Console.WriteLine(“static M(string)”);    }    public void M(object s)    {        System.Console.WriteLine(“M(object)”);    } }class Program{    static void Main()    {        C c = new C();        c.M(“hello”);    }} (1) writes static M(string)(2) writes M(object)(3) uh, dude, this code doesn’t even…

24

What Would Tufte Do?

What is this a chart of? I’ll post the answer tomorrow. UPDATE: Someone has already correctly deduced the answer. (And man, that was fast!) So don’t read the comments if you don’t want spoilers.  

10

Comma Quibbling

[UPDATE: Holy goodness. Apparently this was a more popular pasttime than I anticipated. There’s like a hundred solutions in there. Who knew there were that many ways to stick commas in a string? It will take me some time to go through them all, so don’t be surprised if it’s a couple of weeks until…

277

Mutating Readonly Structs

Consider this program which attempts to mutate a readonly mutable struct. What happens? struct Mutable {    private int x;    public int Mutate() {        this.x = this.x + 1;        return this.x;    }} class Test {    public readonly Mutable m = new Mutable();    static void Main(string[] args) {        Test t = new Test();        System.Console.WriteLine(t.m.Mutate());        System.Console.WriteLine(t.m.Mutate());        System.Console.WriteLine(t.m.Mutate());    }} There are…

28

Why Do Initializers Run In The Opposite Order As Constructors? Part Two

As you might have figured out, the answer to last week’s puzzle is “if the constructors and initializers run in their actual order then an initialized readonly field of reference type is guaranteed to be non null in any possible call. That guarantee cannot be met if the initializers run in the expected order.” Suppose…

42

Why Do Initializers Run In The Opposite Order As Constructors? Part One

Pop quiz! What do you expect the output of this program to be?   using System; class Foo{    public Foo(string s)    {        Console.WriteLine(“Foo constructor: {0}”, s);    }    public void Bar() { }} class Base{    readonly Foo baseFoo = new Foo(“Base initializer”);    public Base()    {        Console.WriteLine(“Base constructor”);    }} class Derived : Base{    readonly Foo derivedFoo =…

36