LINQ is all about being smart

The competent programmer is fully aware of the limited size of his own skull. He therefore approaches his task with full humility, and avoids clever tricks like the plague.

I wanted to be smart. I tried to use LINQ to XML for parsing an Atom feed. Here’s what I’ve got to read a link:

var urlElement = item.Elements(xmlns + "link")
                                .Where(link.Attribute("rel").Value == "alternate")
                                .Select(link => link.Attribute("href").Value)

Do you see the issues? 3 Nullpointers:

  1. Attribute “rel” in my Where
  2. Attribute “href” in my Select
  3. First() –> What if I have nothing to get the first of?

Once again this underlines what Edsger W. Dijkstra said in the quote written above. You try to be smart by using the latest stuff, coolest tools, etc. But the limitations are never described, you’ll only experience them while debugging.

Another lesson learned.

Small update: Those who are using Resharper will get notified about the first 2 nullpointers, but not about the third. On the other hand, solving that in the where clause doesn’t fix the value analysis (seems to be hard in lambda’s).

One thought on “LINQ is all about being smart”

Comments are closed.