The Daily Parker

Politics, Weather, Photography, and the Dog

Class or struct?

I've encountered a problem familiar to veteran C# developers: whether to use a class or a struct for a particular design. So I'm going to follow my own advice and develop first for elegance and second for execution speed.

The specifics: As I mentioned earlier, I'm re-writing the way the Inner Drive Extensible Architecture handles measurements. I've identified 16 scenarios in which I use measurement classes, and I want them to be as intuitive as possible. So, for example, scenario #1 is "instantiate a new measurement:"

Length meters = new Length(15, typeof(Meter));
meters.ToString() == "15 m"

Another scenario is "add two measurements:"

longLength = length1 + length2;

I also expect to use the items in lists:

SortedList<Length> lengths = new SortedList<Length>();

This last scenario requires that the Length class implement IComparable (or IComparable<T>), on the one hand, and that it be lightweight, on the other.

Here's where the class v. struct problem comes in. Structs are lightweight, because the .NET runtime places them on the stack directly instead of placing pointers to them on the stack. In other words, using a class requires indirection, and that takes enough time for you to notice if you're dealing with hundreds or thousands of them.

Therefore, my first attempt, which I am discarding, was to create the measurement classes (Length, Volume, Mass, etc.) as structs.

I soon ran into several problems that convinced me to create an abstract Measurement class that the individual types of measurement will inherit from:

  1. Lots of duplicate code. Adding, subtracting, equality, assignment—they're the same, with minor and specific differences, for each of the measurement types. You can't easily share code between structs. Using inheritance, about 90% of the code can go into the abstract class.
  2. Validation in multiple constructors. I need to validate the initial data used to create measurements, so that attempting to create a length of Fahrenheit throws an exception. Since the entire struct must be initialized before control leaves the constructor, this requires that I put validation code in several places, depending on how the constructors work.
  3. Finally, now that .NET allows property setters and getters to have different visibility (public get with protected set), it's much easier to make classes immutable without making them structs.

If there's a tremendous performance hit, I'll worry about that when the design is stable.

A waste of a perfectly good scandal

Molly Ivins, on congressional reform:

Tom DeLay gets indicted, and all the Republicans can think of is a $20 gift ban. Forget the people talking about "lobby reform." The lobby does not need to be reformed, the Congress needs to be reformed. This is about congressional corruption, and it is not limited to the surface stuff like taking free meals, hotels and trips. This is about corruption that bites deep into the process of making laws in the public interest. The root of the rot is money (surprise!), and the only way to get control of the money is through public campaign financing.

You don't ask the local wolf pack to reform sheep-herding.

Our wacky administration

In its efforts to starve the Federal government out of existence, Bush cut $28 million—and 32 jobs—from the National Renewable Energy Laboratory.

Then he mentioned, in his state of the union speech, that we need renewable energy. Forgetting for a moment that the pusher-in-chief suddenly got religion on our addiction to (foreign) oil, it's still kind of embarrassing that he cut our renewable energy budget at the same time. Or, more to the point for these clowns, embarrassing that they got caught doing it.

So the 32 got their jobs back today:

Two weeks ago, the lab workers, including eight researchers, were laid off at the lab because of a $28 million budget shortfall. Then, over the weekend, at the direction of Energy Secretary Samuel Bodman, $5 million was transferred back to the lab to get the workers back on the job.
Lab officials are ecstatic about getting the positions back, although they say the remaining $23 million shortfall has forced delays in research subcontracted to universities and companies. Still, it was an untimely issue for the president, who flew to Colorado to push the energy initiatives he announced in his State of the Union address.

Quel faux pas!

Good morning!

The city of Eureka, Nunavut, in way-Northern Canada, has its first sunrise of the year today around 11:30 CT (17:30 UTC). Technically the sun never actually gets above the horizon, but a tiny bit of it will scrape along the southern horizon for about an hour before disappearing until tomorrow.

Eureka is typically the northernmost weather station that sends hourly reports to NOAA, and this time of year it's almost always on the world's coldest places list. For example, at this writing, Eureka is -41°C (-42°F)—but it's a dry cold, so you don't feel it as much.

Now <i>this</i> is Chicago

The 7:00 am (13:00 UTC) temperature at Chicago O'Hare was -22°C (-7°F), the coldest temperature recorded there since 1 February 2004.

Yes, this is Chicago, where you can see wacky temperatures like these:

Feb. 2006 Max temp Min temp
Sat 11th 1.7°C -3.3°C
Sun 12th -1.1°C -5.0°C
Mon 13th 3.9°C -8.9°C
Tue 14th 13.3°C -4.4°C
Wed 15th 5.6°C 0.0°C
Thu 16th 2.8°C -5.6°C
Fri 17th -4.4°C -15.6°C

At least we're not in Douglas, Wyo., where they're waking up to -36°C (-32°F) this morning.

Database error causes $8.1M budget shortfall

Software security expert Bruce Schneier reports on a database error in nearby Porter County, Ind., that has cost the county millions of dollars, and what that means to software design:

A house erroneously valued at $400 million is being blamed for budget shortfalls and possible layoffs in municipalities and school districts in northwest Indiana.
County Treasurer Jim Murphy said the home usually carried about $1,500 in property taxes; this year, it was billed $8 million.

If you don't like the weather...

Two days ago it was 13°C (56°F) in Chicago. Yesterday a storm dumped 28 mm (1.1 in) of rain on us before the cold front behind it dropped us below freezing. This was the largest temperature drop in 42 years here.

Here's our street after the storm:

Today we're looking forward to overnight lows around -18°C (-1°F). Fortunately, the local El stop has heat lamps to stand under when it gets cold. Sometimes, though, you have to share them with these guys:

In related news, scientists report that Greenland's glaciers are flowing faster, dumping more fresh water into the North Atlantic, which in turn may accelerate global warming. So maybe the pigeons won't need to hang out with commuters under heat lamps much longer.

Quick hits

Some items in the news today that probably should go without comment:

Maureen Dowd rocks

...even if she doesn't need men. In her column today (sub.req.) she pulls no punches with two men no one needs:

As the story of the weekend's bizarre hunting accident is wrenched out of the White House, the picture isn't pretty: With American soldiers dying in Iraq, Five-Deferment Dick "I Had Other Priorities in the 60's Than Military Service" Cheney gets his macho kicks gunning down little birds and the occasional old man while W. rides his bike, blissfully oblivious to any collateral damage. Shouldn't these guys work on weekends until we figure out how to fix Iraq, New Orleans, Medicare and gas prices?