Using the 'Condition or not Switch' formula

Coordinator
Apr 14, 2010 at 4:36 PM
Edited Apr 17, 2010 at 3:31 PM

This is a very cool formula that I learned long ago from an old friend of mine, Darius Bacon.

	return !Switch || (a == b);

where "(a == b)" is the condition or expression; and, "switch" is an on/off switch defined by an outside factor, such as the user configuration.

If the switch is enabled, the statement will evaluate the condition and return true (if A equals B) or false (if A and B are different.) And if the switch is disabled, the statement always return true, because the condition is not relevant in this case. If the formula looks very cryptic. Stop reading and stare at it for a while until it you fully understand the logic.

(Just kidding...)

It took me a while to fully understand it. The logic behind this is very simple. Consider the following snippet:

	public bool Switch {get;set;}

	// ...

	public bool Evaluate(int a, int b)
	{
		// if switch is enabled, we want to compare a and b, and return true if they are the same or false if they are different
		// if disabled, return true.  we don't want to run this rule.

		if (Switch)
		{
			if (a == b)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
	}

Of course, the above statement could be shorten up using the ?: Operator Condition:

	return Switch ? (a == b) : true;

Most people would stop here because the above statement is very clean and easy to understand.  However for a rare few others, this can be further reduced to a single expression, thus reducing the code execution by a small fraction.

	return !Switch || (a == b);

Here is a C# sample and its CIL code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConditionNotSwitch
{
	class Program
	{
		static bool Switch = false;

		static void Main(string[] args)
		{
			OperatorCondition(1,1);
			ConditionNotSwitch(1,1);
		}

		private static bool ConditionNotSwitch(int a, int b)
		{
			return !Switch || (a == b);
		}

		private static bool OperatorCondition(int a, int b)
		{
			return Switch ? (a == b) : true;
		}
	}
}

 Here is the CIL code for ConditionNotSwitch and OperatorCondition methods generated by the ILDASM tool.

.method private hidebysig static bool  ConditionNotSwitch(int32 a,
                                                          int32 b) cil managed
{
  // Code size       20 (0x14)
  .maxstack  2
  .locals init ([0] bool CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldsfld     bool ConditionNotSwitch.Program::Switch
  IL_0006:  brfalse.s  IL_000e
  IL_0008:  ldarg.0
  IL_0009:  ldarg.1
  IL_000a:  ceq
  IL_000c:  br.s       IL_000f
  IL_000e:  ldc.i4.1
  IL_000f:  stloc.0
  IL_0010:  br.s       IL_0012
  IL_0012:  ldloc.0
  IL_0013:  ret
} // end of method Program::ConditionNotSwitch

And here is the CIL code for OperatorCondition:

.method private hidebysig static bool  OperatorCondition(int32 a,
                                                         int32 b) cil managed
{
  // Code size       20 (0x14)
  .maxstack  3
  .locals init ([0] bool CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldsfld     bool ConditionNotSwitch.Program::Switch
  IL_0006:  brtrue.s   IL_000b
  IL_0008:  ldc.i4.1
  IL_0009:  br.s       IL_000f
  IL_000b:  ldarg.0
  IL_000c:  ldarg.1
  IL_000d:  ceq
  IL_000f:  stloc.0
  IL_0010:  br.s       IL_0012
  IL_0012:  ldloc.0
  IL_0013:  ret
} // end of method Program::OperatorCondition