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:
- Attribute “rel” in my Where
- Attribute “href” in my Select
- 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).