DaedTech

Stories about Software

By

Type Variance: Generics, Reflection, Anonymous and Dynamic

In working with C# quite heavily over the last few years and talking to a lot of people in different capacities, I’ve noticed a fair bit of confusion on the subject of object types and their attendant rules. So I’d thought I’d offer a post to help understand different factors that go into schemes for declaring types, using them, and keeping them in sync. Hopefully this helps you if you’ve found yourself confused in the past:

Generics

Just about anyone who has ever worked with C# is familiar with generics at least as a client. If you’ve ever declared a List, for instance, you’re using a generic. If you’ve been at it for a while and have a few more notches on your C# belt, you’ve probably written a class with a format like the following:

public class ConsoleWriter
{
public void WriteToConsole(T target)
{
Console.WriteLine(target);
}
}

Useless as it is, this class is instantiated with some type and operates on arguments of that type. For instance, if you declare a writer with integer, it will accept integer inputs and write them to the console:

var writer = new ConsoleWriter();
writer.WriteToConsole(24);

If you try to write to the console with some type other than int here, you’ll wind up with a compiler error. The reason for the error is that generics are a compile-time way of specifying type variance. That is, the compiler knows exactly what T will be as the code is being compiled and thus it is able to tell you if you’re supplying the wrong kind of T. And this makes sense when you think about it — you’re instantiating a List or ConsoleWriter, so the compiler knows that you intend for your instances to deal with ints.

Another way to think about generics is that a generic class is a sort of class “stub” that is waiting for additional information to be supplied when you create instances. That is, a ConsoleWriter is meaningless in a non-instantiated context in the same way as an abstract base class. It needs more information — it needs you to come along and say via instantiation, “this is the kind of ConsoleWriter I’m going to use.”

When you see a “T” (or any other generic descriptor) remember that it will not exist at runtime. In other words, when the program is actually executing, there is no ConsoleWriter<T> but only ConsoleWriter<int> . From a runtime perspective, you may as well have an actual class called “IntConsoleWriter” as far as the runtime is concerned. Generic parameters are meaningless when the application runs.

GetType()

Another way of exploring variability in types is with the object.GetType() method. This method takes advantage of two important facets of C#: (1) the fact that everything inherits from object and (2) reflection. Reflection is concept specific to managed languages (such as Java and C#) that allows executing code to inspect the state of its own source code (in a manner of speaking, but this is an oversimplification). For instance, if you had the class below, it would print “A” and then “B” to the console on separate lines when you called its DescribeYourselfToTheConsole() method.

class SelfDescriber
{
public int A { get; set; }

public int B { get; set; }

public void DescribeYourselfToTheConsole()
{
foreach (var property in this.GetType().GetProperties())
Console.WriteLine(property.Name);
}
}

For those who may not be familiar with reflection or fully aware of its usages, this is quite a powerful concept. As someone who cut his teeth on C and C++, I recall my first exposure to this many moons ago and thinking “you can do what?!?” Awesome!!!” Beware though — reflection is slow and resource intensive.

When you call GetType() on some object, you’re actually accessing the state of that object in code. Unlike generics, this is purely a run-time concept. And that makes sense as well — why would you need to lookup information about a class at compile time? You could just inspect the source code at that point. But at runtime, you might not know exactly what type you’re dealing with. Take a look at this modified version of ConsoleWriter from the last example:

public class ConsoleWriter
{
public void WriteToConsole(object target)
{
Console.WriteLine(string.Format("Value {0} is of type {1}", target, target.GetType()));
}
}

Console writer is getting an object passed to it, but that’s not particularly helpful in understanding more details about it. In unmanaged languages like C++, all you could really do in this situation is cast the object and hope for the best. But in managed languages like C#, you can actually inspect it to see what type it is and then do things with it accordingly. In this example, we simply print out its type, but you also have the option of doing things like saying if(target.GetType() == typeof(int)) or if(target is int) and then doing int-specific things.

Again, all of this happens at run-time when different execution paths dictate non-deterministic instance typing. And that’s just a fancy of way of saying that in an object-oriented, polymorphic language, you can’t know at compile time what instances you’re going to have at runtime since your instance creation may depend on external factors (for instance, if you have factory methods). So reflection is a runtime way to understand what type you’re dealing with.

Anonymous Classes

I’ve seen a lot of confusion around the subject of Anonymous classes. Sometimes I see them confused with dynamic types (covered next) and sometimes I see them misunderstood. Don’t think overthink when it comes to anonymous types — they’re just classes that don’t have names. Why don’t they have names? Because they’re declared inline in code instead of in the standard class structure. For instance:

public void SomeMethod()
{
var noNameType = new { customerId = 1, customerName = "Erik" };
Console.WriteLine(string.Format("Name is {0}", noNameType.customerName));
}

Here I’ve declared a type that has no name, but a Customer by any other name… well, you get the drift. In C# we can declare types inline this way. As it turns out, this comes in quite handy with Linq, IQueryable and its extension methods. It also gives rise to the “var” keyword that you see me use there, which is one of the most misunderstood language constructs there is. If I had a dollar for every time I’ve seen someone mistake this for the VB construct “dim” or some other dynamic typing moniker, I wouldn’t be Warren Buffet, but I’d definitely have enough money for a night on the town. “var” is the syntax for handling implied typing, which is the only means you have for declaring anonymous types (I mean, what else could you do seeing as they don’t have names?)

Anonymous typing is a compile time way of specifying types, like generics. However, unlike generics, anonymous types are not templates. They are normal, full blown, honest to goodness types that simply happen not to have names. If you tried to assign one to int or an int to one, the code would not compile, just as with any other static type that you’d made a class for.

Dynamic

In that last sentence, I mentioned static typing, which describes languages where reference values are declared and checked at compile time. In other words, with static typing, you would say “int x = 6” whereas in a dynamically typed language such as JavaScript you might simply say “x = 6” and thus it is up to the runtime interpreter to figure out that you’ve declared an integer. C# has its roots heavily in the camp of statically typed language, but in C# 4.0, the concept of dynamic typing (sometimes referred to as “duck typing”) was introduced.

The “dynamic” keyword basically tells the C# compiler, “this guy’s with us and he’s cool — no need to check him for weapons.” It’s then waved on through. For instance, the following code compiles without issue:

dynamic noNameType = "asdf";
noNameType = 12;

Assert.AreEqual(12, noNameType.ToString());

However, the test fails with a “RuntimeBinderException” meaning that he did have weapons after all. noNameType was an integer and so its ToString() method evaulated to a string, which normally wouldn’t have compiled but here failed at runtime. You can do all sorts of wacky stuff — instead of “ToString()” you could have said .AsadfFdsafasdfasf() and it still would have compiled (but obviously failed at runtime since that’s not a method on int).

Why would you want to do this, particularly in an otherwise statically typed language? Well, there are some use cases with painful interop and COM scenarios where getting ahold of actual types is surprisingly difficult and simply invoking methods you need to be there is comparably easy. Additionally, there are some interesting concepts like the expando object and doing things like impelenting interfaces at runtime, which can be useful if you’re doing rather intricate things like writing a mocking engine.

I would advise a good bit of caution here, though. C# is not an interpreted language and everyone reading your code and working with it is going to be used to and expecting statically typed code. You should probably only use dynamic typing when it’s onerous not to, unlike the other techniques I’ve covered here.

Wrap Up

So there you have it. We have two typing techniques that are compile time techniques (generics and anonymous types) and two that are run-time techniques (GetType()/reflection and dynamic typing). Generics are classes (or methods) that are templates allowing a type to use to be specified by clients at compile time. GetType() and reflection can be used to explore properties of types at runtime. Anonyomus types allow specification of an actual, first class type without the ceremony of an entire defined class, and dynamics allow us to ‘trick’ the compiler and break the rules… when we know (think) it’ll be okay. Take care with that last one, and use in good health. Hopefully this clears up the odd misconception or confusion you may have had about one or more of these concepts.

By

The Fragile Genius of Webforms

Turning Desktop Developers into Web Developers

I’ve been doing a lot of Webforms (ASP.NET) development over the last four months, as I’ve mentioned in some posts. Before that, I was doing a lot of WPF development for desktop applications and before that a hodgepodge of things that included web development with the Java stack. I had done very little with Webforms (though I had dabbled with a couple of internal corporate applications), so wrapping my head around it has been interesting.

For those of you not familiar with the way Webforms works, it could probably best be summarized as “helping you write web applications without realizing that’s what you’re doing.” Okay, that might be a bit snarky, so let’s go with “making windows desktop developers comfortable with writing web applications.” (in particular Winforms developers). It goes out of its way to create an abstraction layer that represents web concepts as their desktop equivalents which, I believe was done with the intention of easing the transition of a generation of Winforms developers to web application development. And, well done. I think it was wildly successful at that.

Indeed, it’s quite possible to write code for Webforms apps without understanding anything about HTTP, statelessness and the client-server nature of the web and understanding next to nothing about Javascript, HTML, and CSS. Instead of these concepts, Webforms developers are deal with concepts like “button click event” and “user control” ala Windows desktop development. It’s really quite a concept — “keep doing what you’re used to doing, and we’ll handle transforming the result from desktop apps to web apps.” It took me a while to stop wondering when I’d stop writing everything with wizards and datasources and gridviews and start outputting some templated markup with binding logic. It happened in trickles and drizzles but never really at all beyond that.

If your web experience is in any other technology, you’re certainly used to a less monolithic approach. Rather than “controls” that generate reams and gobs of HTML/CSS/Javascript behind the scenes, you’re no doubt used to the template approach where what you’re looking at is more or less HTML but with some notation for binding to a model and handling core concepts like iteration, validation or missing data. Perhaps there is also some notion of inline script execution. This is definitely how life works with technologies like JSP, PHP and even ASP.NET MVC. Developers using those technologies understand and embrace web as a delivery mechanism and architectural component and divide work up accordingly.

But not with Webforms. Webforms evens the playing field using wizards, WYSIWYG and extremely involved abstractions to make everyone working on a Webforms project a Webforms developer. Webforms actually abstracts understanding of underlying concepts away from the developers in this fashion, the way that Front Page and other such code generation tools have in the past. Karl Seguin gives an excellent Webforms vs ASP MVC summary that touches on these points and others.

A Brief Foray Into Architectural Philosophy

Persistence technologies, messaging/communication with externalities, web vs desktop are all non-core implementation details. Oh, don’t get me wrong, they’re important. But ebay doesn’t have an Oracle DB application or even a web application — it has a consumer resale application. The core of their business is enabling resale and that transcends the current technology stack. If Oracle went out of business tomorrow and a unified web became passe, ebay would no doubt try to soldier on by switching to MySQL and peer to peer app communication (or whatever). If people stopped wanting to resell goods, on the other hand, ebay would be kaput.

To staff a project that scales beyond tiny (i.e. multiple developers and up) a sensible way to divide up the work is to have some core business rules programmers and then others specializing in various satellite aspects of the application. So ebay might have people that develop server side application/business logic, people that specialize in Oracle stored procedures and people that specialize in web as a delivery mechanism and it would be expected that those people be domain experts, database experts and web experts, respectively. (Not to say that there couldn’t be some fluidity among these groups according to skill-set or availability and learning curve).

This separation of skills required in a team environment allows for the pairing of team members with tasks for which they are best suited. These pairings, in turn will naturally tend to foster productive collaboration, division of labor and generally good morale. People, especially knowledge workers like software developers, prefer autonomy and that tends to exist in greater abundance when each team member has a unique skill set that he or she is applying. A project with an architecture like the one I’m describing is more likely to be staffed by happy and productive developers.

Webforms and Generalist Mediocrity

Webforms does not lend itself well to the sort of architecture that I’ve just described. It invites developers to use controls that completely automate and obfuscate the generation of client side markup and scripts. But beyond that, it invites developers to describe the persistence layer in the specialized markup as well (the various “data sources” and their wizards), in effect flattening the application and rendering it monolithic. As such, Webforms developers can be (can be — not must be, please don’t misunderstand my point) relatively low-knowledge generalists whose only specialty is, well, Webforms. Webforms itself is the one with knowledge of different application layers and architectural patterns.

Image by “Scottius11” via wikimedia commons.

Whoah. Let’s think about that for a minute because it’s actually fairly profound. In a Webforms project, it is Webforms itself (or, more accurately, the people who designed ASP.NET) that understands the technologies involved. Webforms is your most knowledgeable ‘stakeholder’. Most successful frameworks and architectural technologies offer a cafeteria plan of sorts to let developers pick and choose what they need and want and to allow people to be paired up with particular facets of an application (a lot of IoC containers and various toolkits have this property). They empower developers. Webforms instead offers them a crutch — a way to perpetually develop applications without really understanding how they work.

Imagine that you owned dogs all your life but decide you want a cat. Now imagine a team of engineers coming along to ease the transition. They build a wooden “Trojan Dog” with a litter box in it so that you can “walk the cat” to get it to go to the bathroom. They build a sound altering muzzle so that when the cat meows, it sounds like a bark. They give you toys and pants loaded with catnip so the cat will ‘retrieve’ the toy and bring it back to you in game of ‘fetch’. In short, they create all manner of pretty elaborate and impressive adapter tricks to allow you to own cats and pretend that they’re dogs. They probably also do a bit of hand-waving when asked why this scheme makes any sense.

The Genius And Fatal Flaw

Like the hypothetical “dog to cat transition technology”, the Webforms abstraction of all things web is impressive. It’s impressive for a group of developers to be able to create a technology that allows tens of thousands of people to write applications without really understanding the underlying technology stack (again, not in all cases — just saying it’s possible). It’s impressive for those developers to create something that allows other developers to generate a radically different type of application without having to learn much in the way of new skills. I can’t overstate that — it’s amazing.

But the same thing that makes it amazingly effective at bringing old school Winforms developers into the 21st century also limits their growth and that of the applications that they produce. Webforms is a “helicopter parent” that doesn’t, on its own, know how to stop telling its 17 year old child when it’s time to brush his teeth and wash his face, and thus it runs the risk of perpetuating learned helplessness in the same. The internet is no longer new and alien, and our web development toolbox should reflect that.

ASP.NET MVC is slick, powerful and impressive as a framework. Developers should be comfortable with Javascript, HTML and CSS if they work on the client side. Developers should be familiar with IoC, communications protocols, decoupling and testing if they work on the server side. They should be using frameworks to enhance their knowledge and speed their work and not to handle everything for them so they don’t have to understand. It’s time to appreciate the genius of Webforms, acknowledge its importance, and wish it well in retirement.

By

Factory Method

Quick Information/Overview

Pattern Type Creational
Applicable Language/Framework Agnostic OOP
Pattern Source Gang of Four
Difficulty Moderate — slightly easier than Abstract Factory

Image courtesy of dofactory

Up Front Definitions

  1. Creator: This is the basic, abstract factory interface.
  2. Concrete Creator: Inheritor factory classes that implement some actual object creation scheme.
  3. Product: This is the base/abstract polymorphic object that gets created by the factories.

The Problem

Let’s say that you’re coding up some kind of game and in this game a character navigates a world and encounters various entities with which to interact, including enemies. You’re starting out with a very bare bones proof of concept, and here is your sketch of some enemies:

public abstract class Enemy
{
public abstract string Attack();
}

public class Dragon : Enemy
{
public override string Attack()
{
return "You've been torched with fire breath!";
}
}

public class Gator : Enemy
{
public override string Attack()
{
return "You've been chomped!";
}
}

public class HouseFly : Enemy
{
public override string Attack()
{
return "You've been annoyed!";
}
}

public class MotherInLaw : Enemy
{
public override string Attack()
{
return "You've been guilt-tripped!";
}
}

public class ChuckNorris : Enemy
{
public override string Attack()
{
return "Your entire existence has been erased from space-time in one devastating but magnificent master-stroke!";
}
}

The concrete enemies return a string representation of their attack, which you will, no-doubt, later transform into some kind of first class attack object or even graphic as the game moves along, but for now, you just want to get the concept right. To get things going, you create the following class:

public class GameNarrative
{
public void MoveMainCharacterTo(string placeName)
{
if (placeName == "swamp")
InteractWithEnemy(new Gator());
}

private void InteractWithEnemy(Enemy enemy)
{
Console.Write(enemy.Attack());
}
}

You want to keep things clean, so you abstract an interaction method, knowing that at some point this will probably mean something different than dumping to Console. From there, you’re now left to figure out the enemy situation based on where the character goes and you’re off to a good start since gators tend to hang out in swamps. Let’s add in a few more scenarios:

public void MoveMainCharacterTo(string placeName)
{
if (placeName == "swamp")
InteractWithEnemy(new Gator());
else if (placeName == "starbucks" || placeName == "family reunion")
InteractWithEnemy(new MotherInLaw());
else if (placeName == "middleEarth")
InteractWithEnemy(new Dragon());
else if (placeName == "texas truck stop")
InteractWithEnemy(new ChuckNorris());
else
InteractWithEnemy(new HouseFly());
}

Now you’re off and running. You decide that HouseFly is a good enemy to use in this case, since it seems like a good stock enemy ala the “Goombas” in Super Mario Brothers or Foot Soldiers in the Teenage Mutant Ninja Turtles (I’m probably dating myself a bit here). But wait a minute — house flies could happen anywhere. Let’s say that they’re the else condition, but they also have a 50/50 chance of appearing in the other places. Hmmm… let’s introduce an abstraction to hide the ugly and annoying details of randomization and do this:

public void MoveMainCharacterTo(string placeName)
{
if (placeName == "swamp")
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new Gator());
else
InteractWithEnemy(new HouseFly());
}
else if (placeName == "starbucks" || placeName == "family reunion")
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new MotherInLaw());
else
InteractWithEnemy(new HouseFly());
}
else if (placeName == "middleEarth")
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new Dragon());
else
InteractWithEnemy(new HouseFly());
}
else if (placeName == "texas truck stop")
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new ChuckNorris());
else
InteractWithEnemy(new HouseFly());
}
else
InteractWithEnemy(new HouseFly());
}

Ew, that’s starting to smell a bit, but no time to worry about that now. Your efforts here have led to some angel investment capital and now the pressure is on. You use that money to hire a UX/gameplay expert so that he can worry about the gaming decisions while you focus on architecture. He tells you that you need to introduce the concept of difficulty and that Chuck Norris can be anywhere except Middle Earth but only if the difficulty level is set to 5 or more. But Chuck Norris is a family man himself, so he’ll only attack you at a family reunion on a really high difficulty level like 10. Otherwise, he’ll wait until you leave. Hmmm….

public void MoveMainCharacterTo(string placeName)
{
if (placeName == "swamp")
{
if (_difficultyLevel > 5)
{
switch (_randomizer.PickANumberOneThrough(3))
{
case 1: InteractWithEnemy(new Gator()); break;
case 2: InteractWithEnemy(new HouseFly()); break;
case 3: InteractWithEnemy(new ChuckNorris()); break;
}
}
else
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new Gator());
else
InteractWithEnemy(new HouseFly());
}
}
else if (placeName == "starbucks")
{
if (_difficultyLevel > 5)
{
switch (_randomizer.PickANumberOneThrough(3))
{
case 1: InteractWithEnemy(new MotherInLaw()); break;
case 2: InteractWithEnemy(new HouseFly()); break;
case 3: InteractWithEnemy(new ChuckNorris()); break;
}
}
else
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new MotherInLaw());
else
InteractWithEnemy(new HouseFly());
}
}
else if (placeName == "family reunion")
{
if (_difficultyLevel > 10)
{
switch (_randomizer.PickANumberOneThrough(3))
{
case 1: InteractWithEnemy(new Gator()); break;
case 2: InteractWithEnemy(new HouseFly()); break;
case 3: InteractWithEnemy(new ChuckNorris()); break;
}
}
else
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new Gator());
else
InteractWithEnemy(new HouseFly());
}
}
else if (placeName == "middleEarth")
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new Dragon());
else
InteractWithEnemy(new HouseFly());
}
else if (placeName == "texas truck stop")
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new ChuckNorris());
else
InteractWithEnemy(new HouseFly());
}
else
{
if (_difficultyLevel > 5)
{
if (_randomizer.IsRandomCoinFlipHeads())
InteractWithEnemy(new ChuckNorris());
else
InteractWithEnemy(new HouseFly());
}
else
InteractWithEnemy(new HouseFly());
}
}

You look at this and realize that you’re definitely whipping up some spaghetti code here, but no time for that now. Your game designer tells you that you need a new mode of the game where instead of interacting with the enemies you talk with them first. The logic for encountering the enemies is exactly the same and you still need your old code, but you need it in a new module. Oh, and you should probably add a few new enemies that you find in a few new places. And house flies should probably stop appearing after difficulty level 7. Oh, and dragons can appear in Starbucks sometimes after a successful battle with MotherInLaw. Oh, and gators can appear at your family reunion if you have it in Florida.

Here’s the code for that… just kidding. You’re probably not going to read subsequent posts if I destroy your eyesight with the code I’m describing.

So, What to Do?

As has become customary in this section, let’s examine what went wrong. Most of the time it’s the point at which copying and pasting start, but not in this case. Another good tell in this series of posts where things go off the rail is when I start trotting out excuses for what ‘you’ are doing in the second person narrative. As soon as things are a little ugly because ‘you’ have a deadline or ‘your’ project manager is riding you, that’s usually when things went wrong. Here is no exception. Let’s rewind to the second code snippet, representative of a simpler time:

public void MoveMainCharacterTo(string placeName)
{
if (placeName == "swamp")
InteractWithEnemy(new Gator());
else if (placeName == "starbucks" || placeName == "family reunion")
InteractWithEnemy(new MotherInLaw());
else if (placeName == "middleEarth")
InteractWithEnemy(new Dragon());
else if (placeName == "texas truck stop")
InteractWithEnemy(new ChuckNorris());
else
InteractWithEnemy(new HouseFly());
}

This is alright, though I would argue that we can make a nice improvement here. Notice that we’re following a pretty consistent pattern here: we check place name and then we invoke the same method on a polymorphic object that varies with place name. It might seem like a trivial bit of redundancy, but these add up as you leave them in and, I would argue, poison the well when it comes to future design decisions. Broken Window Theory and all that. So let’s change the code to this:

public void MoveMainCharacterTo(string placeName)
{
var enemy = GenerateLocalEnemyFor(placeName);
InteractWithEnemy(enemy);
}

private static Enemy GenerateLocalEnemyFor(string placeName)
{
switch (placeName)
{
case "swamp": return new Gator();
case "starbucks":
case "family reunion": return new MotherInLaw();
case "middleEarth": return new Dragon();
case "texas truck stop": return new ChuckNorris();
default: return new HouseFly();
}
}

Notice that there’s now only one call to “InteractWithEnemy” and we’ve split the functionality of mapping place name to en enemy instance from the functionality of interacting with the enemy in a nod to the SRP. You also might notice that we have implemented what has come to be a common variant of the original Factory Method pattern, which is the static factory method. A static factory method is a stateless method that takes in some criteria and spits back a matching polymorphic instance of the common base return type. For instance, this method says “if the place is middle earth, we need a dragon enemy.”

With this refactoring accomplished, let’s take on the next requirement, which is that house flies can occur anywhere with a 50% probability. Before our heads start rushing with design patterns and complex implementations, let’s consider what might be the easiest way to get this working. Didja think of this:

public void MoveMainCharacterTo(string placeName)
{
var enemy = GenerateLocalEnemyFor(placeName, _randomizer);
InteractWithEnemy(enemy);
}

private static Enemy GenerateLocalEnemyFor(string placeName, IRandomizeThings randomizer)
{
if (randomizer.IsRandomCoinFlipHeads())
return new HouseFly();

switch (placeName)
{
case "swamp": return new Gator();
case "starbucks":
case "family reunion": return new MotherInLaw();
case "middleEarth": return new Dragon();
case "texas truck stop": return new ChuckNorris();
default: return new HouseFly();
}
}

Notice how easy that is with a little bit of logic separation. But, if that implementation strategy were going to hold up, there’d be no meat to this post, so let’s press on. Chuck Norris can be anywhere except for Middle Earth and a family reunion if the difficulty level is at least 5 and anywhere except Middle Earth if the difficulty level is at least 10. At this point it’s important to recognize what’s happening here and it’s easier to do so now with this factored road down which we’ve started. What’s happening is that what was originally a simple mapping from place name to enemy inheritor is becoming muddied by a growing number of additional factors. And what’s a good way to handle additional complexity in an object oriented programming language? Well, encapsulate it in an object.

The idea of centering the enemy generation around places is appealing since that’s how we originally conceived of it, so let’s stick with that. The family reunion one seems to be the most complicated, so let’s take a look at that:

public class FamilyReunionFactory
{
private readonly IRandomizeThings _randomizer;

public FamilyReunionFactory(IRandomizeThings randomizer)
{
_randomizer = randomizer;
}

public Enemy GetEnemy(int difficultyLevel)
{
if (difficultyLevel < 10)
return _randomizer.ChooseAtRandom(new HouseFly(), new MotherInLaw());
else
return _randomizer.ChooseAtRandom(new HouseFly(), new MotherInLaw(), new ChuckNorris());
}
}

(I added a “choose at random” method to the interface of signature T ChooseAtRandom<T>(params T[] thingsToChoose) to keep the emphasis on the factory class as much as possible rather than distracting with worries about random implementation. I acknowledge that this would have made the problem code a little less noisy, but it still would have been headed down a bad street with creeping complexity)

Cool! Now the GameNarrative class doesn’t need to worry about the creeping complexity around what goes into the decision of which enemy to generate beside the place name. We have a class with the single responsibility of figuring out how enemies get generated for the family reunion location. But how does this fit with the original class? Well, let’s take a look at an interim solution. Here is an updated, no-longer-static factory method:

private Enemy GenerateLocalEnemyFor(string placeName)
{
switch (placeName)
{
case "swamp": return new Gator();
case "starbucks":
case "family reunion": return new FamilyReunionFactory(_randomizer).GetEnemy(_difficultyLevel);
case "middleEarth": return new Dragon();
case "texas truck stop": return new ChuckNorris();
default: return new HouseFly();
}
}

Notice that instead of directly instantiating an enemy when we’re at the family reunion, we delegate that task to our newly created factory. Let’s make a factory for each place now so that we can remove that awkward GetEnemy call and keep this method clean. In order to do this and return a factory instead of an enemey, we’re also going to need to make these factories polymorphic cousins of one another, by defining an interface (could also be an abstract base, which is less decoupled but allows implementation of a common constructor).

public interface IFamilyReunionFactory
{
Enemy GetEnemy(int difficultyLevel);
}

And, with that in place and all of the other factories defined (elided), here is the new narrative class:

public class GameNarrative
{
private readonly IRandomizeThings _randomizer;

private readonly int _difficultyLevel;

public GameNarrative(IRandomizeThings randomizer, int difficultyLevel)
{
_randomizer = randomizer;
_difficultyLevel = difficultyLevel;
}

public void MoveMainCharacterTo(string placeName)
{
var enemyFactory = GetEnemyFactory(placeName);
var enemy = enemyFactory.GetEnemy(_difficultyLevel);
InteractWithEnemy(enemy);
}

private IEnemyFactory GetEnemyFactory(string placeName)
{
switch (placeName)
{
case "swamp": return new SwampFactory(_randomizer);
case "starbucks": return new StarbucksFactory(_randomizer);
case "family reunion": return new FamilyReunionFactory(_randomizer);
case "middleEarth": return new MiddleEarthFactory(_randomizer);
case "texas truck stop": return new TexasTruckStopFactory(_randomizer);
default: return new DefaultFactory(_randomizer);
}
}

private void InteractWithEnemy(Enemy enemy)
{
Console.Write(enemy.Attack());
}
}

That’s looking pretty darn legible now. But it’s not just that the code is easy to read or even that it’s separated into factored methods and classes. The important thing here is conformance to SRP and the fact that it’s easy to find what needs to change and change it independently of other concerns. Think back to the change request that broke our spirit earlier. Adding new enemies? Piece of cake — just add classes for them and then add them to the random generation in the factories that are eligible to generate them. Porting enemy generation? Just make the GetEnemyGeneration method (or some variant of it) publicly accessible and call it elsewhere. All of the new enemy generation business rules? Put them in their appropriate factories. Cross cutting rules? Create a base factory class. These tasks are now quite manageable.

A More Official Explanation

The stated purpose of the Factory Method design pattern (from dofactory) is:

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

This creational pattern is also sometimes referred to as “virtual constructor” because of the way it separates the promise of object creation from the implementation via polymorphism. Be it an interface or abstract base class, the abstract definition defines a contract for the generation of polymorphic entities and then delegates that work to implementers/inheritors.

This has the powerful benefit of allowing flexibility and following of the open/closed principle not just for creation of objects but for strategies for object creation. In our example above, the IEnemyFactory is the Creator, the various factories are the Concrete Creators and the enemies are Products. We have the ability with this implementation to seamlessly add new enemies but also to seamlessly add new schemes for generating enemies. If we wanted to add a location called “mountains” with its own unique set of denizens to the game, this would be straightforward and have nearly 0 bearing on existing code.

It is worth noting the conceptual similarity to Abstract Factory at this point and pointing out that the jump from Factory Method to Abstract Factory is just the addition of more polymorph families. For instance, if each location had enemies, terrain, and allies, Abstract Factory would be a good fit. That said, the transition from Factory Method to Abstract Factory is extremely violent to the code base since the addition of methods to the factory require changes to all implementers. Thus it is probably good to spend some effort up front reasoning about what the factory should do, particularly if the concrete factories are spread far and wide (unless you are using an abstract base class and then there is some reprieve here as you can specify default behavior).

Other Quick Examples

  1. An enterprise application capable of pulling information from multiple persistence models (factory methods to create DAOs).
  2. A GUI intensive application that generates controls/forms/pages on the fly depending on user interaction.

A Good Fit – When to Use

This is an interesting one circa 2012 because it seems like a decent amount of the heavy lifting that I used to see this pattern do has been replaced by IoC containers or plugin architectures. In other words, the Gang of Four made it fashionable to move complex instance creation into its own place in your code, and the Unit Test/TDD crowd later made it fashionable to move it out of your code altogether. It’s also been squeezed on the other side, so to speak, by the prevalence of the “poor man’s factory method” — a simple static method that returns polymorphs representing a common interface or base class.

But in reality, this pattern has its own niche not covered by either of these situations. IoC containers and, to some extent, plugin architectures are really about decisions made at application start time. The point of abstracting them out of the codebase is to allow application behavior to be changed without rebuilding and re-deploying. Factory Method as I would advocate covers a subtly different usage scenario wherein the polymorphic type is not known at application startup. An obvious example (and the one we covered in this post) is where the user supplies input that governs which object should be created in some way.

But this could still mean using the static method rather than the class hierarchy of Factory Method. I would recommend the latter specifically in situations where the logic surrounding instance creation is more complex than switching over a simple argument or two and/or likely to grow and change with time (the aforementioned Open/Closed Principle). If you find that you have a static factory method that’s ballooning in size and cyclomatic complexity it’s probably time to consider a factory method hierarchy.

Square Peg, Round Hole – When Not to Use

The inverse of various situations described in the previous section is a good place to start. You probably don’t want to use this pattern for very simple scenarios (say, switching over an enum or character to create instances) and you probably don’t want to use it for decision that can be made prior to runtime as that introduces more complexity and indirection to your application than necessary. Another place that you’d avoid this is when you need an abstract factory because you’re creating object families.

As always, YAGNI applies, and factories are sort of iconic in representing over-engineering in OOP. I once heard a cynical but funny quote (don’t recall who originally said it, but if you know, please comment): “some people think they’ll use java to solve their problems — now they have a problem factory.” This is obviously tongue in cheek, but the implied point about pattern happy enterprise development shines through. It’s easy to get pretty elaborate when abstracting instance creation out of plumbing and service code (and I love the practice) but there’s a fine line between this and creating too many levels of indirection. Be careful that you actually need these factories and aren’t just speculatively abstracting. As a rule of thumb, I’d say that you should be able to look at them and think “man, it’s a good thing I pulled that into a class.”

So What? Why is this Better?

As show in the example, when judiciously used, the Factory Method pattern can transform what would have been a snarl of instantiation spaghetti into a well separated, maintainable set of classes. The “after” in this picture shows code that conforms to established OOP principles such as the Open/Closed Principle and the Single Responsibility Principle (you can throw Liskov in there too since there’s a lot of polymorphism here).

Once this is in place, adding new target/product objects for creation is a segregated and approachable problem and adding new creation schemes becomes quite easy. It’s also straightforward to separate creation from use in your code as well, leading to a lot of architectural flexibility. And, properly applied, it’s a good mechanism for avoiding the bane of programming existence — copy and paste coding.

By

Practical Math for Programmers: DeMorgan’s and Other Logical Equivalences

The “Why This Should Matter to You” Story

TL;DR: You read my post about conditional bloopers and thought, “what’s wrong with that?!?”

Let’s say that you recall the lesson from last time and you make sure that you also avoid statements like “if(A && (A || B))”. You draw out truth tables whenever you feel you have time (and when no one is looking because, if you’re honest with yourself, you feel a little awkward doing it when people are watching). And yet things like this still happen to code you’ve written:

public void DoNightThings()
{
if (!(!_itsLate || !_iAmTired))
GoToBed();
}

gets refactored to:

public void DoNightThings()
{
if (_itsLate && _iAmTired)
GoToBed();
}

When you notice this in the source history, you check with your truth tables, and sure enough, it’s right. You kind of get it, in the end, but you always feel like you’re a step behind.

Math Background

In Boolean algebra, truth tables are sort of the equivalent of rote arithmetic memorization such as learning multiplication tables and using them to perform long-hand multiplication of larger numbers. It’s a rote, brainless, and time-consuming algorithm that you follow when there isn’t something better to use to get you to the answer more quickly. But the good news is that if you memorize a different set of information — some axiomatic laws of equivalence — you can get a lot of answers more quickly and easily. Here is the series of equivalences paired with explanations that will really help you understand the finer points of simplifying your conditional logic.

Identity Laws:  (A ^ TRUE) = A and also (A v FALSE) = A.  These identity laws simply point out the identity of variables compared with special constants.  This is like talking about additive identity (x + 0 = x) or multiplicative identity (x*1 = x) in elementary algebra (i.e. what you learn in junior high).

Domination Laws:  (A ^ FALSE) = FALSE and also (A v TRUE) = TRUE.  These are the opposites of the identity laws and have elementary algebra annihilator equivalent (x*0=0) or (x/x = 1).  These operate simply in that any false in a conjunction or any true in a disjunction renders the entire expression false or true, respectively.

Double Negative:  ~~A = A.  (Probably more formally known as “double negation” or negation of negation, but I think this easier to remember).  This quite simply says that if something is not not there, then it is there or, in Boolean logic, two falses make a true.

Commutativity:  (A v B) = (B v A) and (A ^ B) = (B ^ A).  This simple but important law    shows that the order of arguments to the conjunction and disjunction operators do not matter.  Think back to your arithmetic days when you learned that 4+3 = 3+4.  Same principle.

Associativity:  (A v B) v C = A v (B v C) and (A ^ B) ^ C = A ^ (B ^ C).  You can think of this as demonstrating that the location of parantheses is irrelevant when all operations are conjunction or disjunction operations.  If you reach ever further back in your brain from arithmetic, you may remember this guy as 3 + (4 + 5) = (3 + 4) + 5.

Tautology:  (A v ~A) = True and (A ^ ~A) = False.  We went over this in more detail last time, but suffice it to say that conjunction and disjunction with a variable’s negation leads to vacuously true or false results.

Idempotence:  (A v A) = A and also (A ^ A) = A.  Idempotence in general is the idea that an operation can be applied multiple times but only have a single effect.  For a rather practical example, consider turning a light switch on; the first time you push up a light turns on, but subsequent pushes up don’t turn the light somehow “more on”.  Idempotence in Boolean expressions means that redundant conjunctions and disjunctions simply have no effect.

Distribution:  A ^ (B v C) = (A ^ B) v (A ^ C) and A v (B ^ C) = (A v B) ^ (A v C).  This is a powerful equivalence because it provides a means to “move” parentheses around in Boolean expressions and retain their exact truth values.  Consider this in English and there is sense to it: “If A is true and also either B or C is true then A or C is true and A or B is true.”  Should that be a little abstract for your taste, think of it conversationally “It’s raining and either I’m inside or I’m wet” is the same as “It’s raining and I’m wet or it’s raining and I’m inside.”

De Morgan’s Laws:  ~(A ^ B) = (~A v ~B) and ~(A v B) = (~A ^ ~B).  This is another heavy hitter like distribution except that this one allows us to “move” negations when simplifying Boolean expressions.  This one also makes sense conversationally: “If it’s not true that person X is male and person x is female, then either person X is not male or person X is not female.”  De Morgan’s Laws also have an interesting incarnation in higher order logics like first order logic, but that’s probably going to be beyond the scope of this series for quite some time.

Absorption: A v (A ^ B) = A and A ^ (A v B) = A.  This was the main example we covered last time, so you can work out the truth table if you like.  As it turns out, this is a formally defined equivalence as well.

How It Helps You

In the last post in this series, I covered truth tables, which are sort of like the Boolean algebra equivalent of long division.  They’re a mechanical, inelegant process by which you can brute force a correct answer.  Logical equivalences are algebraic axioms that provide you with the building blocks for deductive reasoning — the building blocks for really thinking things through and critically solving problems.

You don’t necessarily need to memorize these, but I would study them enough at least to have something tickle the back of your mind the next time that you’re staring at an incredibly complex looking conditional.  That something will be “I think we can do better than this” and then, even if you don’t have the equivalences memorized, at least you’ll know enough to go look them up.  Armed only with truth tables, this can feel like flailing around in the dark while looking for a pattern.  But armed with logical equivalences you’ll say “oh, that’s right — I can eliminate the not here and I can turn these ors into ands over here, and… yeah, much better.”

Memorizing these equivalence laws would be even better, but not for the sake of pedantry or winning obscure trivia games.  I say that memorizing them is better because if you memorize them, you will practice them and conditionals, particularly complex ones, will start to work themselves into simpler forms automatically in your mind.  You will see things like Neo in the Matrix when he’s about to defeat Agent Smith, if the movie were a lot more boring.  Point is, though, you’ll develop an extremely good sense for when conditional logic could be simplified, corrected, or clarified and that has powerful ramifications for the readability, correctness and maintainability of the code you work with.

To circle back to the small example of a real world situation, understanding these Boolean laws of inference is likely to put you ahead of the curve, rather than behind it, when it comes to understanding conditional simplification.  Rather than the person whose code is always getting refactored, you’ll be the one doing the refactoring and doing it confidently.

Further Reading

  1. Wikipedia
  2. A bit more circuit flavored, but with some applied examples at the bottom.
  3. An introduction to the idea of proofs in Boolean algebra
  4. Very theoretical, with formal proof for most of the inference laws.
By the way, if you liked this post and you're new here, check out this page as a good place to start for more content that you might enjoy.

By

One Singleton to Rule Them All

I would like to preface this post by saying that I don’t like the Singleton design pattern at all. There are some who love it, others who view it as a sometimes-useful, often-abused tool in the toolbox, and then there are people like me who believe that it’s extremely rare to see a use of it that isn’t an abuse — so rare that I consider it better to avoid the pattern altogether. I won’t go into much more detail on my thoughts on the Singleton (stay tuned for that when I get to this pattern in my design patterns series), but here is some further reading that expounds a lot on reasons not to like this pattern:

  1. Scott Densmore: Why Singletons are Evil
  2. Misko Hevery: Singletons are Pathological Liars
  3. Alex Miller: Patterns I Hate
  4. Steve Yegge: Singleton Considered Stupid

With all of those arguments against Singleton in mind, and considering the damage that abuse (and I would argue use) of the pattern causes in code bases, I found myself thinking of code bases I’ve heard of or encountered where the code was littered with Singletons. In these code bases, refactoring becomes daunting for a variety of reasons: the hidden dependencies, the complex temporal dependencies, the sheer volume of references to the Singleton instances, the almost obligatory Law of Demeter violations, etc. Singletons cause/encourage a rich variety of problems in code bases. But I think that something could be done about two such problems: the redundant Singleton implementation logic and the Single Responsibility Principle (SRP) violation of which Singleton classes are prima facie guilty.

Take a look at how the Singleton is implemented (as recommended by Microsoft, here). The basic implementation initializes the static instance in the property accessor while variants use field initializing syntax and introduce thread safety.

The basic implementation is here:

public class Singleton
{
     private static Singleton instance;

     private Singleton() {}

     public static Singleton Instance
     {
          get
          {
               if (instance == null)
                    instance = new Singleton();
               return instance;
          }
     }
}

More specific even to C# is James Michael Hare’s implementation using the Lazy<T> class to eliminate the redundant if-instance-null-instantiate logic needed in each Singleton class. But that still leaves the redundant Instance property, the redundant _instance field, and the awkwardness (SRP violation) of an object managing its own cardinality. What if we got rid of both of those issues?

public static class Singleton where T : new()
{
     private static readonly Lazy _instance = new Lazy();

     public static T Instance { get { return _instance.Value; } }
}

public static class SingletonClient
{
     public void Demonstrate()
     {
          Singleton.Instance.Write("Look!! Logger isn't a Singleton -- it's a real class!!");
     }
}

Using generics, this implementation allows an instance of every Singleton to be expressed in the code of a single class. In this fashion, any type can be implemented as a Singleton without the type being coupled to an internal Singleton implementation. It also standardizes the Singleton implementation by making sure it exists only once in the code base. With this pattern, there is a similar feel to IoC containers — classes can be implemented without concern for who will instantiate them and how.

Here is my take on the disadvantages and potential disadvantages of this approach:

  1. You still have Singletons in your code base.
  2. This might actually encourage more Singleton usage (though fewer actual Singleton implementations) by removing responsibility for implementation.
  3. Since constructor of object in question must have public default constructor, removes the Singleton gimmick of making the constructor private and thus the safeguard against additional instances being created is also removed.
  4. A natural extension of the previous item is that it remains a matter of documentation or convention to use Singleton<T> and not instantiate rather than it being impossible to instantiate.

But the advantages that I see:

  1. Only one class implements cardinality management, meaning refactoring away from Singleton is nominally easier.
  2. No Singleton SRP violations
  3. Singleton implementation (initialization, lazy load, thread-safety) is standardized to prevent inconsistent approach.
  4. I actually consider preventing the private constructor gimmick to be a plus — particularly where potential refactoring may be needed (and it pretty much always is when Singletons exist).
  5. With this pattern in your code base developers are less likely to be come comfortable/familiar with implementing the Singleton pattern.
  6. No need to introduce interfaces for Singletons to inject instance into classes using dependency injection — just inject a non-Singleton managed instance.
  7. A code base with nothing but normal instances is very easy to reason about and test, so the closer you get to that, the better.

I would like to reiterate that I’m not suggesting that you run out and start doing this everywhere. It mitigates some of the natural problems with the Singleton design pattern but “mitigates some” is a long way from “fixes all”. But, if you have a code base littered with Singletons this could potentially be a nice intermediate refactoring step to at least standardize the singleton implementation and provide slightly more friendly seams to testing legacy code. Or if you’re faced with and outvoted by a group of other developers that love their global variables (er, excuse me, Singletons), this might be a decent compromise to limit the damage they cause to the code’s maintainability. On balance, I’d say that this is an interesting tool to add to your arsenal, but with the caveat that something is probably wrong if you find that you have a use for it.

I’d be curious to hear others’ takes on this as well. I just kind of dreamed this up off the top of my head without taking a lot of time to explore all potential ramifications of it use. If you’ve implemented something like this before or if you have opinions on the advantages/disadvantages that are different than mine or if you see ways it could be improved, I’d be interested to hear about it in the comments.