Alex Rivera | Logout

Code suggestions by Resharper making code less readable?

Asked 2009-01-21T22:52:13.593
36

While trying to get to all green, i got the following suggestion by Resharper.

Original code:

    static public string ToNonNullString(this XmlAttribute attr)
    {
        if (attr != null)
            return attr.Value;
        else
            return string.Empty;
    }

Suggestion: remove redundant 'else' resulting in following:

    static public string ToNonNullString(this XmlAttribute attr)
    {
        if (attr != null)
            return attr.Value;
        return string.Empty;
    }

To me, the suggested version seems less readable than the original. Does Resharper suggestion reflect the definition of good maintainable code?

Edit
Report

2 Answers

3

My coding standard is always use brackets (even if there is only one instruction after if command)
This is requires a bit effort (more typing) but I so often become convinced that it is very worth it!

One of most common bug (and paradoxically difficult to find) is adding additional instruction after if statement and forgetting adding brackets...

So I like what Resharper proposed. Especially when having nested if-statements:

Assume we have this code:

   if (condition1)  {
      instruction1;
   }
   else {
       if (condition2) {
           instruction2;
       }
   }

It can be changed to look like this:

   if (condition1)  {
      instruction1;
   }       
   else if (condition2) {
      instruction2;
   }       

And this is much more readable to me then before.
(It would be also more visible when you have more than 2-level nested statements)

answered 2010-08-25T07:10:16.793
1

Its always debatable when it comes to best practices and coding standards. One of the reason for this is they cannot be enforced very easily using an IDE like Visual Studio. There are tools available like FxCop and StyleCop which can be used to analyse the code for standards. FxCop is used for compiled code analysis and StyleCop is used for source code analysis.

You can configure StyleCop to a minute level as to which formatting you would like to apply to the code. There is an add-in called StyleCop for Resharper which gives suggessions right inside Visual Studio. I had a detailed blog post about the same at http://nileshgule.blogspot.com/2010/10/refactoring-clean-code-using-resharper.html

answered 2010-10-06T11:39:22.477

Your Answer