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.

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.

State of Delusion

From Paul Krugman's column (sub.req.) this morning:

This administration is all politics and no policy. It knows how to attain power, but has no idea how to govern. That's why the administration was caught unaware when Katrina hit, and why it was totally unprepared for the predictable problems with its drug plan. It's why Mr. Bush announced an energy plan with no substance behind it. And it's why the state of the union—the thing itself, not the speech—is so grim.

And this little tidbit from Poynter Online correspondent Alan D Abbey:

I ran across this brief couplet upon perusing "The Norton Book of Light Verse" with my son, who needed a short poem for something he is doing in school. It's a nice comment on the current media environment, and the explosion in volume, at least, of content and brands. It's by 17th-century physician and poet Samuel Garth, and it goes like this:
"What frenzy has of late posssess'd the brain
"Though few can write, yet fewer can refrain."

Heh.

McAfee, I hate thee

One of my clients has had a recurring server issue caused, it seems, by McAfee Anti-Virus. So we're switching to Symantec.

The problem has been that, for quite some time, the naPrdMgr.exe process (which handles product updates) has gone into a death-spiral, consuming 100% of CPU cycles and making the server totally unresponsive to anyone else. I've finally gotten in touch with McAfee, and they said the client's license has expired.

OK, so how does an expired license crash a server? When McAfee Anti-Virus doesn't have a current license, and it attempts to get an update from home, it crashes. Yes. It crashes. This behavior is a known defect, but they won't fix it for us unless we renew the license. So until we pay them we can't fix our server, and every day, like poor Mr. Bix[1], I have to fix it.

Now, let me explain how Symantec does this, because I think it's instructive, and I've just updated my own virus protection in this fashion:

  1. Forty-five days before the license expires, the server itself sends an email to the administrator, right there on site, and pops up a helpful box when you log into the server for any purpose.
  2. To update the license, you pay your fee, then they email you a license certificate.
  3. You go online and enter the certificate number, then they email the license to you. This two-step process seems cumbersome, but it helps ensure the right person gets both files, and the files aren't misplaced.
  4. You go to the server, log into the virus console, and slurp up the license. Boom: done.

So instead of giving McAfee more money, we're switching to Symantec.

[1] I refer, of course, to the following passage from Did I Ever Tell You How Lucky You Are? by Dr. Seuss:

And poor Mr. Bix! 
Every morning at six,
poor Mr. Bix has his Borfin to fix! 

It doesn't seem fair. 
It just doesn't seem right, 
but his Borfin just seems 
to go shlump every night. 

It shlumps in a heap, 
sadly needing repair. 
Bix figures it's due 
to the local night air. 

It takes him all day to un-shlump it. 
And then....

The night air comes back 
And it shlumps once again!

So don't you feel blue. 
Don't get down in the dumps. 
You're lucky you don't 
Have a Borfin that shlumps.

Because they're the phone company

We were dark for over 6 hours today because someone at SBC did something, though no one seems to know who or what. The result was that the Inner Drive Technology World Headquarters had no phone or Internet service from 9:15 am CT/15:15 UTC until 3:30 pm CT/21:30 UTC.

Sadly, this came on the first day of our Weather Now beta launch, which shows off some of our coolest stuff ever. (At this writing it's still a few hours behind, with weather from lunchtime today, but it's catching up as fast as it can.)

One of my friends asked, "How can they do that?" Well, like I said...they're the phone company.

Use property accessor methods, even inside your classes

Genius Boy here just spent ten minutes debugging a class because I used the private field directly inside the class, instead of the property accessor method.

Here's what the property looked like:

public string FileSpec
{
	get { return _fileSpec; }
}
private string _fileSpec;

Notice there's no set method. The file spec is set only in the constructor, and is immutable at runtime. That means that throughout the class I had code like this:

if (File.Exists(_fileSpec))
{
	_cache.Load(_fileSpec);
}

The problem? Well, the private data could contain any number of tokens representing disk or remote folders, which wasn't a requirement when the class was first built.

The solution? C# 2.0 gives you the power to create an accessors of different visibilities, like this:

public string FileSpec
{
	get { return _fileSpec; }
	private set
	{
		_fileSpec = DeTokenize(value);
	}
}

The constructor now looks like this:

public MyClass(string fileSpec)
{
	FileSpec = fileSpec;
}

And then the code that uses the value can do this:

if (File.Exists(FileSpec))
{
	_cache.Load(FileSpec);
}

All better.

Two slightly related items before going back to work

First, New York Times columnist Bob Herbert has a great column today (sub.req.):

[The President's] breathtaking arrogance is exceeded only by his incompetence. And that's the real problem. That's where you'll find the mind-boggling destructiveness of this regime, in its incompetence. ... [T]he plain truth is that he is the worst president in memory, and one of the worst of all time.

Second, I've been ambivalent about the Times charging $49.95 per year to read most of its content, but I think more and more that it's fair. I don't know, though. I pay it, because I read Krugman and Dowd every week, and I like occasionally reading the Magazine. What are other people's thoughts on this?

Why regulation was a good idea

Another thing government does better than business: make businesses play nicely with each other.

Cable companies and telephone companies are fed up with the free Internet because they have to carry it on their backbones for free. So they're looking for ways to charge for use, including creating premium access for a fee.

One of the easily foreseen ways this "premium access" could manifest, as the Washington Post reports, looks like this:

[Y]ou may one day discover that Yahoo suddenly responds much faster to your inquiries, overriding your affinity for Google. Or that Amazon's Web site seems sluggish compared with eBay's.
...For the first time, the companies that own the equipment that delivers the Internet to your office, cubicle, den and dorm room could, for a price, give one company priority on their networks over another.

Perhaps it's time to re-regulate telecommunications? Or maybe I'm wrong, and we should de-regulate further. How about letting the Post Office charge more to deliver mail from certain buildings? Or how about letting electric utilities charge differential rates by political affiliation?

More on Google

Adam Sharp, of Maryland-based Sharp SEO, actually read through the Justice Deptartment's Google subpoena. He posted a blog entry excerpting and linking to the actual Google subpoena which is, in turn, hosted on Ziff-Davis' website:

In Google’s understanding, Defendant would use the one million URLs requested from Google to create a sample world-wide web against which to test various filtering programs for their effectiveness. Google objects to Defendant’s view of Google’s highly proprietary search database—the primary reason for the company’s success—as a free resource that Defendant can access and use, some levels removed, to formulate its own defense.

Now here's my take on Internet privacy.

Astute readers may notice that Sharp's blog has no link to biographical information, nor does his "under construction" corporate site. Really astute readers will notice that he has a link to my blog entry on this topic and figure out how I knew about his blog.

When Sharp linked his blog to mine, our blog engines shook hands, and my blog engine made a log entry commemorating the event. I saw the log entry and clicked on the referring link to find out who was linking to me. I expect he's going to do exactly the same thing in reverse shortly after I hit the "Post to Weblog" button.

Since I wanted to link back to his blog, I wanted to give him proper attribution. I haven't written for a newspaper since college, but I like to think I still retain some journalistic sense about sourcing my material. So I first looked on his blog for an "About" page or some other identifying material. Finding none, I browsed to his corporate website and found the "under construction" page. No info there.

Next, I ran a WHOIS query on his domain name, and got exactly what I wanted: his name and state of residence, which is sufficient for proper attribution.

(I also found out, in preparing this post, that my own registration is quite out-of-date. My apologies to any stalkers who have wasted time lurking in the wrong place.)

My point is this: Privacy on the Web is difficult to maintain. You have to take active steps to limit what people can find out about you, especially where public databases like the Internet registry are concerned. And, every Website you visit logs your IP address, browser, referring page (where you were when you clicked on the Website's own link), and click-stream (how much time you spent viewing each page, and in what order you viewed them).

That said, if someone wants to find you, they will. The Internet doesn't really make that any easier than it was 30 years ago when most people could be found by calling 411. Oh wait: most people can still be found through 411. Never mind then.

We as a people have always given up certain kinds of privacy as a cost of having an open society. Ours was the first country where land records were made public, the better to have good title to property, which helps everyone. Corporate documents, including personal information about coporate ownership, is public information: it encourages corporate responsibility, among other things.

The Internet is the same. You get all this information at your fingertips, but in order to give it to you, the provider needs to know where to send it (your IP address) and how (your browser type). If you want to have your own piece of the Internet, you need to tell some registrar somewhere who you are—at the very least so you can pay the registrar.

This is a complicated topic on which I will write more after I shovel the walk.