Why ReSharper Is Worth Using (reason #879)

 

I often email the folks at JetBrains (the makers of ReSharper) letting them know that one of their refactors is wrong.  They often email me back letting me know my about my misunderstanding.  I actually enjoy this.

Today, was a perfect example but I did not get past the ReSharper feedback form when I realized my code was wrong (sadly missed the exchange with them).  I’m busy writing the SEO save for Silicon Valley Code Camp’s new web site (coming by next week hopefully).  In this case, I’m looking for a trackId passed into the sessions.aspx page. If I find it, then I redirect to a more friendly URL.

Here is my code I was going to complain to ReSharper about as incorrectly warning me that my trackId = –1; is redundant.

image

SNAGHTML40a7f61

My mistake is that trackId is always replaced (even with 0) if the QueryString does not evaluate to a valid int.

The corrected code is this:

image

One less bug thanks to ReSharper.

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. Missing “this” between “for” and “non-nullable”…

  2. Since TryParse() use parameter with “out” modifier, the function will be responsible to initialize this variable. for non-nullable type the default will be 0, that’s why when the function is not successful, 0 will be set to trackId there as no value is assigned after it has been initalized.

    Should it be using “ref” instead, that’ll be another story.

  3. Peter Kellner says:

    As Mike C says, I just looked at the disassembled code and right there at the top is
    result = 0;
    So, indeed, it sets it to 0 right away no matter what.

  4. I do not think TryParse sets to 0 on failure, but you could do a simple test to put this issue to bed for us ;-)
    Init to something other than 0, put in crap, see what value is after that.

  5. Peter Kellner says:

    HI Stuart,
    I agree it is 0 before and after. The vagueness was whether TryParse explicitly sets it to 0 on a failure. I gather it must from the resharper warning I got. Agree?

  6. Well, it does get set if it can, but the value of that int is 0 before TryParse is called.
    This is the default value for any non-nullable numeric type.
    Put a break point on the tryparse line and check its value before hand, you should see 0 ;-)

  7. Peter Kellner says:

    Stuart,
    Are you saying trackId IS NOT being set by TryParse?

  8. Actually, the trackId value is 0 before you even call the TryParse method. This is the default value for an int that has not been initialized.

  9. Good example! In addition to this, I’ve used ReSharper to delve into the .NET source code behind Int32.TryParse() to see where trackID will be replaced with 0.

Follow

Get every new post delivered to your Inbox

Join other followers: