Saturday, February 14, 2009

My favorite gripes about .NET

I've been coding primarily in .NET (VB.NET and C#) ever since it first came out, back in 2002, and on the whole, I think it's amazing. Microsoft did a whole lot right when they created the .NET framework.

I recently just took the .NET 3.5 certification exam, and studying for it, after a few years away from coding, gave me a chance to realize that MS also got a number of details wrong along the way. Allow me to share my favorites:

Bizarre Side-effects

Much of the .NET framework was well-designed, with the poor coder who has to use it in mind. But there are occasional pieces that make me shake my head. For instance, imagine that you saw this code in someone's application:

CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = "MyKeyContainer"; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);

Unless you happened to know the CryptoAPI very well, would you realize that this is how you create and store a key-pair? In other words, creating and storing the key is a side-effect of calling the RSACryptoServiceProvider constructor, if you happen to pass a CspParams object that happens to have its KeyContainerName property set.

It should have been designed to work something like this, of course:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

rsa.Key = RSACryptoServiceProvider.CreateKey();

rsa.PersistKey("MyKeyContainer");

Bizarre Names

When you want to specify that it makes no sense for an assembly to run unless it has the permissions to open a file dialog box, you can flag that assembly with the following attribute:

[assembly: FileDialogPermission(SecurityAction.RequestMinimum)]

And that makes some reasonable sense: you're saying that it needs, at a minimum, to be able to show the file dialog box. Still, a better name would probably be something like "Require" than "RequestMinimum": it's less ambiguous.

But if you want to step your security up a notch, and tell the runtime to automatically deny all permissions other than the ones you're listing, you use the enumeration SecurityAction.RequestOptional:

[assembly: FileDialogPermission(SecurityAction.RequestOptional)]

To call required permissions "optional" makes absolutely no sense, and it makes it significantly more difficult for your average coder to figure out what's going on. It would be much easier to understand if MS had chosen to call it something like "RefuseAllExcept":

[assembly: FileDialogPermission(SecurityAction.RefuseAllExcept)]

(For what it's worth, it's called "Optional" because whoever came up with the name was thinking from the perspective of the runtime: "Do I need to throw an exception if the assembly doesn't have this permission? No? OK, then it's an optional permission." But you don't flag an assembly with this attribute because you're concerned to communicate that it might not need this permission: you add this attribute because you only get the permission you need, i.e., you want to make sure that the assembly is good and secure and only does things you're explicitly aware of, and that the runtime actively refuses to let it do anything you hadn't previously thought about.)

Screwy Parameter Orders

Certain methods and classes in the .NET framework typically take parameters in one order; but other methods and classes take them in a very different order. There are numerous examples of this, but to take just one: if you want to format a string according to a specific culture using the String.Format() method, you pass parameters in the order "CultureInfo", "FormatString".

String.Format(new CultureInfo("en-UK"), "c", 100)

But if you want to use the ToString() method of an object to format it, you pass them in the order "FormatString", "CultureInfo".

d.ToString("c", new CultureInfo("en-UK"))

Stupid things like that just make a framework harder to use than it needs to be: it's one more thing to remember in a world where I've already got too much going on.

Inconsistent Object Instantiation

In general, there are two different ways you can get an instance of a class: you can do it yourself, or you can get something else to do it for you. Either one works fine, though there are reasonably good arguments for preferring the second (known as a "Factory Pattern"). The problem is that the .NET framework is inconsistent throughout. Some classes can only be instantiated with their own constructor:

AppDomainSetup setup = new AppDomainSetup();

Other closely related classes can only be instantiated through a static factory method:

AppDomain domain = AppDomain.CreateDomain("My App Domain");

And others can be instantiated either way:

Process process1 = new Process(); Process process2 = Process.Start("cmd.exe");

Frankly, I probably wouldn't care that much: it's mostly a stylistic difference, and the MS documentation is handy and usually pretty good. But any number of questions in the 70-356 exam turn on knowing whether you use a static or an instance constructor, and remembering that you can only get an AppDomain object by calling a static constructor, but you can get a Process object either way, gets old after a while.

Event Handling in C#

I've been using VB since version 1.0, long before it was a commercially viable language, and long before it had anything like object-oriented features. VB.NET is arguably a completely different language, but one of the things that remains very much the same, and very easy to do, is the handling of events. The syntax for declaring them is simple and intuitive, and easy enough for nearly anyone to understand. To handle an event in the most basic fashion, you have to specify four different "elements": you declare (1) the event and (2) the event handler, (3) you connect them by instantiating the event class using "WithEvents", and then (4) you raise the event.

Class AlarmClock Public Event Ring()

Sub MakeMeRing() RaiseEvent Ring() End Sub

End Class

Class Sleeper

Public WithEvents Alarm As New AlarmClock

Sub WakeUp() Handles Alarm.Ring Console.WriteLine("I'm awake, already.") End Sub

End Class

In contrast, in C#, for the most basic event, you have to follow seven steps. You declare (1) the event (2) the event handler, and (3) instantiate the class holding the event. You then wire up the event by (4) instantiating the delegate, and (5) adding it to the event. You then raise the event by (6) checking if there are any handlers, and then finally (7) you raise the event.

class AlarmClock { public event EventHandler Ring; public void MakeMeRing() { if (Ring != null) { Ring(this, new EventArgs()); } } }

class Sleeper { public AlarmClock alarm = new AlarmClock();

public Sleeper() { alarm.Ring += new EventHandler(alarm_Ring); }

void alarm_Ring(object sender, EventArgs e) { Console.WriteLine("I'm awake, already."); } }

For only one of those steps (declaring the event) does the language itself provide any structure for you: you otherwise have to use other unrelated language elements, such as delegates, constructors, and operator overrides: and you have to understand each of these, and think about each of these, before you can raise and handle events. In contrast, with VB, you only have to think about events, and not about three or four other unrelated language elements.

I'll grant that the C# implementation is elegant and flexible: but if you want, you can do it the same way in VB. VB just provides an alternate, much simpler way of handling simple events. The C# team has implemented plenty of cool features like LINQ, anonymous methods, generics, extension methods, nullable value types, and all sorts of other cool things. It's a shame that they haven't figured out a simpler way of raising and handling events. ents.

I've got more gripes – of course J – but the rest will have to wait.

Interesting Recommendation

I received an email from Amazon today, with the following recommendation in it:

Dear Amazon.com Customer,

We've noticed that customers who have purchased or rated books by Aristotle have also purchased If You Got It, A Truck Brought It: Our First Adventure by Robin Smith. For this reason, you might like to know that If You Got It, A Truck Brought It: Our First Adventure is now available.  You can order yours for just $8.99 by following the link below.

Product Description
With bright colorful illustrations and adorable characters, Josie and her freewheeling friends take you on an adventure to learn how a book is made.Along the way we'll get a start on learning colors, numbers and the alphabet.

 

Notice the basis of the recommendation: because I'd purchased books by Aristotle, they recommended a particular children's book. I smell a statistical cluster in there somewhere.

Friday, February 13, 2009

Initial Thoughts on WPF

I've started work on a quick-and-dirty little application for our church treasurer, who wants to be able to email donation statements to members, rather than send them via increasingly expensive snail-mail each quarter. I've written more WinForms apps than I can count, so I thought I'd try this one using Microsoft's still fairly new Windows Presentation Foundation. What you read on the MSDN website is that WPF is the hottest thing ever, and they give sample shots like this to whet your appetite.

Granted that I'm still at the very beginning stages of learning WPF, I wanted to give my first impressions. So some thoughts:

  1. Technically, it sounds really cool. I like the fact that, unlike GDI+, it's able to use hardware acceleration, with all the cool 3D potential that implies.
  2. I'm not sure about XAML yet. I'm not a huge fan of having to learn a proprietary markup language. Still, declarative programming? Interesting. Inability to debug anything I write? Interesting in an entirely different sense.
  3. It's a huge change from WinForms. Learning WPF is like learning ASP.NET: it's a completely different model. Don't expect to be able to pick it up in a few days.
  4. The UI design tools in Visual Studio are regrettably slow and clunky. I've got a fairly beefy machine, and it still takes several seconds to load up a very basic window in design view, with almost no controls on it. About every other time I switch to the design window, it tells me that I need to click on a special ribbon for it to refresh, because something (I have no idea what) has changed.
  5. The bundled controls are uninspired and uninspiring. To take just one example, for some astonishing reason, WPF has gone through three iterations, and it still doesn't come with a datagrid – which is perhaps the most basic control required in any business app. There are several third-party grids available, and MS has just released a "WPF Toolkit", which does have a basic datagrid. But still. It doesn't give me a lot of confidence that MS actually expects anyone to use this yet.
  6. There's a ton of inconsistency between the different UI controls. For instance, if you want to specify the text of a menu item, you set the Header attribute, like this:

    <MenuItem Header="Menu Item 1"/>

    But if you want to specify the text of a button or of a textbox, you specify it in the content area of the tags:

    <Button>Sample Button</Button>
    <TextBox>asdfasdfasdf</TextBox>

    But then get this. If you want to set the text of the button in code, you set its Content property:

    sampleButton.Content = "Content for a button";

    But if you want to set the text of the textbox, you set its Text property.

    sampleTextBox.Text = "Text for a textbox";

    Was there really nobody inside Microsoft who thought, "WTF?" MS has boasted any number of times that since they started from a clean slate with WPF, they got the design right. I can't yet speak for the overall elegance of the design, but if they can't even get the simple stuff right – consistent names and property accessors, for instance – it doesn't give me a lot of hope that they nailed the big stuff.
  7. The documentation isn't up to Microsoft's standards. It's there, and it's technically correct, but it's not terribly helpful. A good example is the official documentation for the Hyperlink.RequestNavigate event. Take a look at the page. It has, well, all sorts of vaguely interesting information. But it's entirely useless for someone who wants to understand the context for this event. When is it fired? Why does it exist? How do you use it? (Or the question I'm trying to troubleshoot at the moment, why does it not seem to be firing for me?) Compare that page to the documentation for a similar topic in ASP.NET, the Button.Click event. That page has a brief explanation of the context in which the event occurs, sample code in two languages, and links to four different articles for more information. Presumably the improved documentation will come with time. But it's not there yet.

I'm glad that Microsoft continues to improve their technology. But Microsoft also has a tendency to hype a particular technology, only to abandon it more-or-less completely within a few years. Anybody remember DNA? Or DAO? Or RDO? I don't think WPF is the same: it's more like ASP.NET vs. ASP: the underlying technology change is that significant. But unlike the migration to ASP.NET, the initial benefits of migrating to WPF are less clear to me.

Monday, February 9, 2009

Cisco has removed the WRT100 1.0.0.2 firmware

A month or two back, I posted a solution to the highly problematic 1.0.0.4 firmware for the Linksys WRT 110 wireless-N router. (In brief: a wide range of folks, including yours truly, have reported that when the 1.0.0.4 firmware will cause the router to freeze up if a second computer attempts to join the network. The solution was to download and install the 1.0.0.2 firmware from the WRT 100 support page.)
Unfortunately, Cisco has now removed the 1.0.0.2 firmware link from the WRT 100 page: the only option they present is to download the 1.0.0.4 firmware, which, as I mentioned, is highly problematic. Color me unimpressed with Cisco.
For folks stopping by because they're experiencing problems with the 1.0.0.4 firmware, I don't have a lot of options. I couldn't find the copy of the 1.0.0.2 firmware, unfortunately, or I'd post it for download. My only real suggestion is to note that if one or more of your machines are Vista, some of the folks on the Cisco forums have suggested disabling IPv6 support. ("Start", right-click on "Networks", choose "Properties", "Manage Network Connections", right-click on your wireless network adapter, deselect "Internet Protocol Version 6".) I haven't tested this: YMMV: let me know if it works for anyone.
Do other folks out there still have a copy of the 1.0.0.2 version of the firmware? If so, I'm happy to throw it up on my website. I've continued poking around for open-source firmware, but none of the various options seem to support the Ralink RT2720L/RT2780 platform.
Other thoughts?
[Added 2/10/09: Other folks have reported that disabling IPv6 under Vista does in fact work for them. That doesn't exactly excuse Cisco -- nothing you send at a router should take it down, and if it does, that's really, really bad -- but it does seem like the best workaround, now that Cisco has removed the working copy of the firmware.]
[Added 2/16/09: It turns out that Linksys has an alternate version of their website, and on that alternate version, they've still got the working 1.0.0.2 firmware.  In case they remove that again at some point, I've also posted a copy on my own website.]

Thursday, February 5, 2009

Horrified

I don't understand our laws.

The AP is reporting that an 18-year old girl in a Florida abortion clinic accidentally gave birth to a live infant. One of the workers at that clinic allegedly then took the still-breathing child, stuffed it into a plastic bag, and threw it away to die. Not surprisingly, the local DA's office is investigating whether homicide charges should be filed.

What astonishes me, though, is not so much that someone could do that to a breathing infant, but that if the clinic had succeeded in killing the infant actively and intentionally, just a few minutes earlier, no charges would have been possible.

I'm a moderate when it comes to abortion: I think there's room for plenty of grey in the debate. I can't quite convince myself that a single fertilized cell should be treated, morally, as a full human being. But neither is a 23-week fetus merely a blob of tissue. It's not one thing when it's inside the womb and something else when it's outside: it's the same thing. It doesn't change from "lump of tissue" to "individual human being" because it moved eight inches.

It's also a huge category mistake, in my mind, for the argument to turn on whether and/or when a fetus is a human being. Something doesn't have to be a human being for it to have positive moral standing. We've pretty much all agreed that cruelty to animals is a bad thing, that it should be illegal to kill whales and other intelligent mammals, and that endangered species should be protected by law. A fetus doesn't have to be a full human being in the eyes of the law for the state to have a legitimate interest in its care and treatment: and that interest, quite frankly, usurps any individual right to privacy.

This is why I'm convinced that Roe v. Wade remains a horrible and incomprehensible decision. My right to swing my fist stops where my neighbor's nose begins; and any right to privacy stops where an infant's health and safety begins. The state can intrude on the privacy of neglectful or abusive parents, and take their children away from them; and the state should similarly have the right to legislate against abortion. This was an issue which should never have been taken out of the hands of the legislative branch. Incomprehensible laws, and barbaric acts, like the one currently being explored in Florida, are the result.

Friday, January 30, 2009

Coyote Scat

Caedmon and I were wandering around our backyard yesterday, and we came across quite a large quantity of feces on the ground. Given Caedmon's age, it wasn't surprising that he wanted to investigate further; nor, given my interest in everything outdoors, was it surprising that I wanted to as well.

The feces looked like they might have come from a dog, and we've got a few large friendly beasts of that sort roaming our neighborhood. But we also see coyotes in our backyard periodically (I've never managed a good picture of one), and in fact, we had spotted one just two days ago. And it turns out that the feces in question appeared to have hair in them, which isn't normal for a domestic canine, nurtured on traditional dog food.

From Coyote Scat
From Coyote Scat

My suspicions were confirmed when, a few yards away, I spotted the (very) scattered remains of a small animal, probably a rabbit but perhaps a cat. Its fur was lying in patches over maybe a 15 square foot area.

From Coyote Scat
From Coyote Scat

Given the substantial quantity of feces, I have to imagine that one or more coyotes have been frequenting this site for some time.  I don't really mind coyotes except when they eat our cats (we've lost two over the years), but it's still a little odd that we've apparently got one holed up in our backyard.

Needless to say, Caedmon was fascinated by all of this, and even managed to step repeatedly into the feces in question, smearing them all over his shoe. Lovely.

From Caedmon (17 Months)

Monday, January 5, 2009

Chatting with Charlie about Gaza Part 2

My pastor and I had coffee today, to discuss and compare our different views on the current situation in Gaza. Our perspectives, of course, don't entirely mesh: I'm more likely to find Israel's actions in Gaza defensible (though not entirely so), and he's more likely to find them reprehensible (though not entirely so). Charlie and I have always had the ability to debate collegially and disagree while remaining friends, and today was no different: neither of us particularly changed our minds, but we were each able to talk about our perspectives, and learn from the other.

One interesting aspect of our conversation is that while we continued to disagree on where the (primary) blame lay for the latest round of violence, we were largely able to agree on what needed to happen next. I don't want to speak for Charlie, but this is what I hope Israel does in the coming months:

  1. Once the cease-fire is in place, Israel should immediately lift its blockade on Gaza, and invest in rebuilding the infrastructure its military has destroyed. Israel has made it clear to Gaza and to the world that it wields a very large stick; it should show Gaza an even more substantial carrot.
  2. Israel should look the other way for a few months. I can't imagine that Hamas and Islamic Jihad (or their remnants and successors) will allow an attack of this size to pass without an attempt at retribution, cease-fire or no. If Israel is serious about peace, it should plan on ignoring several major violations of the cease-fire in the coming months, without re-imposing the blockade or responding with more violence. But Israel should also make it plain to Hamas (or its remnants) that its patience is limited, and that if violence continues beyond a very short period, Israel will wield an even larger stick, with even more dire consequences.
  3. Israel should enforce its own ban on settlement-building in any contested territory, and the government should bend over backwards to ensure that its courts offer quick and fair hearings for any Palestinian lawsuits over contested land. It shouldn't take a Palestinian farmer five years to evict illegal settlers. And it should begin carrying out its own demolition orders.
  4. Israel should announce to the world that it acknowledges, in principle at least, that East Jerusalem is the legitimate capital of Palestine, and will negotiate the many remaining sticky details with Fatah in good faith.
  5. If this calms things down, in several months, Israel should begin the release of Palestinian political prisoners who are not guilty of actual bloodshed.

I don't think Israel should do these things because it has been wrong and its enemies are right: I think Israel should do these things because its people want peace, and it has the strength to make peace, if it has the will to do so.

If Israel took these steps, it would take all the wind out of Hamas' sails. Hamas is able to sell the Palestinian people the fantasy of Israel's destruction because the Palestinians believe the possibility of peace and prosperity is just as much a fantasy. If Israel could show them that it is as serious about peace as it is about deterrence, that its people are willing to risk suffering as much for peace as they have in war, Hamas' evil and corrupt ideology would lose all its appeal. The IDF has clearly regained its deterrent capability: and Israel will have a (perhaps brief) opportunity to be merciful without appearing weak. If Israel takes advantage of the opportunity its military has created, perhaps even this grievous war will have been worth the price.