Gendarme.Rules.BadPractice
From Mono
Gendarme's rules that check for bad practices are located in the Gendarme.Rules.BadPractice.dll assembly. Latest sources are available from anonymous SVN (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.BadPractice/) (tarball (http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.BadPractice.tar.gz?view=tar)).
| Table of contents |
Rules
CheckNewExceptionWithoutThrowingRule
This rule checks for exception objects that are created but not thrown, returned or passed to another method as an argument.
Bad example:
void MissingThrow (object arg) { if (arg == null) new ArgumentNullException ("arg"); DoWork (arg); }
Good examples:
void Throw (object arg) { if (arg == null) throw new ArgumentNullException ("arg"); DoWork (arg); } Exception CreateException () { return new Exception (); }
CheckNewThreadWithoutStartRule
This rule checks for threads that are created but not started, returned or passed to another method as an argument.
Bad example:
void UnusedThread () { Thread thread = new Thread (threadStart); thread.Name = "Thread 1"; }
Good examples:
void Start() { Thread thread = new Thread (threadStart); thread.Name = "Thread 1"; thread.Start (); } Thread InitializeThread () { Thread thread = new Thread (threadStart); thread.Name = "Thread 1"; return thread; }
CloneMethodShouldNotReturnNullRule
This rule check that a Clone() method never returns a null value.
Bad example:
public class MyClass : ICloneable { public object Clone () { MyClass myClass = new MyClass (); // set some internals return null; } }
Good example:
public class MyClass : ICloneable { public object Clone () { MyClass myClass = new MyClass (); // set some internals return myClass; } }
ConstructorShouldNotCallVirtualMethodsRule
This rule warns the developer if any virtual methods are called in the constructor of non-sealed type. The problem is that the exact method that will be executed is not known before runtime. Also such virtual calls may be executed before the constructor of deriving type is called, thus making possible mistakes.
Bad example:
class A { public A () { this.DoSomething (); } protected virtual void DoSomething () { } } class B : A { private int x; public B () { x = 10; } protected override void DoSomething () { Console.WriteLine (x); } } B b = new B (); // outputs 0 because B constructor hasn't been called yet
Good example:
class A { public void Run () { this.DoSomething (); } protected virtual void DoSomething () { } } class B : A { private int x; public B () { x = 10; } protected override void DoSomething () { Console.WriteLine (x); } } B b = new B (); b.Run (); // outputs 10 as intended
EqualsShouldHandleNullArgRule
This rule ensures that methods Equals(object) returns false when the object parameter is null.
Bad example:
public bool Equals (object obj) { // this would throw a NullReferenceException instead of returning false return ToString ().Equals (obj.ToString ()); }
Good example:
public override bool Equals (object obj) { if (obj == null) return false; return ToString ().Equals (obj.ToString ()); }
GetEntryAssemblyMayReturnNullRule
Calling Assembly.GetEntryAssembly() outside the root (main) application domain will always return null.
This may become a problem inside libraries which can be used, for example, inside ASP.NET applications.
Bad example:
// this will throw a NullReferenceException from an ASP.NET page Response.WriteLine (Assembly.GetEntryAssembly ().CodeBase);
Good example:
public class MainClass { static void Main () { Console.WriteLine (Assembly.GetEntryAssembly ().CodeBase); } }
ToStringReturnsNullRule
This rule is used for ensure that no overriden ToString() method returns a null value.
This makes the value more useful in debugging.
Bad example:
public override string ToString () { if (count == 0) return null; else return count.ToString (); }
Good example:
public override string ToString () { return count.ToString (); }
Feedback
Please report any documentation errors, typos or suggestions to the Gendarme Google Group (http://groups.google.com/group/gendarme). Thanks!

Powered by MediaWiki