Skip to content

Gendarme.Rules.Correctness.MethodCanBeMadeStaticRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

MethodCanBeMadeStaticRule

Assembly: Gendarme.Rules.Correctness
Version: git

Description

This rule checks for methods that do not require anything from the current instance. Those methods can be converted into static methods, which helps a bit with performance (the hidden this parameter can be omitted), and clarifies the API.

Examples

Bad example:

public class Bad {
    private int x, y, z;
    bool Valid (int value)
    {
        // no instance members are used
        return (value > 0);
    }
    public int X {
        get {
            return x;
        }
        set {
            if (!Valid (value)) {
                throw ArgumentException ("X");
            }
            x = value;
        }
    }
    // same for Y and Z
}

Good example:

public class Good {
    private int x, y, z;
    static bool Valid (int value)
    {
        return (value > 0);
    }
    // same X (and Y and Z) as before
}

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally