I think I'm an XMLReader poo pooer…

Yesterday I posted a comment to Scott Hanselman’s post about his preference for XmlReader over techniques like XPATH. Here’s my comment if you’re lazy like me:

I go 100% in the other direction. I much prefer to use XPATH as I find it easier to see what the code is doing in a single expression like:

//Foo[1]/Bar[@val = ‘gerrard’]

rather than ten or 15 lines of procedural code.

Perf is great, but in most of my apps simple maintanability and easy ramp up are way more important that getting every single drop of speed…

Today Scott posted a follow-up, including the following:

I also received a number of poo-poo emails that said “use XPath” or “don’t bother” and “the performance is good enough.”

Sure, if that works for you, that’s great. Of course, always measure before you make broad statements. That said, here’s a broad statement. Using an XmlReader will always be faster than the DOM and/or XmlSerializer. Always.

Can I nominate myself for spokesman of the poo pooers? Call me the Mort Spokesman if you like ;).

I don’t think anyone is suggesting that there isn’t a speed difference, only that the perf improvement may not be worth the decrease in readability and maintainability of the code. Again, I agree 100% that there is some perf difference, I just don’t believe it’s enough of a difference to make it worth the cost in terms of complexity.

Beyond that, I can’t imagine many real world applications where that speed difference (even if it is orders of magnitude) isn’t going to be dwarfed by the time it takes to travel across a network and to run db transactions.

I tend to design applications with the following questions in mind, in order of priority:

  1. How easy is it to train a new developer to replace me?
  2. How easy will it be to fix a problem when one arises late on a Friday?
  3. How easy is it to change the application?
  4. How easy is it to expand the application?
  5. How will the application perform?

Notice where perf is in that list. Most of the developers I have worked with are really challenged by the breadth of technologies and techniques available to them. Having the applications they maintain and support easy to understand, easy to troubleshoot and easy to modify is more important to me than optimizing for every single drop of perf possible. If it wasn’t, I’d probably be writing in Assembly. Or I’d try to get my job at the t-shirt factory back.

Of course, it’s not a black and white issue. There are trade-offs and balances depending on who the user community is, and the business processes that you are supporting. Further, the old chestnut applies…test early and often. If it turns out that connecting to the db and running a sproc is less of a bottleneck for your app than your choice to use XPath instead of XMLReader, go for it! Optimize away.

That said, I think the Alpha Geeks tend to focus on perf, and premature perf optimizations, to the exclusion of maintanability more often than is healthy. Especially if, like me, you don’t like working late ;). Remember, writing test programs with tight loops, and comparing coding patterns in terms of number of operations is fun for devs. Whether or not it adds business value to solutions is another question entirely.