In this series we’re exploring the problem “a player can use a weapon, a wizard is a kind of player, a staff is a kind of weapon, but a wizard can only use a staff”. The best solution we’ve come up with so far is to throw a conversion violation at runtime if the developer makes a mistake, which seems less than optimal.
A common problem I see in object-oriented design is:
- A wizard is a kind of player.
- A warrior is a kind of player.
- A staff is a kind of weapon.
- A sword is a kind of weapon.
- A player has a weapon.
But before we get into the details, I just want to point out that I am not really talking about anything specific to the fantasy RPG genre here. Everything in this series applies equally well to Papers and Paychecks, but wizards and warriors are more fun to write about, so there you go.
OK, great, we have five bullet points so let’s write some classes without thinking about it! What could possibly go wrong?
Occasionally I’m asked to review code that has a lot of repetition in it. Like, for instance, someone is writing a function memoizer:
static Func<A, R> Memoize<A, R>(this Func<A, R> function)
var cache = new Dictionary<A, R>();
return argument =>
if (!cache.TryGetValue(argument, out result))
result = function(argument);
cache[argument] = result;
And then they will write remarkably similar code for functions of two arguments, functions of three arguments, functions of four arguments, and so on. “I want my code to be DRY; how can I avoid repeating myself?” is what the developer asking for review usually says.
This is a sequel to my 2009 post about division of long integers.
I am occasionally asked why this code produces a bizarre error message:
Console.WriteLine(Math.Round(i / 6000000000, 5));
i is an integer.
The error is:
The call is ambiguous between the following methods:
'System.Math.Round(double, int)' and 'System.Math.Round(decimal, int)'
Um, what the heck? Continue reading
No tech today, but some fun for Friday.
The 2015 Moisture Festival is over; if you’re not familiar with the festival, it’s a month-long celebration of old-timey and modern vaudeville, comedy, variety, burlesque and circus arts at multiple venues here in Seattle. Hundreds of artists come in from around the world, and often end up staying in my spare bedroom to cut down on their costs. This year I had the pleasure of spending a week with one of the world’s greatest jugglers, Niels Duinker, during the festival and I thought I’d post some links to his videos. When I was a college student I taught myself to juggle, but that was before all the great youtube tutorials that are now available.
Here is Niels on how to do three — start here if you’ve never juggled before.
Back in the day I got a few pretty solid four ball patterns going but I never managed to get more than eight throws with five, or six throws with three in one hand. Maybe I’ll give it another shot!
Gareth Wilson from Fog Creek interviewed me the other day for their series on the lives of developers; we talked about playing Dam Buster on the Commodore PET and typing in programs in WATCOM VI and all kinds of stuff. Thanks Gareth, I enjoyed it very much. You can read the interview here.
Last time I explained why the designers of C# wanted to have both checked and unchecked arithmetic in C#: unchecked arithmetic is fast and dangerous, checked arithmetic is slightly slower but turns subtle, easy-to-miss mistakes into program-crashing exceptions. It seems clear why there is a “checked” keyword in C#, but since unchecked arithmetic is the default, why is there an “unchecked” keyword?
One of the primary design goals of C# in the early days was to be familiar to C and C++ programmers, while eliminating many of the “gotchas” of C and C++. It is interesting to see what different choices were possible when trying to reduce the dangers of certain idioms while still retaining both familiarity and performance. I thought I’d talk a bit about one of those today, namely, how integer arithmetic works in C#.
Hey all, apologies for the sudden and very long break there. A number of people asked me if I was OK, falling off the face of the earth like that — thanks for your concern, I am fine, just over-busy.
Ricky Jay has a line in The Spanish Prisoner where he says that when your hobbies interfere with your work, that’s fine, but when they interfere with each other, you have a big problem. I had that terrible problem back in November and something had to give, so I stopped blogging and woodworking for a while there. The combination of a tight deadline at work, helping Mark get the next edition of Essential C# ready for later this year, and working on a series of educational videos ended up consuming all my available bandwidth for technical stuff.