﻿<?xml version="1.0" encoding="utf-8"?><Type Name="AvoidMethodsWithSideEffectsInConditionalCodeRule" FullName="Gendarme.Rules.Correctness.AvoidMethodsWithSideEffectsInConditionalCodeRule"><TypeSignature Language="C#" Value="public class AvoidMethodsWithSideEffectsInConditionalCodeRule : Gendarme.Framework.Rule, Gendarme.Framework.IMethodRule" /><TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit AvoidMethodsWithSideEffectsInConditionalCodeRule extends Gendarme.Framework.Rule implements class Gendarme.Framework.IMethodRule, class Gendarme.Framework.IRule" /><AssemblyInfo><AssemblyName>Gendarme.Rules.Correctness</AssemblyName><AssemblyVersion>3.10.0.0</AssemblyVersion></AssemblyInfo><Base><BaseTypeName>Gendarme.Framework.Rule</BaseTypeName></Base><Interfaces><Interface><InterfaceName>Gendarme.Framework.IMethodRule</InterfaceName></Interface></Interfaces><Attributes><Attribute><AttributeName>Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine))</AttributeName></Attribute><Attribute><AttributeName>Gendarme.Framework.Problem("A conditionally compiled method is being called, but uses the result of a non-pure method as an argument.")</AttributeName></Attribute><Attribute><AttributeName>Gendarme.Framework.Solution("If the non-pure method has no visible side effects then decorate it with PureAttribute. If it does have side effects then rewrite the code so that the method is not called.")</AttributeName></Attribute></Attributes><Docs><summary>
             A number of System methods are conditionally compiled on #defines. 
             For example, System.Diagnostics.Trace::Assert is a no-op if TRACE
             is not defined and System.Diagnostics.Debug::Write is a no-op if DEBUG
             is not defined.
            
             When calling a conditionally compiled method care should be taken to
             avoid executing code which has visible side effects. The reason is that 
             the state of the program should generally not depend on the value of 
             a define. If it does it is all too easy to create code which, for example, 
             works in DEBUG but fails or behaves differently in release.
            
             Methods which have no visible side effects are termed pure methods. 
             Certain System methods and delegates (such as System.String and 
             System.Predicate&lt;T&gt;) are assumed to be pure. If you want to 
             use a method which is not known to be pure in a conditionally compiled 
             method call then you'll need to decorate it with PureAttribute (see the
             examples below).
            
             Note that it is OK to disable this rule for defects where the define will
             always be set (now and in the future), if the program's state really
             should be affected by the define (e.g. a LINUX define), or the impure
             method should be decorated with PureAttribute but it is not under
             your control.
             </summary><remarks>To be added.</remarks><example>
             Bad example:
             <code>
             using System.Collections.Generic;
             using System.Linq;
             using System.Runtime.Diagnostics;
            
             internal sealed class Worker {
             	private Dictionary&lt;string, Job&gt; jobs;
            
             	private bool IsValid (Job job)
             	{
             		return job != null &amp;&amp; job.PartId &gt; 0;
             	}
            
             	private void Process (string name, Job job)
             	{
             		// This is OK because IsValid has no side effects (although the rule
             		// won't know that unless we decorate IsValid with PureAttribute).
             		Trace.Assert (IsValid (job), job + " is not valid");
            
             		// This is potentially very bad: if release builds now (or in the future)
             		// don't define TRACE the job won't be removed from our list in release.
             		Trace.Assert (jobs.Remove (job), "couldn't find job " + name);
            
             		job.Execute ();
             	}
             }
             </code></example><example>
             Good example:
             <code>
             using System.Collections.Generic;
             using System.Linq;
             using System.Runtime.Diagnostics;
            
             // note: in FX4 (and later) this attribute is defined in System.Runtime.Diagnostics.Contracts
             [Serializable]
             [AttributeUsage (AttributeTargets.Method | AttributeTargets.Delegate, AllowMultiple = false)]
             public sealed class PureAttribute : Attribute {
             }
             	
             internal sealed class Worker {
             	private Dictionary&lt;string, Job&gt; jobs;
            
             	[Pure]
             	private bool IsValid (Job job)
             	{
             		return job != null &amp;&amp; job.PartId &gt; 0;
             	}
            
             	private void Process (string name, Job job)
             	{
             		// IsValid is decorated with PureAttribute so the rule won't complain
             		// when we use it within Trace.Assert.
             		Trace.Assert (IsValid (job), job + " is not valid");
            
             		bool removed = jobs.Remove (job);
             		Trace.Assert (removed, "couldn't find job " + name);
            
             		job.Execute ();
             	}
             }
             </code></example></Docs><Members><Member MemberName=".ctor"><MemberSignature Language="C#" Value="public AvoidMethodsWithSideEffectsInConditionalCodeRule ();" /><MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" /><MemberType>Constructor</MemberType><AssemblyInfo><AssemblyVersion>3.10.0.0</AssemblyVersion></AssemblyInfo><Parameters /><Docs><summary>To be added.</summary><remarks>To be added.</remarks></Docs></Member><Member MemberName="CheckMethod"><MemberSignature Language="C#" Value="public Gendarme.Framework.RuleResult CheckMethod (Mono.Cecil.MethodDefinition method);" /><MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance valuetype Gendarme.Framework.RuleResult CheckMethod(class Mono.Cecil.MethodDefinition method) cil managed" /><MemberType>Method</MemberType><AssemblyInfo><AssemblyVersion>3.10.0.0</AssemblyVersion></AssemblyInfo><ReturnValue><ReturnType>Gendarme.Framework.RuleResult</ReturnType></ReturnValue><Parameters><Parameter Name="method" Type="Mono.Cecil.MethodDefinition" /></Parameters><Docs><param name="method">To be added.</param><summary>To be added.</summary><returns>To be added.</returns><remarks>To be added.</remarks></Docs></Member></Members></Type>