Site Launch: PreferredSat.com

PreferredSat.com screenshotOn behalf of the image0.com web team, I’d like to announce the launch and welcome PreferredSatellite into our the image0.com family.

Implementation Approach
After the design of the site was finalized, the team had to make a decision on the approach and technology used.  Since the client updates the deals and content quite frequently, developing the site from the ground up in ASP.Net was automatically out the window.  This definitely screamed for a CMS.

The site was going to be hosted on shared hosting, so open source was definitely the way to go.  Between Drupal and WordPress, the latter was the better option based on the plugins available at the time, the ease of managing content for the client, and the little configuration required in the initial stages to get started on the development.    One of those plugins is the seamless integration of Google Analytics that is currently allowing the client to track conversions to the Order Now page.

A great experience overall, it is with great excitement that we announce this addition into the image0.com web portfolio.

Design Patterns Series: The Strategy Pattern

To use the example from Head First: Design Patterns, suppose you have a class called Duck and two classes that inherit it: MallardDuck and RedheadDuck.

Duck
quack()
swim()
display()
fly()

What happens when you are asked to implement a RubberDuck class? A RubberDuck does not fly() or quack() so the design is a bit off.  This is actually a great example that illustrates that a “has a” relationship is better than an “is a” relationship.  Instead of inheriting from a Duck superclass, we will be inheriting some interfaces, that represent behaviors – FlyBehavior and QuackBehavior.

The Strategy Pattern, as the book describes it is, “defines a family of algorithms, encapsulates each one, and makes them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.”   Here is what the Duck class looks like after we apply the Strategy pattern.
Client

Duck
FlyBehavior flybehavior
QuackBehavior quackBehavior
swim()
display()
setFlyBehavior()
setQuackBehavior()

encapsulates

FlyBehavior (interface)
fly()

inherited by

FlyWithWings
fly()

and

FlyNoWay (used by RubberDuck)
fly()

Similarly, you can have apply the same principle to QuackBehavior.

Here is what the code looks like in C#.

public abstract class Duck
{
  QuackBehavior quackBehavior;
  FlyBehavior flyBehavior;
}
public class MallardDuck : Duck
{
   public MallardDuck()
   {
        flyBehavior = new FlyWithWings();
        quackBehavior = new Quack();
   }
}
public class RubberDuck : Duck
{
     flyBehavior = new FlyNoWay();
     quackBehavior = new QuackNoWay();
}

The takeaway here is to use behaviors, or has-a relationships on entities that might change later on so that you don’t have to rewrite the parts of the code or class that do not change.

The Quintessential OOP Topic – Design Patterns: A Series

Someone has already solved your problems.

This is how the Head First: Design Patterns book starts.  Motivating, isn’t it? Maybe this as far as OOP, or object-oriented programming is concerned.  OOP has been around for quite a while, so chances are someone has already encountered a similar problem and probably solved it.

The goal in OOP is to develop maintainable, reusable code with high cohesion and low coupling.  High cohesion refers to the measure of how much the methods have in common with the class they are in.  An example of a low coupled class is something like this:

Employee (class)
- GetSupervisor()
- IsBonusEligible()
- SendEmail()

As you can tell, the SendEmail() has no business being there, which makes this a low-cohesive class.

Low Coupling is where one class has a low degree of dependency the inner workings of a different class.  An example of a high coupling would be a Class A that got a temperature reading from a Thermostat class.  If Class A needed to be changed because the Thermostat class changed how it calculated the temperature, these two classes would be highly coupled.

Design patterns will help you avoid these types of anomalies and in the series of post following, I will go into detail describing each one, following the texts and examples from a favorite book of mine:  Head First: Design Patterns.

You can follow these series under this post tag: /tag/design-pattern-series/

Behavior-Driven Development (BDD) – The What, Why, and How

UPDATE:Incorporating BDD into Visual Studio/TFS? You will want to read this post.

The standard development method they teach you in Software Engineering 101 is

  1. Get your requirements
  2. Come up with a top-bottom architectural design
  3. Code up modules one by one
  4. Test (Unit/System/Regression)
  5. Delivery/Deployment

It’s a great approach and the solution you deliver can be built without compilation errors and work fine… or so you think.  You deliver the solution and all of the sudden, the client points out that there were requirements missed and/or incomplete.  My favorite taught-myself-in-24-hours-developer reaction to this is:

“But… it… compiles… therefore… it works…  it has to!”

With Behavior-Driven Development (BDD), and one of the reasons this approach is favored by QA teams all over the world is this approach ensures and, actually, requires that you work against the user requirements when working on the design.

Let’s say one of the requirements of an HR system being built is that the payroll must be able to use one pay rate when an employee has worked 40 hours or less and a different pay rate for overtime (> 40 hours).

With the top-down approach, you would eventually get down to developing this module, perhaps after the database development is done.  You also wouldn’t really be able to test it until the at least a part of the solution would  build successfully.

The What

With BDD, you tackle it from the requirements side, by writing tests on what the module should and should not do.  One of the great qualities of BDD also is that it stresses on readability.  This means that instead of test method names like TestOvertime(), you get method names like WhenMoreThan40Hours_UserShouldGetPaidOvertime().  That’s the first test.  Second could be something like WhenCalculatingOvertime_UserMustBeEligibleForOvertime().

Now, combine this with the strength of a mocking framework, and you might never go back to top-down.  If you have not heard of this before, mocking frameworks do not highlight your bad code and laugh at you.  In this context, it helps you mock or impersonate your dependencies.

In laymen’s terms, if you are testing a method that calculates hours for an employee and the information is supposed to come from a database, in this case, the database is a dependency.  If you attempt to connect to a database to get your test data and the connection or the query fails for some reason, your test may fail for the wrong reason.  A mock framework let’s you “mock” a call to the database and return test data.

I will get into code details and working with RhinoMocks in a post shortly following this one, but here is what some sample code may look like.

The How

//mock framework - RhinoMocks
var mock = new MockRepository();

//this creates a mock interface of the IDatabase interface
//you don't even need to implement the code
//for this interface in a class to use it here.
var myDatabase = mock.StrictMock<IDatabase>();

var employee = new Employee(myDatabase); //constructor takes in IDatabase object

using(mock.Record())
{
      Expect.Call(myDatabase.
                GetHoursWorkedForEmployeeID("123"))
                .Return(40);
}
using(mock.Playback())
{
     Assert.AreEqual(employee.IsOvertimeEligible,false);
}

The Why

By writing your unit tests first against your requirements, you end up building a system that is unit-test-ready and is very tight with the requirements.

In addition, this is also beneficial when working in a multi-developer team, by having your unit tests written, this ensures that when a developer goes into your module and changes functionality that may work for his module, but breaks yours, it won’t let him check in the solution, but this requires a bit of extra configuration work with MSBuild and Microsoft Team Foundation Server.

Every time you build a new method or make a change to the system, you can run your tests to make sure the new/modified functionality did not break any existing functionality.

In a nutshell, while this may seem like BDD would take longer, you should consider that time you spend on the dreaded back-and-forth with your QA team as they uncover your bugs and easter eggs can be virtually eliminated if you deliver a solution that unit tested itself at compile time or when you checked it into your source control repository.

User Agent Switcher for Firefox

I’ve recently had to develop some custom functionality for a web page that would only be visible if a GoogleBot was crawling the site.

The development part of it is pretty easy – you check for the HTTP User Agent with either an “if” or a “switch/case” statement and do your thing. Simple concept. Same for most languages, with just minor syntax differences.

So how do you test what the site will render when the GoogleBot pays it a visit without writing your own browser? I came across Chris Pederick’s User Agent Switcher for Firefox.

It lets you select which browser/agent you want to simulate right from Firefox’s drop down menu (screenshot below), which makes testing a breeze.

Check it out for yourself, as well as some of his other cool web dev tools here: http://chrispederick.com

User Agent Switcher for Firefox