Gendarme.Rules.Performance.AvoidRepetitiveCallsToPropertiesRule Class
The rule warn if virtual, or unlikely to be inline-able, property getters are called several times by a method. In most cases repetitive calls simply requires more time without any gains since the result will always be identical. You should ignore the reported defects if a different value is expected each time the property is called (e.g. calling DateTime.Now).

See Also: AvoidRepetitiveCallsToPropertiesRule Members

Syntax

[Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine, Gendarme.Framework, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null))]
[Gendarme.Framework.Problem("This method calls several times into the same properties. This is expensive for virtual properties or when the property cannot be inlined.")]
[Gendarme.Framework.Solution("Unless a different value is expected from each call, refactor your code to avoid the multiple calls by caching the returned value.")]
public class AvoidRepetitiveCallsToPropertiesRule : Gendarme.Framework.Rule, Gendarme.Framework.IMethodRule

Remarks

This rule is available since Gendarme 2.8

Example

Bad example:

Example

            private int Report (IList list)
            {
            	if (list.Count > 1) {
            		DisplayList (list);
            	}
            	Console.WriteLine ("# items: {0}", list.Count);
            	return list.Count;
            }
            

Example

Good example:

Example

            private int Report (IList list)
            {
            	int count = list.Count;
            	if (count > 1) {
            		DisplayList (list);
            	}
            	Console.WriteLine ("# items: {0}", count);
            	return count;
            }
            

Requirements

Namespace: Gendarme.Rules.Performance
Assembly: Gendarme.Rules.Performance (in Gendarme.Rules.Performance.dll)
Assembly Versions: 2.8.0.0