Jeff blogged last month about Exception-Driven Development. I've been using ELMAH for years and you should too. Having great instrumentation in your app is such a joy. I added ELMAH to NerdDinner and have learned all sorts of things. It's amazing that someone would care to hack a site about Nerds eating dinner, but they try.
This wasn't a hack, but a great bug found in my Nerd Dinner Mobile code that I wouldn't have thought to look for. Here I'm getting a NullReference Exception...why?
Well, here's the code:
private bool UserAgentIs(ControllerContext controllerContext, string userAgentToTest){ return (controllerContext.HttpContext.Request.UserAgent.IndexOf(userAgentToTest, StringComparison.OrdinalIgnoreCase) > 0);}
Of course, I'm breaking the "Law Suggestion of Demeter" with all that digging, but what's the real issue? I'm assuming that the request has a UserAgent string at all! Exactly as the YSOD that ELMAH "tivo'ed" for me above.
So I changed it to this. Yes, I know that this could all be on one line and really baroque, but I find a few more lines to be easier to read.
public bool UserAgentIs(ControllerContext controllerContext, string userAgentToTest){ string UA = controllerContext.HttpContext.Request.UserAgent; if (string.IsNullOrEmpty(UA) == true) return false; return (UA.IndexOf(userAgentToTest, StringComparison.OrdinalIgnoreCase) > 0);}
I likely would have never thought of this bug had I not had logs and instrumentation. A smart user could have told me, or I could have used a Unit Test Generator like Pex, OR I could have just used my head and thought of it first. ;) Assert your assumptions. I didn't, and I assumed, wrongy, UserAgent would be non-null.
if (true == true) doSomething();
public bool UserAgentIs(ControllerContext controllerContext, string userAgentToTest){ string UA = controllerContext.HttpContext.Request.UserAgent ?? String.Empty; return UA.IndexOf(userAgentToTest, StringComparison.OrdinalIgnoreCase) > 0;}
string ua = controllerContext.HTTPContext.Request.UserAgent ?? "";
string ua = controllerContext.HTTPContext.Request.UserAgent ?? "";return ua.StartsWith(userAgentToTest, StringComparison.OrdinalIgnoreCase);
@jeroenh: That's why I would use StartsWith in this case:string ua = controllerContext.HTTPContext.Request.UserAgent ?? "";return ua.StartsWith(userAgentToTest, StringComparison.OrdinalIgnoreCase);
public bool UserAgentIs(ControllerContext controllerContext, string userAgentToTest) { string userAgent= controllerContext.HttpContext.Request.UserAgent; if (string.IsNullOrEmpty(userAgent)) { return false; } return (userAgent.IndexOf(userAgentToTest, StringComparison.OrdinalIgnoreCase) > 0); }
if (Jim.TotallyMissedThePointOf(ScottsPost)) { Scott.KillKittens(); // He's the one with the experience, after all }
Scott used it. Would you not hire him or do you consider him to have poor design skills or not grasp the basics? Just asking...
I have not noticed the things you have or worked with the people you have and so do not realize why such a correlation makes a such a strong impression on you.
I have worked with people in the past that are so passionate and forceful about their style
I'm pretty sure that the KillKittens() method of the Scott class does not provide a parameterless overload and that the proper signature would be Scott.KillKittens(int numKittens).
foreach (Kitten poorTinyDefencelessKitten in AllKittens) { poorTinyDefencelessKitten.Kill(); }
Scott can't get a job working with or for Jim. Ever.
Jim works very hard at making up for some abhorent loss that must have occured close to 20 years ago while he was captain of his High School debate team.
class Kitten : Cat { bool hasRabies() { return false; } }Cat thisCat = new Kitten();...bool thisCatMustDie = thisCat.hasRabies();...if (thisCatMustDie = true) { Kill(thisCat); }
Ads by The Lounge