Sometimes we developers do things the hard way for no better reason that not knowing that a better way is there.
Here’s an example, posted with permission of the author:
This code works - it’s doing nothing more than setting up an object based on information available in an XElement.
Problem is, the code works too hard. What the author didn’t know at the time is this: There are predefined explicit
casts on XAttribute that take care of the null checks for you.
For example, instead of
You may instead write this:
Using Reflector, we can look at the implementation of the string conversion and see how it works:
This means that our original large block of code can be economically rewritten like this:
This pattern repeats itself throughout the .NET framework - features are waiting to be discovered, features that can
save you a lot of work, if you could only find them.
I guess that’s the point - the discoverability of this kind of thing is poor.
Comments
blog comments powered by Disqus