How to use simulators during web development

Ski SimulatorThis is probably two topics that I would like to combine into one, as they are related in the context I will be writing about.

In one of the previous articles I wrote about the story wall that we evolved on our project. Christian in his article, described the way we worked not in pairs but in threesomes. Two developers and a QA. This was the way that involved iterative, small-steps, story delivery with constant showcasing to QA. QA was able to instantly check the correctness of a functionality, business logic, even a site layout and provide feedback to devs about it.

As any application (at least majority of them) our application has multiple points of integration to other systems. Database, web services, file system etc. One of the integration points was delivered some time ago and never tested, we were ready for a problems and bugs.

It would be very unwise to stop development because the part of the system needs fixing or some rework carried. We decided that we will shield our selfs with the layer of wrappers around third party systems , that we called Anti Corruption Layer.

The Layer gives as a constant API controlled by ourselves but it doesn’t mean that we can continue our way of working and constantly showcase all the acceptance criteria to our QAs and BAs.

We decided to bring on simulators on a board and hooking them into our anti corruption layer. This is how we achieved it:

Control

It would be very painful to switch simulator on or off using configuration in one of the file. Knowledge about the environment was not sufficient enough as we don’t want simulators to be on or off all the time. What we decided was to create a class called SimulatorDecider that will use two variables to determine if the Simulator should be on or off: current environment and a browser cookie.

The environment variable allowed us to switch simulators off regardless of the cookie, in any environment other than DEV or TEST.

Cookie in web browser is very simple to set and to remove. We created a little page called Cookie Monster that has a simple on/off buttons for setting and removing the Simulator cookie.
The approach gives a possibility to control and switch on/off different parts of the system by using different cookies for each parts.

Simulator

We have a bunch of wrappers around the integration points. The one that we are interested in, the one that we would like to simulate, we decorate with Configurable object and inject SimulatorDecider into it. This is how it works:


interface IFoo
{
  ReturnType DoStuff(ParameterType type);
}

public class Foo : IFoo
{
  public ReturnType DoStuff(ParameterType type)
  {
    // Doing some real stuff that is very important
  }
}

public class SimulatedFoo : IFoo
{
  public ReturnType DoStuff(ParameterType type)
  {
    // Doing some other stuff that is only SIMULATED
  }
}

public class ConfigurableFoo : IFoo
{
  private SimulatorDecider _simulatorDecider;

  public SimulatedFoo(SimulatorDecider simulatorDecider, IFoo realFoo, IFoo simulatedFoo)
  {
    _simulatorDecider = simulatorDecider;
  }
  public ReturnType DoStuff(ParameterType type)
  {
    if (_simulatorDecider.ShouldSimulate())
    {
      return _simulatedFoo.DoStuff(type);
    }
    return _realFoo.DoStuff(type);
  }
}

Because we are using dependency injection container (Yadic) we don’t need to worry about dependencies.
It is also possible to not code SimulatedFoo as separate type and just inline simulated behavior within the configurable type. We made this decision on a base of how complex the simulated behavior should be.

Hope you find this useful when you stuck on integration pice that you don’t know how to carry on 🙂
Comments are welcome as always 🙂

Greg

How to deal with nulls

This week I’ve been refactoring and improving some code in a .Net world. In last few weeks I also had some close encounters with Java as well. When looking at the code in both environments I realized that there is a lot of checking for null values on in a code. Where it is not a bad practice to check for null it actually presents another problem.

For someone like me when I was new to a code I couldn’t guess, what is the goal of that check. When you dealing with null the very important thing is to know where they came from and why it happened that null values occurred. When dealing with integration points it is even more important.

As you can guess code:

if (serverResponse == null)
{
    DoSomeStuff();
}

is not very descriptive. What actually happend when I got null, does it mean that there was a communication problem, error occurred or perhaps a null means that the operation was successful.

I think the best approach will be to avoid nulls if it is possible at all. Ways we could try to avoid it:
use Maybe pattern
• create Empty object values
• use Null Object pattern

Maybe pattern

I’m not sure if that is it’s appropriate name but I’ve used it on few projects and it seems like people are referring to it this way.

Have a Maybe interface and return it instead of the object itself.

interface Maybe<T>
{
    bool Hasvalue;
    T Value;
}

Also have two implementation of the interface:

public class Something<T>
{
    private readonly T _value;
    public Something(T value)
    {
        _value = value;
    }

    bool HasValue { get { return true; }  }
    T Value { get { return value; } }
}

public class Nothing<T>
{
    bool HasValue { get { return false; } }
    T Value { get { throw new InvalidOperationException(); } }
}

When returning value from an object return Maybe. Collaborator that consumes value can check if it is Something or Nothing or just ask if it has value.

Empty Object value

Just like string.Empty you can implement Empty or something similar as predefined value on your type. For example:

class Hen
{
    Egg LayEgg()
    {
        // doing stuff
        return Egg.Empty;
    }
}

class Egg
{
    public static Egg Empty = new Egg(“nothing in it”);
}

You can always check before the execution if the egg is empty.

Null object pattern

Object with no default behavior. For example:

interface TV
{
void ShowMovie();
}

class LCD : TV
{
    void ShowMovie()
    {
        // showing movie
    }
}

class Plasma : TV
{
    void ShowMovie()
    {
        // showing movie
    }
}

class NullTV : TV
{
    void ShowMovie()
    {
        // do nothing
    }
}

If something is not working well and we don’t have any of the TVs available we could return NullTV and nothing will happen.
Using the patter we can validate by type if the returned TV is a valid object and act accordingly.

I’m interested what other approaches people are heaving. Also what are the approaches in dynamic languages like Python or Ruby.

Please add your comment 😉

Greg

QCon summary (my presence on Thursday)

I got the opportunity to attend some of the sessions on Thursday QCon London 2010 conference.

Quite a large number of interesting tracks. It was really hard to choose which one to go to. I decide to throw in a mix of everything.

Kicked of with Christopher Reed, a colleague of mine, and his talk on different Cloud solutions out there, and also what are they good for. Chris was emphasizing the trend in the industry. A lot of companies deciding to go into cloud as if could could be a solution to all their IT problems. Chris was pointing out that it is important to make a proper decision what solution to go with as they are solving different problems and require different treat and behavior from the organization. It is impossible to get the most of the cloud and actually not get into more trouble if you don’t accommodate the fact that you are in different environment now. I liked how Chris was dividing cloud solutions into areas based on Processing Speed needed and Data Sensitivity where Processing Speed means how fast you wont your computation to be done and how complex they. Data Sensitivity meant the amount and data importance, for example crucial amount of financial transactions versus widely available weather information served by a web site.

Second talk I was at was from another colleague of mine that actually I never had the opportunity to hear speaking live, Rebecca Parson. Rebecca was talking about the ways that people not skilled in the domain can come out with some really great solutions. She encouraged diversity and cooperation in a teams that could lead to better solutions to any kind of problems.

After Rebecca’s session and a lunch break I went into a presentation by my former colleague, Dan North. I find Dan one of the best entertaining technical speakers there. His fascination about the ways the human brain learns makes his talks always very informative and fun.

Dan was talking about the way human brain tends to see patterns, generalize and then complexify solution as a result. Very often there is no common functionality and there is no emerging patter.
I liked one of the quotes he used in his presentation:

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.”
–Jamie Zawinski, 1997

Fishbowl discussion was last session. The topic was about the Future of Software development. It appears that there is emerging trend in the industry to follow the simplicity and go back to a roots of software development. Simple design and good algorithms over frameworks and bloated architecture. This trend appears to follow hardware development and new presence of mobile devices on a market.

Overall very interesting day. Pity I couldn’t make to all the sessions as they were all interesting 🙂 Hope my colleagues who were at different presentations could share their views and notes if possible.

Greg

What is your favorite development platform, survey results

Not to long ago I posted an article on my favorite environment for software development. There was also a survey in that post asking about your favorite environment. The results are below.

Clearly, Linux is a winner. Here are some comments you guys added to a survey.

Can’t believe people are still using text editors to code… Visual studio rocks!

Well, not sure about this comment 🙂

Well, Ubuntu. For the package system, for the standard package archives and for the PPAs. And for everything else about it being good enough.

Windows 7, mainly because ive been using windows for, well ever. I find it really intuitive and i just know the tools really well. Windows 7 is really nice too.

Obviously I long for the day that will never come when I can develop my .net applications in intellij, deploy to a cut down windows vm for compilation and testing against iis… Anyway dreaming aside I pick mac osx for reasons you said and also because I have one and nothing beats dev on a native machine…

Mac is quite reasonable. Windows just feels clunky.

I need to be able to navigate to files and find things quickly, let alone use apps. Windows explorer/cmd is crap. Linux and Mac OS both work quickly and the command-line is effective. Mac OS X, however, just requires less of my time to maintain it than Linux. All three work similar from within an app like Eclipse.

Could be interpreted as a fan oy phrase, but for dev there is no better option than Linux…

I prefer Max because when I develop I am not just programming. Sometimes I need to check some screencast and last time that I used Linux (last year) I was not able to check all of them. Also I need a good mail and contact manager, I tried Thunderbird and Evolution. But none of them where satisfying my need.
Also i use my computer to develop and for personal use (creating video, music viewing videos). Linux is poor on that side.

I still have to fall back to Windows when working with clients who insist on using Outlook for calendaring and those horrible built-in surveys. Some aspects of my MacBook are pleasant, but I detest the keyboard layout – no home/end/pgup/pgdown and, worst of all, no visible # key 😦

We all got a favorite environment, but what is the actual one that you use every day at work? I’m using Visual Studio as there is no real aternative to .NET development. Fortunatelly ReSharper makes it usable 🙂

Thanks for your answers guys, Greg

Windows, Mac or Linux? Flavours of development environments

After a few months of using Mac as a playground and development machine I took a little step back to remind myself of all different environments/systems/platforms I was using in my short development carrier. I know what you think, that I will say Mac is the best. But it actually it isn’t. At least not for me. Why?

Linux

I used Linux in my early days of development. I was coding on practically knitted together pice of hardware with free operating system. I was using Mandrake and Suse, switching between them constantly . After few years I switched to Ubuntu. Using Linux as development platform was harsh and raw when I started. After few ups and downs I got to grips with it and it server well as platform for PHP, Java and Ruby development. With Ubuntu it was extremely easy to install new software and be up to date. I never managed to crash it (no blue screens of death).

There was one quite big issue with it though, lack of .Net development environment. I have to mention as well problems with file formats in the every day office life.

Windows

The only environment that I could develop on targeting Windows environment as runtime. With .Net and C# Windows became bearable and useful. I didn’t have problems with office documents compatibility anymore. It was possible to set it up with Unix like command line tools and possible to script and automate a lot of build/setup tasks. There was one very big flow in the setup. After few days of usage, somehow Windows manage always to crap itself with plenty of junk and gets very slow. The most common solution for that problem was … reinstall.

Mac

Very shiny and friendly. I love the 4 fingers swam and expose. It has Unix/Linux command line out of the box so I don’t have to install anything for that. Comes with a bunch of tools for development. Unfortunately most of them are for Mac development.

Once you would like to get a latest version of Java, Ruby or Python, it is impossible to simply change or upgrade the version as parts of the system (some OSX applications) depends on specific version. So … I was in a hassle to build it up for what I wanted it to be. Now when it’s ready, it rocks. It would be very cool if some of the task that needs to be done in order to make it work I had to google and spent some time on it.

So … for me the best platform is …. LINUX, TADAAA. Simple and to the point on installing different environments. Huge community with plenty of tips, easy to find. With OpenOffice.org and Google Docs pain of documents incompatibility disappears.

I guess for me the winner is the one that gets me up and running in no time and is very easy and Agile when I want/need the change.

What’s your favorite Platform? What do you like and what dislike. Please take a moment to answer this short survey or just post a comment 🙂 I will post the results and survey findings in next few weeks.

Survey closed

Greg