Wednesday, September 05, 2007

Installing simple_helpful without edge Rails

DHH has helpfully moved the simply_helpful Ruby on Rails plugin.
If you are not running edge you can still install, you just have to use:

script/plugin install http://dev.rubyonrails.org/svn/rails/plugins/legacy/simply_helpful/

Tuesday, August 14, 2007

Tip:#2 Smell: Duplicate Tests Indicate a Missing Class

Smell: A class with two public methods on it, both perform the same functionality, or one is a subset of the other. The existence of a private method is a good indication of this.

To fully test these public methods you have to repeat a bunch of tests. There has to be a lazier simpler solution.

Recently I have been getting this smell a lot using MVC in a .Net winforms app.

.Net forms are hard to unit test. It is therefore helpful to keep your Views as thin as possible. Just use them to expose the form fields as a bunch of properties and to catch events and call the corresponding method on the controller. This moves the logic to the Controller where it is far more testable.

This can however lead to methods on the controller like "OnStartButtonClicked" and "OnStartMenuItemSelected". Both are going to perform the same actions. Both are going to need the same set of tests.

The solution is simple. Use the 'Extract Class' refactoring to pull the private method out to another class and use Dependency Injection to pass an instance of this new class back to the original class.

Following this above advice though you end up with another object. In my experience this split makes a lot of sense. I call this new class a Service. I rename the new thinner Controller to Presenter which better resembles it's remaining responsibilities.

(Note: I've read a few things on the differences between MVC and MVP, but I don't really get it. What I have here may be what is meant by the naming. Either way I like this design better.)

I'm really liking the new code. The view is really thin. The presenter translates UI events to service calls, and knows which views to update when the domain changes.

A piece of advice Steve Hayes often gives is "Design your UI layer so you could replace it with a command line and everything would keep working". I have struggled to do this with .Net, even with MVC. Having this new split however I can see keeping the Service and Model layers and replacing the UI would be easy.

So... Look out for duplicates tests... get lazy and write better code!

Friday, July 06, 2007

Tip:#1 Testing Events in C#

If you have a class that causes a .Net event to fire. When writing a test for the event, you can add a handler to the event that sets a flag that you then assert on in your code. Using normal delegates means the variable would need to be a class member and get initialized in Setup. Use of anonymous delegates cleans this up nicely.

[Test]
public void TriggerEvent_CausesEventThatFiresToFire()
{
Customer customer = new Customer("Ben");
string changedPropertyName = null;

customer.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
{changedPropertyName = args.PropertyName;};

customer.Name = "Kate";

Assert.AreEqual("Kate",changedPropertyName);
}

Thursday, June 28, 2007

Design for unit testing

Object oriented languages allow lots of ways to solve the same problem. As you get better at design you see some designs as good [loose coupling, high cohesion]. You can view your design skills as a set of filters you use to choose which design to pick.

Doing TDD leads you to learning some new filters. If a design is not testable it is not a valid design. I want to write an article on this.. but for now check this out: http://www.codeproject.com/useritems/DesignPatternsForUnitTest.asp

Monday, May 21, 2007

SVN vs ClearCase

At a current client we just swapped over from using ClearCase and ClearQuest to using SVN with clear check in comments within our team and updating the ClearCase repository daily.

The change in productivity is striking. One team member mentioned today that he felt he was saving around 2 hours a day. If that's accurate then on a team of 8 people it's equivalent to freeing up 2 people every day.

It's not just the time spent waiting for version control, but those pauses people make to 'quickly' grab a drink or check their email so the pair are not there when the tool does finish. Not only that, everyone is having more fun too. No one likes waiting for hour glasses. Everyone likes feeling more productive.

Are any steps in your processes disrupting the flow of your work? Can a change of tools help?

The key is to work out the features of the current process that are actually require and which are just nice to have.

GitHub Projects