The Daily Parker

Politics, Weather, Photography, and the Dog

Have laptop, will travel

Like the journeymen of old, I have packed up my tools and traveled far from home to practice my craft. Unlike the journeymen of old, I can go home every weekend.

So, I have a new cube, a new team, and a room at the nearby Extended Stay America. As I get settled, I'll write more on a few subjects familiar to the thousands of other software developers who find themselves in similar circumstances:

  • Work/Life balance when your life is there, you're here, and you bill by the hour (i.e., the importance of finding a good brewpub);
  • Why East Bumble pays better than Chicago or New York;
  • Agile software development on two cups of coffee a day; and
  • How to feel peaceful at O'Hare first thing Monday morning.

At this precise moment, however, I need to obtain a Brita pitcher and a clock with a radio (do I really want to have to futz with streaming audio just to hear Morning Edition?) from the local Target. Then, I'm off to find a brewpub.

Class or struct part 2

Just this morning I wrote about choosing a class over a struct to take advantage of inheritance and abstractness. It turns out, I was wrong.

Well, not that wrong. It's just that this particular design doesn't actually derive that many benefits from inheritance.

First, there will still be a lot of redundant code. The IEquatable<T> and IComparable<T> interfaces both require implementation in concrete classes, so all of the measurement classes would have nearly identical CompareTo and Equals methods. Also, all of the arithmetic operators come in three flavors:

public static bool operator ==(double a, ConcreteClass b)
public static bool operator ==(ConcreteClass a, double b)
public static bool operator ==(ConcreteClass a, ConcreteClass b)

...so all of those need to be in each of the concrete implementations (even if they call the same code in the base class).

And, of course, the Explicit and Implicit operators have to be customized for each class.

Redundancy. I hate it. But it's necessary for this kind of design.

Second, as it turns out, the implementation differs significantly enough between the measurement classes in one key respect that they really can't use a common base class. The problem is, some measurements are actually aggregations of other measurements.

Area, for example, is a length times a length. Volume is either three lengths or a length times an area. Don't even get me started on how to represent velocity.

In my first implementation of representing measurements (still visible as of this writing), I finessed the problem of multi-dimensional values by creating multi-dimensional units, like MeterSquare and PoundSquareInch. That created a real headache in the implementation of conversions, because while converting meters to feet is a simple mulitplication, converting liters to cubic inches is only linear if you proliferate your inch class into square and cubic dimensions.

In other words, despite my goal of having a simple conversion mechanism, I had to write specific conversions for multidimensional measurements anyway, which I managed by putting the knowledge of how to convert units into the units themselves rather than in the measurements where (I think) they belong.

The only way to do that and retain some extensibility was to require each unit to know how to convert itself to a base unit: square meters for area, for example. This, in turn, introduced rounding errors for broad conversions within measurement systems that prevent round-trip conversions from working. In other words, if you convert a very small value to a very big value by using an intermediary, you can't convert the big value back to the same small value. For example, there are exactly 4,014,489,600 square inches in a square mile, but if you convert to square meters first you get 4,014,489,340.464 instead. That's just embarrassing.

In sum, despite what I wrote this morning, I'm throwing out the work that resulted from it and going back to structs for my measurement classes.

And you thought I only picked on Republicans

The best governor we've got claims he didn't know the Daily Show interview was a spoof when he sat down:

"It was going to be an interview on contraceptives...that's all I knew about it," Blagojevich, laughingly [sic], told the St. Louis Post-Dispatch in a story for Thursday's editions. "I had no idea I was going to be asked if I was 'the gay governor.'"
Interviewer Jason Jones pretended to stumble over Blagojevich's name before calling him "Gov. Smith." He later asked if Blagojevich was "the gay governor."

The Daily Show segment aired earlier this month.

In unrelated news, former Chicago Alderman Edwin Eisendrath is running in next month's Democratic primary against Gov. Smith.

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.