Coding for Food

Saturday, April 28, 2012

Behavior flexability of IEnumerable<T>

One of the ways to de-couple your code from specific types, what I'll call hard types, is to use the IEnumerable. I'll call this a soft type, and it can be used many places you might otherwise find a read-only version of IList. Rather than pass around an IList, which is an object with state and memory, if you can pass around an IEnumerable, you should.

I want to read strings from a file, or records from a database table. Rather that loop through and create a list of strings/records, I’ll just return an IEnumerable.

First, create a text file, and save it UTF-8 encoded.

Copy this code (sans line numbers)

1   using System;
2 usingSystem.Collections.Generic;
3 usingSystem.Text;
4 using System.IO;
5
6 namespace DonCode
7 {
8 public delegate IEnumerable<string> GetStrings(StreamReader sr);
9 public class Program
10 {
11 static void Main(string[] args)
12 {
13 Program p = new Program();
14 using (StreamReader sr = new StreamReader("file.txt", Encoding.UTF8)) {
15 GetStrings ReadFileStrings = null;
16 ReadFileStrings = p.ReadFileList;
17 // ReadFileStrings = p.ReadFileNoList;
18 IEnumerable<string> items = ReadFileStrings(sr);
19 foreach (string line in items) {
20 Console.WriteLine(line);
21 }
22 }
23 Console.ReadLine();
24 }
25 public IEnumerable<string> ReadFileNoList(StreamReader r)
26 {
27 string line = r.ReadLine();
28 while (!String.IsNullOrEmpty(line)) {
29 yield return line;
30 line = r.ReadLine();
31 }
32 }
33 public IEnumerable<string> ReadFileList(StreamReader r)
34 {
35 IList<string> list = new List<string>();
36 string line = r.ReadLine();
37 while (!String.IsNullOrEmpty(line)) {
38 list.Add(line);
39 line = r.ReadLine();
40 }
41 return list;
42 }
43 }
44 }


To see this work, put a break point here:


15                   GetStrings ReadFileStrings = null;



Run your code in the debugger (F5) and step through it using F11.



Switch comments:




16                   //ReadFileStrings = p.ReadFileList;
17 ReadFileStrings = p.ReadFileNoList;



Run the code in the debugger (F5) and step through it with F11.



The results are the same but the behavior is different. We went from creating a list and running a pair of loops, to running a single loop, and creating no list. The client code was not changed, nor was the contract altered. By using IEnumerable, we were able to be more flexible in our implementation but still deliver on our contract.

Tuesday, June 14, 2011

Coding with code snippets

My main coding tool is Visual Studio. That product has support for what they call "Code Snippets". Code Snippets are typically small chunks of code that you use frequently, that you can easily insert in to your code. It's like clip-board on steroids.


VS ships with a number of code snippets. They typically live here

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#.



Code snippets are straight XML files with a .snippet extension. Because that extension is registered with VS, clicking on such a file will open it up in VS. Notepad works fine too. We'll look at one of the shipping snippets, and use it for the bases of our own custom snippet. We'll look at prop.snippet:


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Titlegt;prop</Title>
<Shortcut>prop</Shortcut>
<Description>Code snippet for an automatically implemented property
Language Version: C# 3.0 or higher</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


A couple of things to note here. The shortcut is the "keyword" you can type in order to insert the code snippet in to your code. Open a C# file, and within a class, where you would normally insert a property, type prop, then hit the tab key twice. "prop" expands out to "public int MyPropery { get; set; }" Very cool. 28 keystrokes saved. Note that there are two fields that are "highlighted". Tabbing toggles between them. There is a replacement functionality that is available when you first insert a snippet, and the items that are "highlighted" are items that can be replaced. In this case, the author thinks that you'll probably want to replace the property type and the property name with your own, so you can tab from one to another and type in the replacement. In the code snippet, those replacements are in the Declaration section. Here is one:


<id>type</id>
<tooltip>Property type</tooltip>
<default>int</default>


This explains itself. In the code section, all instances of "$type$" will be replaced with "int", and be highlighted for modification. In this simple snippet, there is only one instance of each replacement, but in the more complex one we'll write, the same replacement will appear multiple times, and changing one with change all.

The code section looks like this:

<code language="csharp"><!--[CDATA[public $type$ $property$ { get; set; }$end$]]--></code>


The "$end$" is unknown to me, but perhaps is a built-in replacement for line-feed.

The first useful snippet I'll create is the the Assert that I use on non-null function arguments. I used code contracts once, but to fully take advantage of them you need to use a VS extension. Not an option for us VS Express hackers, so I've dropped back to using:

Debug.Assert(null != parameter);


Here is my implementation:


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>assertnotnull</Title>
<Shortcut>asnot</Shortcut>
<Description>Code snippet to validate not-null parameters</Description>
<Author>pizza-code</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>parameter</ID>
<ToolTip>non-null Parameter</ToolTip>
<Default>argument</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[Debug.Assert(null != $parameter$);$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Save this file as "AssertNotNull.snippet" to your a "Code Snippets" directory in your Documents folder. Mine is:

C:\Users\codecore\Documents\Code Snippets\AssertNotNull.snippet


If notepad is your editor, additional work may be required to remove the ".txt" extension.
Now that you've written your new code snippet, or copied mine, how do you let VS know about it?
Menu->Tools->Code Snippet Manager

The "Code Snippets Manager" dialog contains entries for the code snippet locations that it knows about. If Language is "Visual C#", I see a "Refactoring" and a "Visual C#" entry. Use the "Add" button, and find the directory that you saved your new custom snippet file in. You have import individual snippet files directly using the "import" button. VS now knows about your snippets. The directory of snippets are loaded when VS starts, or when you add the directory to it's knowledge. Adding new snippet files to your snippet directory will not be known to VS. A VS restart, or remove-add will be required to pick-up the new snippets.


Should you be running an edition of VS that supports extension, there is a free snippet tool available, mentioned by Robert Greene in this excellent Channel 9 video: http://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Visual-Studio-Toolbox-Creating-and-Using-Code-Snippets


Here are a couple more of my favorite snippets:
NotifyPropery.snippet aka "propf"


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propf</Title>
<Shortcut>propf</Shortcut>
<Description>Code snippet to implemented INotifyPropertyChanged property</Description>
<Author>pizza-code</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>string</Default>
</Literal>
<Literal>
<ID>back</ID>
<ToolTip>Backing Property name</ToolTip>
<Default>back</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Public Property name</ToolTip>
<Default>Property</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[
protected $type$ $back$;
public $type$ $property$ {
get { return this.$back$; }
private set {
if (value != this.$back$) {
this.$back$ = value;
this.FirePropertyChanged("$property$");
}
}
}$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Finally, here is FirePropertyChanged.snippet aka "fire"


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>fire</Title>
<Shortcut>fire</Shortcut>
<Description>Code snippet for implementing INotifyPropertyChanged interface</Description>
<Author>pizza-code</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged(string propertyName) {
if (null != this.PropertyChanged) this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Thursday, October 7, 2010

Fun With MEF

I've been playing with MEF. I wanted to use it in the simplest way I could.

For those who don't know, MEF is a component that you can use to manage objects in your application. It's in the universe of "Dependency Injection". It has some features that make it really good for "Plug-In" architectures, but I'll be using it for object lifetime management. There is much less "noise" in using MEF, than the other (SimpleServiceLocator) DI that I have used. In many ways DI is like the Factory pattern on steroids. In any case, you decorate the class that you want to provide as a component with the Export attribute, and you mark the properties of your dependent classes with the Import attribute. MEF scans the assemblies in your directory for all the Exports, and scans all the Imports, and does a quick fix-up, matching Exports to Imports. If I need a IFileSystem object in my logger, I "automagically" get one. I don't need to new one up, or MyFileSystemFactory.Create(), or first register a constructor or factory with a static ServiceLocator. See Glenn's article for an in-depth tour.


I ran in to the problem that the Imports in a client assembly were not being satisfied, while the imports in the assembly where my MEF host class is defined were satisfied. Here is the ExtensibilityHost class that is in the Heat.dll host assembly:



//Heat.cs
namespace Heat
{
public static class Constants {
public const string NullOrEmptyString = "null or empty string";
public const string NullObject = "null object";
public const string EmptyString = "empty string";
public const string RangeError = "range error";
public const string SequenceError = "sequence error";
public const string ArrayLengthError = "array length error";
}
}





//HeatExtensibility.cs

using System;
using System.IO;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.Diagnostics.Contracts;

using Heat;
namespace Heat.Extensibility
{
public interface IExtensibilityHost : IDisposable
{
void Compose();
void SatisfyImports(object o);
}
public static class ExtensibilityHostFactory
{
private static IExtensibilityHost host = null;
public static IExtensibilityHost Get()
{
if (null == host) host = new ExtensibiltyHost();
return host;
}
private class ExtensibiltyHost : IExtensibilityHost
{
private enum Sequence
{
Initial = 0,
Compose = 1,
SatisfyImports = 2
}
private Sequence sequence = Sequence.Initial;
private CompositionContainer compositionContainer;
private AssemblyCatalog assemblyCatalog;
private DirectoryCatalog directoryCatalog;
public void Compose()
{
Contract.Requires(this.sequence == Sequence.Initial, Constants.SequenceError);
this.assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
this.directoryCatalog = new DirectoryCatalog((new FileInfo(Assembly.GetExecutingAssembly().Location)).DirectoryName);
this.compositionContainer = new CompositionContainer(this.directoryCatalog);
this.compositionContainer.ComposeParts(this);
this.sequence = Sequence.Compose;
}
public void SatisfyImports(object o)
{
Contract.Requires(null != o, Constants.NullObject);
Contract.Requires(this.sequence != Sequence.Initial, Constants.SequenceError);
this.compositionContainer.SatisfyImportsOnce(o);
this.sequence = Sequence.SatisfyImports;
}
virtual public string AssemblyInfo()
{
return Assembly.GetExecutingAssembly().ToString();
}
virtual public void Dispose()
{
Contract.Requires(this.sequence != Sequence.Initial, Constants.SequenceError);
this.assemblyCatalog.Dispose();
this.directoryCatalog.Dispose();
this.compositionContainer.Dispose();
this.sequence = Sequence.Initial;
}
}
}
}




Here is the exported object. When a client assembly needs an IFileSystem object, MEF will find it (Heat.dll), instatiate only one (singleton), and deliver it to the instance of the class in need of it.




//HeatFileSystem.cs
using System.IO;
using Heat.Contracts;// IFileSystem
namespace Heat.Utility
{
[Export(typeof(IFileSystem))]
public class FileSystem : IFileSystem
{…}
}



Fortunatly, I work near The Guy who introduced me to MEF, and he steared me in the right direction. Thanks Glenn! It turns out IExtensibilityHost.Compose() will resolve the the dependencies in the Host assembly, it does not do this for the client assembly. In order satisfy those, I'll need to call another function, passing in an instance of the object that contains the imports. IExtesabilityHost.SatisfyImports(this) ends up calling the SatisfyImportsOnce function of the MEF composition container.

You'll know that this happens, because the TEST FileSystemTest.FileSystem() passes.

The UnitTestHeatFileSystem.cs file contributes to the client assembly UnitTest.dll.


//UnitTestHeatFileSystem.cs
using Heat.Extensibility;
using Heat.Contracts;//IFileSystem
using Heat.Tests.UnitTest;//ITest
using NUnit.Framework;
namespace Heat.Test.FileSystem
{
[TestFixture]
public class FileSystemTest : ITest
{
private IExtensibilityHost extensibilityHost = null;
[Import(typeof(IFileSystem))]
private IFileSystem filesystem;
[SetUp]
public void Setup()
{
this.extensibilityHost = ExtensibilityHostFactory.Get();
this.extensibilityHost.Compose();
this.extensibilityHost.SatisfyImports(this);
}
[TearDown]
public void TearDown() { this.extensibilityHost.Dispose(); }
[Test]
public void Sanity() { Assert.IsTrue(true); }
[Test]
public void FileSystem()
{
Assert.IsTrue(null != this.filesystem);
}
:
}
}

Thursday, April 8, 2010

Fun With Enumerators

Enumerators allow you to iterate over a collection of objects. The enumerators in C# are not you father's C++ iterators. This is a new animal that allows you to get a sequence of objects from any collection using the pull-model, even collections that promote recursive access.

Consider the challenge of in-order element retrieval from a binary-tree. This is a recursive problem, which lends itself to a push-model, not a pull-model. There is no traditional for loop that returns each element in increasing order. One would need to push the elements to a list first, and then walk the list.

Enumerators solve this problem with some compiler-magic. See Eric Lippert's blog on details of this.

Problem Statement: We need a generic collection that supports fast searching, and that we can iterate in two directions (increasing, and decreasing).

Solution: A generic Binary Tree. Let's code it up.


public class BinaryTree<T> where T : IComparable


We need it to be IComparable so that we can insert it in the correct place.
It makes it easier to find also. Later we'll add the ability to return a
sequence that meets a LessThanEquals clause.

{
public enum Order { Forward, Reverse } // the transversal order

private class Node<X> // the object that our binary tree uses
{
public Node(X value) { this.Value = value; }
public X Value;
public Node<X> Left, Right;
}

private Node<T> root; // the root node of our binary tree


A useful Binary Tree needs to have a good insert function. We'll use
the fact that the items support IComparable in order to put them in
the right place.


public void Add(T value)
{
if (null == this.root) this.root = new Node<T>(value);
else Add(Node<T> node, T value)
}
private static void Add(Node<T> node, T value) // we code this using
{ // the iterative style
Node<T> current = node; // rather than the
bool done = false; // recursive style
while (!done)
switch (current.Value.CompareTo(value)) {
case 1: // Left = less-than
if (null != current.Left) current = current.Left; // walk left
else { current.Left = new Node<T>(value); done = true; }
break;
case -1: // Right = greater-than
if (null != current.Right) current = current.Right; // walk right
else { current.Right = new Node<T>(value); done = true; }
break;
case 0: done = true; break; // we don't insert duplicates
}
}


A fast Find function is the hallmark of a useful Binary Tree collection.

public bool Find(T value)
{
bool found = false; // again, we'll do this iteratively
Node<T> current = this.root;
while ((null != current) && (!found))
switch (current.Value.CompareTo(value)) {
case 1: current = current.Left; break; // walk left
case -1: current = current.Right; break; // walk right
case 0: found = true; break; // item is found
}
return found;
}


For techniques on balancing binary trees, see algorithms + data structures = programs by Nicolas Wirth.

The function to return a sequence is recursive. We will use the yield return
statement to return the current item. You'll note that we appear to suspend
our function at each yield return statement, and then resume at that point when
the next value is requested. The "automatics" in the function are actually stored
between calls, giving IEnumerable functions the flavor of a state-machine. In fact,
that characteristic is exploited by some to create support for green-threads and
micro-threading applications (ala computer games). There are many cool patterns
that are available to state-machine coders that we'll explore in later posts.

public IEnumerable<T> GetAll(Order order)
{
if (null != this.root)
foreach (T t in getAll(order, this.root))
yield return t;
}

private static IEnumerable<T> getAll(Order order, Node<T> node)
{
Node<T> first, last;
// Specify the order to retrieve the items
if (Order.Forward == order) { // tuples would be sweet here.
first = node.Left; last = node.Right;
} else { // Reverse order
first = node.Right; last = node.Left;
}
// FIRST
if (null != first)
foreach (T t in getAll(order, first)) yield return t;
// CURRENT
yield return node.Value;
// LAST
if (null != last)
foreach (T t in getAll(order, last)) yield return t;
// implicit yield break;
}


I'll come back and modify that last function with an optional depth parameter once C# 4 ships. We'll code one more function to round it out.

public IEnumerable<T> LessThanEqual(Order order, T value)
{
// Only return a sequence if there is something in the tree
if (null != this.root)
foreach (T t in lessThanEqual(order, this.root, value))
yield return t;
}

private static IEnumerable<T> lessThanEqual(Order order, Node<T> node, T value)
{
switch (node.Value.CompareTo(value)) {
case -1:
// FIRST
if (Order.Forward == order) {
// value is greater (to our right)
// so output everything to our left
if (null != node.Left)
foreach (T t in getAll(order, node.Left))
yield return t;
} else {
// value is greater (to our right)
// so output those items to our right
// that meet the criteria
if (null != node.Right)
foreach (T t in lessThanEqual(order, node.Right, value))
yield return t;
}
// CURRENT
yield return node.Value;
// LAST
if (Order.Forward == order) {
// value is greater (to our right)
// so output those items to our right
// that meet the criteria
if (null != node.Right)
foreach (T t in lessThanEqual(order, node.Right, value))
yield return t;
} else {
// value is greater (to our right)
// so output everything to our left
if (null != node.Left)
foreach (T t in getAll(order, node.Left))
yield return t;
}
break;
case 1:
if (null != node.Left)
// value is lesser (to our left)
// so output those items to our left
// that meet the criteria
foreach (T t in lessThanEqual(order, node.Left, value))
yield return t;
break;
case 0:
if (Order.Forward == order) {
// FIRST
if (null != node.Left)
foreach (T t in getAll(order, node.Left))
yield return t;
// CURRENT
yield return node.Value;
} else {
// CURRENT
yield return node.Value;
// LAST
if (null != node.Left)
foreach (T t in getAll(order, node.Left))
yield return t;
}
break;
// implicit yield break;
}

}



Let's test it:


static void Main(sting[] args)
{
BinaryTree<int> bt = new BinaryTree();
bt.Add(17);
bt.Add(6);
bt.Add(3);
bt.Add(35);
bt.Add(14);
bt.Add(26);
bt.Add(44);
bt.Add(11);
bt.Add(9);
bt.Add(10);
bt.Add(11); // duplicate test
foreach (int n in bt.LessThanEqual(BinaryTree<int>.Order.Reverse,16))
Console.WriteLine("{0} ", n);
Console.WriteLine("Finding 35 {0}", bt.Find(35));
Console.WriteLine("Finding 112 {0}", bt.Find(112));
}

OUTPUT

14
11
10
9
6
3
Finding 35 True
Finding 112 False






There it is. Enumerating through a collection recursively using the pull model.

Wednesday, March 17, 2010

Animation looping using delegates

I have created an small XNA application that renders a animated sprite that I load from a sprite sheet. A sprite sheet is a image file that has a series of frames or pictures of an object that is different from frame to frame. Imagine a series of frames that show a door opening. Running the animation the other way would display the door closing. Running the animation forward and then backward, again and again, would show the door opening, then closing, repeating. Finally one could cycle the animation, and start over, and run it again. There are at least 5 ways to cycle this single linear animation. I'll list them:

1. Foward, then stop.
2. Forward, then forward again, looping.
3. Reverse, then stop.
4. Reverse, then reverse again, looping.
5. Forward, then reverse, looping.
The first attempt to code this might look like this:


class animation : IUpdatable
{
private AnimationEdge eventType;
private enum Direction { forward, reverse }
private Direction direction = Direction.forward;
private AnimationMode mode = AnimationMode.stop;
private int current;
private Frame[] frames;
public enum AnimationEdge { begin, end, cycle }
public enum AnimationMode {
forward_cycle = 1,
reverse_cycle = 2,
forward_to_ending,
reverse_to_beginning,
forward_reverse_cycle,
stop
}
:
public void Update()
{
switch (this.mode) {
case AnimationMode.forward_cycle:
this.current++;
if (this.frames.Length <= this.current) {
this.current = 0;
this.InvokeAnimationEdgeEvent(AnimationEdge.cycle);
}
break;
case AnimationMode.forward_to_ending:
this.current++;
if (this.frames.Length <= this.current) {
this.current = this.frames.Length - 1;
this.InvokeAnimationEdgeEvent(AnimationEdge.end);
}
break;
:
case AnimationMode.forward_reverse_cycle:
if (Direction.forward == this.direction) this.current++;
else this.current--;
if (this.frames.Length <= this.current) {
this.current = this.frames.Length - 1;
this.direction = Direction.reverse;
this.InvokeEdgeEvent(AnimationEdge.cycle);
} else if (this.current < 0) {
this.current = 0;
this.direction = Direction.forward;
this.InvokeEdgeEvent(AnimationEdge.cycle);
}
break; // whew!
:


Generalizing, on each update, we:
1. Increment the current frame - based on both mode, and direction.
2. If the current frame is out-of-bounds, we fix it, and direction.
3. If we made it to the edge, we pulse an event.
Note the complexity of this state machine. Some modes require three tests per update. There has to be a better way. There is. Delegates.


private Action increment = noop;
private Func<bool> condition = noop_false;
private Action fixup = noop;


Here is the actual update code:


private void IncFrame()
{
this.increment();
if (this.condition()) {
this.fixup();
this.InvokeEdgeEvent(this.eventType));
}
}


Note a couple of things about this. It is private. I actually call this from somewhere else in my class. The event type is a function of the kind of animation. It is part of the instance of the current animation.

I may not want to sync my animation frame rate to my update rate. The update may be called with a lot of jitter. Perhaps the update rate suffers no jitter, but this animation does. There are reasons one may want jitter in their animation. Perhaps to devote more frames to a part of the animation where there is more going on. By decoupling the animation rate from the update rate, one can author animations which have different rates, and varying rates. Look toward VBR coding for more information.

Really, in the animation, it only matters what the current frame is when displaying the animation.


public void Draw(DateTime now, Point target, IRenderingEngine rE)
{
this.SetCorrectFrame(now);
rE.Render(this.Frames[this.current].Image,target,this.Frames[this.current].Rectangle);
}

private void SetCorrectFrame(DateTime now)
{
TimeSpan delta = now - this.lastDrawTime;
if (delta >= this.Frames[this.current].Timespan) {
this.lastDrawTime = now;
this.IncFrame();
}
}


We'll need to set the mode of the animation.


public void SetMode(AnimationMode m)
{
this.mode = m;
if (this.mode == AnimationMode.stop) {
this.increment = noop;
this.condition = noop_false;
this.fixup = noop;
}
if ((this.mode == AnimationMode.forward_cycle) || (this.mode == AnimationMode.forward_to_ending)) {
this.increment = this.forward;
this.condition = this.pastEnd;
}
if ((this.mode == AnimationMode.reverse_cycle) || (this.mode == AnimationMode.reverse_to_beginning)) {
this.increment = this.reverse;
this.condition = this.pastBegin;
}
if ((this.mode == AnimationMode.reverse_cycle)||(this.mode == AnimationMode.forward_to_ending))
this.fixup = this.setEnd;
if ((this.mode == AnimationMode.reverse_to_beginning)||(this.mode == AnimationMode.forward_cycle))
this.fixup = this.setBegin;
if (this.mode == AnimationMode.forward_reverse_cycle) {
this.increment = this.forward;
this.condition = this.pastEnd;
this.fixup = this.forward_fixup;
}
if ((this.mode == AnimationMode.forward_cycle) || (this.mode == AnimationMode.reverse_cycle) || (this.mode == AnimationMode.forward_reverse_cycle))
this.eventType = AnimationEdge.cycle;
else this.eventType = AnimationEdge.end;
}


The actual delegates functions are trivial.


private void forward_fixup()
{
this.setEnd();
this.condition = this.pastBegin;
this.increment = this.reverse;
this.fixup = this.reverse_fixup;
}
private void reverse_fixup()
{
this.setBegin();
this.condition = this.pastEnd;
this.increment = this.forward;
this.fixup = this.forward_fixup;
}
private void setBegin() { this.current = 0; }
private void setEnd() { this.current = this.Frames.Length - 1; }
private static void noop() { }
private static bool noop_false() { return false; }
private static bool noop_true() { return true; }
private void forward() { this.current++; }
private void reverse() { this.current--; }
private bool pastEnd() { return (this.current >= this.Frames.Length); }
private bool pastBegin() { return (this.current < 0); }


The great thing about this is we have general-cased this to the point where even the forward-reverse mode is not special. In summary, we have replaced a shinola-load of switch/case/if statements with the simple:


increment();
if (condition()) {
fixup();
InvokeEdgeEvent(eventType));
}


Powerful.

Saturday, March 6, 2010

Exploring the Chain Of Responsability pattern

The COR pattern is one of the lesser gods in the design pattern universe. It is used when a set of handlers each have a shot at an object (think request), until one of the handlers indicates that it has taken responsibility for that object. Consider a network service that hosts a set of service handlers. A request comes in, and that request is handed down the chain of handlers until it meets all the conditions of that handler. The request is handled by that handler, and is ignored by all the following handlers. Obviously, the position of a given handler, with respect to others in the chain is important. The final handler is often a "Catch-All" to take care of those miscreants that nobody else wants. Examples of implementations the COR pattern include the RADIUS server (Internet Authentication Serer aka IAS) that ships with Windows 2000+. Every handler in that product is a "policy", which is a collection of conditions and a "profile", which is a collection of RADIUS attributes. When all the conditions in a policy are met, the request is handled. In this case, "handled" means that profile is returned to the client. Most profiles include the Access-Accept attribute. The final handler has the "anything" condition,and always returns a profile with an Access-Reject attribute. BTW, IAS has evolved to become the Network Policy Server in W2K8, so if you are wondering why your virus-infested laptop has been quarantined to a remote section of the network with only AV servers available, you have just experienced the power of the COR pattern.

Enough with that, let's implement one! Based on Bizz-Buzz


using System;
using System.Collections.Generic;

namespace FooBarTest // inspired by a coding test on codinghorror
{
public class Program
{
public static void Main(string[] args)
{
// We'll have a COR service that takes a number of returns a string
// There will be 4 policies in it
// the condition delegate indicates what a handled condition is.
ChainOfResponsability<string,int> cor = new ChainOfResponsability<string,int>(4,(s)=>{return (String.Empty != s);});
// The first stage is the "15" handler. Any int that is evenly
// divisible by 15 will return the string "foobar"
// Order is important. You'll want to order your conditions from
// specific to general
cor.AddPolicy(0, (n) => { return (0 == (n % 15)) ? "Bizz Buzz": String.Empty; });
// The order of second and third stage are not important.
cor.AddPolicy(1, (n) => { return (0 == (n % 5)) ? "Buzz" : String.Empty; });
cor.AddPolicy(2, (n) => { return (0 == (n % 3)) ? "Bizz" : String.Empty; });
// The most general condition. The "Catch ALl".
cor.AddPolicy(3, (n) => { return n.ToString(); });
// Look for "Bizz Buzz" when 15 is a root. "Buzz" for 5, and "Bizz" for 3.
for (int i = 0; i < 100; i++) Console.WriteLine(cor.Evaluate(i).result);
Console.ReadLine();
}
}

public class ChainOfResponsability<RESULT,VALUE>
{

public class Tuple // Results, and which policy handled it
{
public RESULT result { get; private set; }
public bool Handled { get; private set; }
public int Stage { get; private set; }
public policy Policy { get; private set; }
}

public Tuple(bool handled, int stage, policy p, RESULT r)
{
this.result = r; // the result of the policy that handled this
this.Handled = handled; // did the request get handled?
this.Stage = stage; // which stage handled it?
this.Policy = p; // which policy handled it?
}
}
public delegate bool EvalTrue(RESULT x); // function to determine that request handled
public delegate RESULT policy(VALUE y); // a policy function. The heart of our COR service.
public ChainOfResponsability(int stages, EvalTrue ev)
{
this.policies = new policy[stages];
this.CC = ev;
}
private EvalTrue CC; // We store the result evaluation function
private policy[] policies; // the set of policies.

// Add a policy to a specific stage in the pipeline.
// Specifying more than one per stage, is unwise. The later one over-writes.
public void AddPolicy(int stage, policy function)
{
if ((0 <= stage) && (this.policies.Length > stage))
this.policies[stage] = function;
else throw new Exception("invalid stage specified"); // TODO throw a more specific Exception
}

// Evaluate the value against each policy in the pipeline until a policy
// indicates that it has handled it.
// Values that aren't handled will result in the Tuple.Handled == false
}

public Tuple Evaluate(VALUE n)
{
bool handled = false;
RESULT result = default(RESULT); // the result of a policy that handles the value
int invokedStage = -1; // which stage handled it?
policy p = default(policy); // which policy handled it?

// go through each policy in our pipeline
for (int stage = 0; (stage < this.policies.Length && !handled); stage++) {
// If there is a policy at this stage, give it a shot at it
if (null != this.policies[stage]) {
result = this.policies[stage].Invoke(n);
handled = this.CC.Invoke(result); // did the current policy handle this?
if (handled) { // yep. let's remember where and who.
invokedStage = stage;
p = this.policies[stage];
}
}
}
return new Tuple(handled,invokedStage,p,result); // immutable
}
}
}

Monday, January 25, 2010

Golden Code

Creating software with the following features:

Testable
Extensible
Maintainable
Perfomant
Scalable
Secure
Reliable
Flawless