Gendarme.Rules.Maintainability

Gendarme's maintainability rules are located in the Gendarme.Rules.Maintainability.dll assembly. Latest sources are available from anonymous SVN (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Maintainability/).

Rules

AvoidAlwaysNullFieldRule

A type has a private field whose value is always null.

Bad example:

internal sealed class Bad {
    private List<int> values;
    
    public List<int> Values {
        get {
            return values;
        }
    }
}

Good example:

internal sealed class Good {
    private List<int> values = new List<int>();
    
    public List<int> Values {
        get {
            return values;
        }
    }
}

Notes

  • This rule is available since Gendarme 2.4

AvoidComplexMethodsRule

This rule compute the cyclomatic complexity (CC) for every method and report any method with a CC over 25 (this limit is configurable). Large CC value often indicate complex code that will be hard to understand and maintain. It's likely that breaking down the method into several methods would help readability. This rule won't report any defects on code generated by the compiler or by tools.

Notes

  • This rule is available since Gendarme 2.0

AvoidDeepInheritanceTreeRule

This rule checks every type to see how deep is it's inheritance tree inside the analyzed assembly set. By default (configurable) the rule will warn if the depth is greater than four levels. Optionally (configurable) it can include any resolvable assembly (out of the analyzed assembly set) in the check.

Notes

  • This rule is available since Gendarme 2.0

AvoidLackOfCohesionOfMethodsRule

This rule checks every type to check for lack of cohesion between the fields and the methods. Low cohesion is often a sign that a type is doing too many, different an unrelated things. The cohesion score is given for each defect (higher is better) and the success threshold can be configured.

Notes

  • This rule is available since Gendarme 2.0

AvoidUnnecessarySpecializationRule

This rule checks methods for over specialized parameters - i.e. parameter types that are unnecessarily specialized with respect to what the method needs to do its job. This often leads to reduced reusability potential of the method. The rule will suggest the minimal type, or interface, required for the method to work.

Bad example:

public class DefaultEqualityComparer : IEqualityComparer {
    public int GetHashCode (object obj)
    {
        return o.GetHashCode ();
    }
}
 
public int Bad (DefaultEqualityComparer ec, object o)
{
    return ec.GetHashCode (o);
}

Good example:

public class DefaultEqualityComparer : IEqualityComparer {
    public int GetHashCode (object obj)
    {
        return o.GetHashCode ();
    }
}
 
public int Good (IEqualityComparer ec, object o)
{
    return ec.GetHashCode (o);
}

Notes

  • This rule is available since Gendarme 2.0

ConsiderUsingStopwatchRule

This rule checks methods for cases where a System.Diagnostics.Stopwatch could be used instead of using System.DateTime to compute the time required for an action. This does not affect execution nor (much) performance but it improves source code readability. This rule only applies to assemblies compiled with the .NET framework version 2.0 (or later).

Bad example:

public TimeSpan DoLongOperation ()
{
    DateTime start = DateTime.Now;
    DownloadNewOpenSuseDvdIso ();
    return DateTime.Now - start;
}

Good example:

public TimeSpan DoLongOperation ()
{
    Stopwatch watch = Stopwatch.StartNew ();
    DownloadNewOpenSuseDvdIso ();
    return watch.Elapsed;
}

Notes

  • This rule is available since Gendarme 2.0

PreferStringIsNullOrEmptyRule

This rule checks methods for cases where String.IsNullOrEmpty could be used instead of doing separate null and length checks. This does not affect execution nor (much) performance but it improves source code readability. This rule only applies to assemblies compiled with the .NET framework version 2.0 (or later).

Bad example:

public bool SendMessage (string message)
{
    if ((message == null) || (message.Length == 0)) {
        return false;
    }
    return SendMessage (Encode (message));
}

Good example:

public bool SendMessage (string message)
{
    if (String.IsNullOrEmpty (message)) {
        return false;
    }
    return SendMessage (Encode (message));
}

Notes

  • This rule is available since Gendarme 2.0

Feedback

Please report any documentation errors, typos or suggestions to the Gendarme Google Group (http://groups.google.com/group/gendarme). Thanks!