Thursday, February 26, 2009

Fluent language in FsStory

A nice feature for a story runner is to be able to provide arguments in a story sentence, like this:

ATableWithNumberOfLegs 4

This example is not hard to understand, but there's some mental translation going on, since the order of the words is all screwed up. Let's try another one:

TheNumberOfLegsOfATableIs 4

Better, but still not good. First, it's longer than the previous example. Second, even if the grammar is correct, the word order is, well, unusual.. ;)

What about this?

ATableWith 4 Legs

Now we're talking!

Here's a bigger and more complete example:

[<Fact>]
let tableLegsScenario =
given (ATableWith 4 Legs)
|> whens (ICutOf 1 Leg)
|> thens (ItHasOnly 3 LegsLeft)


Note: As far as I know, RSpec/Cucumber is the only story runner(s) that is able to use variables inside a story sentence.

The nice thing about this is that I didn't have to change FsStory itself to make it work. Not a single line. So, what's the trick?

If you split up a story sentence like this, you have to prepare the step definition code (the behind-the-scenes code) in a certain way:

let ATableWith = fun n _ -> ... do something here ...

..or if you prefer, without a lambda:

let ATableWith n _ = ... do something here ...

Then you have to define Legs:

let Legs = id

As you see, in this case, it was just a matter of discovering the usage, rather than implementing it in the language. Honestly, I had no idea of this usage when I started to write on FsStory. This is clearly one of the reasons why I like internal DSLs!

Exercise: How would you implement the step definition for the following story sentence?

given (ATableWith 4 LegsAnd 2 Chairs)

Cheers!

Sunday, February 22, 2009

ImpossibleEstimation and Pomodoro Technique

Sometimes when using Pomodoro Technique I find it real difficult to estimate how long a particular activity will take, i.e., when locating a bug or find out why the webserver won't read my files.

In Pomodoro Technique, every activity should have a time estimate - how many Pomodori I think it will take. Though, this is sometimes impossible! "The problem is solved when I find the bug and since I don't know what the bug is related to, it's impossible to say how much time it will take to find it."

The solution: instead of just writing a number besides the activity, I use the less-than sign (<) before the number, indicating that I have a time-box for the activity, but that it might take less time. If I'm not done when the time-box is over, I have to ask a colleague to help me or ask my boss for extra resources - thus, escalating my problem. Then, I'm forced to have collected some data of the problem to help them to help me. The nice thing is that I still can have most of the benefits Pomodoro Technique gives me, i.e., increased focus when in a Pomodoro and possibility to get "the whole picture" during my breaks. The latter have proved to be an extra nice thing to have during hard problem-solving.

And, if a colleague comes to the rescue, we can construct another time-box, to know when to escalate it further, or at least to notify the team or the boss that we have some nasty problems at hand.

Yet, if I'm collecting metrics of my estimation skills, it is not a very good idea to track data for these bug-fixing Pomodori estimates. Put a "N/A" or a "-" in your records sheet and think for yourself: "Today was an exception, tomorrow will be a bug-free day." And don't forget to do your daily mind-map before you leave for home.

Monday, February 9, 2009

[Announce] FsStory, executable stories in F#

Since Claudio Perrone's talk on Øredev, I have been thinking about what his MisBehave would look like in F#. In his talk, Claudio also mentioned Cucumber, a story runner written in Ruby. My plan was to make a lightweight DSL for writing stories in F# code, with the story parts separated from the implementation parts.

Currently, FsStory enables the developer to write user story scenarios (in Given/When/Then form) in F# code, like this:
#light

open FsStoryRunner
open MutatedTurtleMovesImpl
open Xunit

(*
In order to impress my friends
As a .NET programmer
I want to draw funny fractal pictures
*)

[<fact>]
let MoveTurtleToPosition() =
given ATurtle
|> andGiven IsRotated90DegreesToTheRight
|> whens (TurtleWalksSteps 9)
|> thens (TurtleIsLocatedAt (0,9))
|> endStory

Did you notice [<fact>] attribute just before the function definition? It is a xUnit.net specific attribute, telling xUnit.net that the function is a runnable test. So, why xUnit.net? Answer: xUnit.net is the currently the only test framework that runs static test methods, which is what F# functions compiles to.

Note: If you think that the story above is too low level to be a "good" user story, you're right, but it's just an example..

The "ATurtle", "IsRotated90DegreesToTheRight", "TurtleWalksSteps", etc, are functions that you have to implement yourself. What these functions do is not FsStory's business, except that they have the same type. It's a good thing to think about this in advance.

If you're testing something object oriented, i.e. a C# project, then you're probably have to let the functions have the type () -> (). That is, they take no argument and return void, in C# lingo. You'd also need a mutable variable to accomplish this.
#light

open Turtle
open FsxUnit.Syntax

let mutable turtle = new Turtle() // turtle must have type Turtle

let ATurtle () = turtle <- new Turtle() // For reuse in same story let MovesOneStepForward () = turtle.Go() let IsMovedOneStepForward () = turtle.Position.X |> should equal 1 let RotationIs angle () = turtle.Direction |> should equal 0.0
It's up to the developer what library she wants to use for her assertions. In this example, FsTest was used, but she could go for NUnit or NBehave or something else. I hadn't actually tried this and do not longer think this will work. Either use xUnit.net or FsTest (which is based on xUnit.net).

Another style is to work with immutable objects. One example of immutable objects are value objects, in DDD. Immutable objects correspond well to functional programming principles. Here is an example of an implementation of a scenario when an immutable object is used in the SUT (System Under Test).
let ATurtle () = new TurtleImmutable()
let IsRotated90DegreesToTheRight = fun (turtle : TurtleImmutable) -> turtle.Left()
let TurtleWalksSteps steps = fun (turtle : TurtleImmutable) -> turtle.GoSteps(steps)
let TurtleIsLocatedAt (x,y) = fun (turtle : TurtleImmutable) -> turtle.Position |>
should equal (new Position(x,y)) ; turtle

To clarify, all methods on the (immutable) turtle return a new turtle and that turtle is returned and then passed in as an argument to the next test function (by FsStory). As you might have spotted, the example uses a lambda, an anonymous functions (the "fun") instead of specifying an argument explicitly. It's a good thing to get a running story before actually implementing the logic and assertions. Using the function "id" (just returning the argument) on the right-hand side is very helpful for getting everything to run.

You can find FsStory at http://www.codeplex.com/fsstory.

Friday, February 6, 2009

Trying out ruby for the very first time

So, I've made up my mind. The language of this year is going to be Ruby! The reasons are many, but mostly I feel I can use this language more in my daily work, and on a podcast over at AltDotNet about Ruby, they mentioned something like "learning ruby will make you a better C# developer". Languages like lisp and python will have to wait for at least one year!

About two weeks ago, I was out skiing with Gustaf and some friends of him. We ended up playing a lot of four in a row, a really funny and simple game. This game made me think about how to develop a min max AI, and suddenly I had written down the pseudo code for such an AI in my notebook. Here is a picture of the game is supposed to be played (from wikipedia):
Now, back home at my computer, I started digging into the ruby documentations, and after not more than a few hours I had finished the first version of the AI! Turns out Ruby isn't that hard to learn, at least not if you just want to get some basic things done. It's like writing c#, except you skip the types, and instead of writing void foo(int x) { ... } you write def foo(x) ... end, and so on...

Here is how my final version of the game looks when you run it:

C:\Documents and Settings\Christian\Desktop>ruby 4irad.rb
Run against ai (A), another player (P) or let two AIs play against eachother (X)
A
Enter AI 1 level (1 to 5):
3
Enter name of human player (default Human 1):

---------------
|0 1 2 3 4 5 6|
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
---------------
AI 1 (3) played: 3
---------------
|0 1 2 3 4 5 6|
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |O| | | |
---------------
Human 1, make your move:
2
Human 1 played: 2
---------------
|0 1 2 3 4 5 6|
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | |X|O| | | |
---------------
AI 1 (3) played: 3
---------------
|0 1 2 3 4 5 6|
---------------
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |O| | | |
| | |X|O| | | |
---------------
Human 1, make your move:

and so on...

I wonder how much code can you post in a blog, without the post becoming too long? I would like to post the code as an attachment, but I'm not sure you can do that in blogger, so I'll simply put it here. To try it out, copy the code to a file, i.e. 4inarow.rb, then run it using ruby 4inarow.rb. Enjoy ;)
$debugEnabled = false
def debug(text)
 if $debugEnabled then
  puts ">>> " + text
 end
end

class State

 attr_reader :cols, :rows, :player, :winner, :lastPlayed, :isFull
 
 Player1 = "Player 1"
 Player2 = "Player 2"

 def initialize
  @cols = []
  (0..6).each { @cols << [] }
  @rows = []
  (0..5).each { @rows << Array.new(7, nil) }
  @player = Player1
  @winner = nil
  @isFull = false
 end
 
 def deep_copy
  return Marshal.load( Marshal.dump( self ) )
 end
 
 def to_s
  return "---------------\n" + 
   "|" + (0..6).to_a.join(" ") + "|\n" +
   "---------------\n" +
   @rows.reverse.collect{|row| "|" + row.collect{|r| if !r then " " elsif r == Player2 then "X" else "O" end}.join("|") + "|"}.join("\n") + "\n" +
   "---------------"
 end

 def canPlay( i )
  return @winner == nil && @cols[i].length < 6
 end

 def play( i )
  if canPlay i then
   rowIndex = @cols[i].length
   @cols[i] << @player
   @rows[rowIndex][i] = @player
   @lastPlayed = i

   switchPlayer
   updateWinner
   updateIsFull
  end
 end
 
 def updateIsFull
  for p in @rows[5]
   if !p then return end
  end
  @isFull = true
 end

 def findWinner( row )
  for i in 0..(row.length-4)
   p = nil
   n = 0
   for j in 0..3
    rp = getPlayed(row[i+j])
    if rp.class != Fixnum then
     if !p then
      p = rp
     elsif p != rp
      p = 0
      break
     end
     if rp != 0 then
      n += 1
     end
    end
   end
   if p != 0 and n == 4 then
    return p
   end
  end
  return nil
 end

 def updateWinner
  for row in getAllRanges
   p = findWinner row
   if p then
    @winner = p
   end
  end
 end

 def switchPlayer
  if @player == Player1 then
   @player = Player2
  else
   @player = Player1
  end
 end

 def getPlayed(cell)
  c = cell[0]
  r = cell[1]
  col = @cols[c]
  if r >= col.length then
   return r - col.length + 1
  end
  return @cols[c][r]
 end

 def posValid( c, r )
  return c >= 0 && c <= 6 && r >= 0 && r <= 5
 end

 def getDiagonal( c, r, dc )
  d = []
  while posValid( c, r )
   d << [c, r] 
   c += dc
   r += 1
  end
  return d
 end

 def getAllDiagonalsRanges
  diags = []
  for r in 0..5
   diags << getDiagonal( 0, r, 1 )
  end
  for c in 1..6
   diags << getDiagonal( c, 0, 1 )
  end
  for r in 0..5
   diags << getDiagonal( 6, r, -1 )
  end
  for c in 0..5
   diags << getDiagonal( c, 0, -1 )
  end
  return diags
 end
 
 def getAllColsRanges
  return (0..6).collect{|c| (0..5).collect{|r| [c, r]}}
 end
 def getAllRowsRanges
  return (0..5).collect{|r| (0..6).collect{|c| [c, r]}}
 end
 
 def getAllRanges
  return  getAllColsRanges + 
    getAllRowsRanges + 
    getAllDiagonalsRanges
 end
end

class Node

 attr_reader :state, :children, :isLeaf

 def initialize( state, player )
  if !player then
   raise "player is nil"
  end
  
  @state = state
  @isLeaf = true
  @children = {}
  @player = player
 end
 
 def getPoints( row )
  points = 0.0
  for i in 0..(row.length-4)
   p = nil
   n = 0
   divider = 1
   for j in 0..3
    rp = @state.getPlayed(row[i+j])
    if rp.class == Fixnum then
     divider *= rp
    else
     if !p then
      p = rp
     elsif !rp then
     
     elsif p != rp
      p = nil
      break
     end
     if rp then
      n += 1
     end
    end
   end
   if p then
    blockPoints = calculatePoints n / divider
    if p == @player then
     points += blockPoints
    else
     points = points - blockPoints
    end
   end
  end
  if points.infinite? then
   debug "Found winner: " + points.to_s + ": " + row.join("|")
  end
  return points
 end

 def calculatePoints( n )
  return n.to_f**3 / (4 - [n,4].min)
 end

 def nodePoints
  points = 0.0
  for row in state.getAllRanges
   p = getPoints(row)
   if p != 0 then
    #debug row.collect{|r| if r then r else " " end}.join("|") + " " + p.to_s
   end
   
   newPoints = points + p
   if newPoints.nan? then
    raise "points + p == NaN: points=" + points.to_s + " p=" + p.to_s
   end
   points = newPoints
  end
  #puts state
  #puts "Points: " + points.to_s
  return points
 end

 def allChildPoints
  return children.values.collect {|child| child.totalPoints}
 end

 def isMyTurn
  return state.player == @player
 end

 def totalPoints
  if @totalPoints then
   return @totalPoints
  end
  p = nil
  if @isLeaf then
   p = nodePoints
  else
   if isMyTurn then
    p = allChildPoints.max
   else
    p = allChildPoints.min
   end
  end
  if !p then
   raise "nil points returned"
  end
  @totalPoints = p
  return p
 end

 def grow
  if @totalPoints then
   #puts "Gah, totalPoints is set"
   @totalPoints = nil
  end
  if @isLeaf && !@state.winner
   #debug "Growing leaf: " + self.to_s
   for i in 0..6
    if @state.canPlay i then
     childState = @state.deep_copy
     childState.play i
     child = Node.new(childState, @player)
     children[i] = child
    end
   end
   @isLeaf = false
  else
   for child in @children.values
    child.grow
   end
  end
 end

 def findNodeWithState( state )
  if state.lastPlayed then
   return @children[state.lastPlayed]
  else
   return nil
  end
 end
 
 def to_s
  return "Played: " + @state.lastPlayed.to_s + " Points: " + totalPoints.to_s
 end
end

class AIPlayer

 def initialize( level, name )
  @level = level
  @name = name
 end

 def to_s
  return @name + " (" + @level.to_s + ")"
 end

 def selectBestNode
  # Add some randomness
  bestChild = nil
  bestChildCount = nil
  for child in @node.children.values
   #puts child.state
   #puts child.totalPoints
   if !bestChild then
    bestChild = child
    bestChildCount = 1
   else
    if child.totalPoints > bestChild.totalPoints then
     bestChild = child
     bestChildCount = 1
    elsif child.totalPoints == bestChild.totalPoints then
     bestChildCount = bestChildCount + 1
     if rand(bestChildCount) == 0 then
      bestChild = child
     end
    end
   end
  end
  
  #Just to make sure
  if !bestChild then
   raise "bestChild is nil"
  end
  
  debug "Best points: " + bestChild.totalPoints.to_s
  
  
  @node = bestChild
 end

 def play(state)
  if state.lastPlayed then
   debug "Finding last played node"
   @node = @node.findNodeWithState(state)
   @node.grow
  end
  debug "Finding the best node"
  selectBestNode
  @node.grow
  state.play @node.state.lastPlayed
 end
 
 def setupGame(state, player)
  @node = Node.new(state, player)
  debug "Creating min max tree (" + (7**@level).to_s + " nodes)..."
  (1..@level).each{ @node.grow } 
 end

end

class HumanPlayer

 def initialize(name)
  @name = name
 end

 def to_s
  return @name
 end

 def play(state)
  puts @name + ", make your move:"
  
  begin
   toPlay = gets
   if toPlay == "q\n" then
    exit
   end
   toPlay = toPlay.to_i
   if !state.canPlay toPlay
    puts "Can't play " + toPlay.to_s + ". Select another:"
    selectOther = true
   else
    state.play toPlay
   end
  end while selectOther
 end
 
 def setupGame(state, player)
  
 end
end

def runGame

 puts "Run against ai (A), another player (P) or let two AIs play against eachother (X)?"
 gameType = readline.strip
 case gameType
  when "A", "a"
   player1 = createAIPlayer
   player2 = createHumanPlayer
   
  when "P", "p"
   player1 = createHumanPlayer
   player2 = createHumanPlayer
   
  when "X", "x"
   player1 = createAIPlayer
   player2 = createAIPlayer
   
  else
   puts "Invalid input: #{gameType}"
   exit
 end

 players = [player1, player2]
 state = State.new
 
 player1.setupGame(state, State::Player1)
 player2.setupGame(state, State::Player2)

 while (!state.winner && !state.isFull)
  for player in players
   puts state
   player.play(state)
   puts player.to_s + " played: " + state.lastPlayed.to_s
   if state.winner then break end
  end
 end

 if state.winner
  puts state
  puts "Winner: " + state.winner.to_s
 elsif state.isFull
  puts state
  puts "Draw!"
 end
end

$aiCounter = 0
def createAIPlayer
 $aiCounter = $aiCounter + 1
 name = "AI " + $aiCounter.to_s
 puts "Enter " + name + " level (1 to 5):"
 level = readline.to_i
 return AIPlayer.new(level, name)
end

$humanCounter = 0
def createHumanPlayer
 $humanCounter = $humanCounter + 1
 defaultName = "Human " + $humanCounter.to_s
 puts "Enter name of human player (default " + defaultName + "):"
 name = readline.strip
 if !name || name == "" then
  name = defaultName
 end
 return HumanPlayer.new(name)
end

def runTests

 state = State.new
 
 state.play(3)
 state.play(3)
 state.play(4)
 state.play(4)
 state.play(5)
 state.play(5)
 state.play(6)

 #test getPlayed
 if state.getPlayed([3, 0]) != State::Player1 then raise "getPlayed doesn't work" end
 if state.getPlayed([3, 1]) != State::Player2 then raise "getPlayed doesn't work" end
 if state.getPlayed([3, 2]) != 1 then raise "getPlayed doesn't work" end
 if state.getPlayed([3, 2]).class != Fixnum then raise "getPlayed doesn't work" end

 #test findWinner
 winningRow = (3..6).collect{|c| [c, 0]}
 if !state.findWinner(winningRow) then raise "findWinner doesn't work. winningRow=" + winningRow.to_s end
 
 #test state.winner
 if state.winner == nil then raise "winner should be set" end
  
 puts "All tests ok"

end

runGame
#runTests

Monday, February 2, 2009

BDD using NBehave + Rhino Mocks AMC

Update: I found an old blog post by Aslak Hellesøy (the main developer behind Cucumber) that touches on this subject.

Some time ago, I investigated what BDD is all about. In essence, it's TDD with a twist. For example, the word "test" implies that we're testing what someone (we?) already made, but TDD says we're going to write the tests before the actual implementation of the unit of code. Somewhat, the word "test" has a direction backwards, while "should" has a direction forward. Hence, "should" is more comprehensive to use when describing and specifying the future. Makes sense?

What I'd like to show you is a piece of code I wrote to see how NBehave's story runner could work with Rhino Mocks' Auto Mocking Container[1, 2].

[Story, Test]
public void GetMoneyWhenPassGO()
{
 // Set up and initialize
 var mocks = new MockRepository();
 var container = new Rhino.Testing.AutoMocking.AutoMockingContainer(mocks);
 container.Initialize();

 // Resolve and obtain references
 IPlayerTurn turn = container.Create<PlayerTurn>();
 IBoard board = container.Resolve<IBoard>();
 IPlayer player = container.Resolve<IPlayer>();
 turn.AddPlayers(new List<Player>() {player});

 // Story begins here
 var story = new Story("Player recieves money when passes 'GO'");

 story
     .AsA("Player")
     .IWant("to recieve money when I pass 'GO'")
     .SoThat("I can buy things that generate money");

 story
     .WithScenario("Normal play scenario")
     .Given("A board with 4 squares", () => Expect.Call(board.NumberOfSquares).Return(4))
     .And("a player near 'GO'", () => Expect.Call(board.GetIndexForPlayer(player)).Return(2))     
     .And("mockery has started", () => mocks.ReplayAll())
     .When("player passes 'GO'",() => turn.PlayerSequence(player, 3))
     .Then("the player earns $4000", () => player.AssertWasCalled(x => x.Credit(4000)));

}

When this test is run with ReSharper, the test passes and outputs the story with indentation. Nice! (Except the "mockery has started" part of the story..)

Now a question pops up: since we have only specified and tested the first (top-level) interaction, what about the rest of the interactions? I think this is a good question that leads us further down the rabbit hole.

A written user story comes from a dialogue with a person with domain knowledge. Hopefully, after some discussion, we have understood some of the moving parts of the domain problem the customer wants us to solve. Let's assume that we want to implement a single feature at a time, a couple of questions arise: should we start top-down or bottom-up? And how far "up" should we go, i.e. should we start with the UI or the domain model, if we choose a top-down approach?

If we choose to start with the domain model, then I think the above way of specifying the behavior looks nice. The key question is where the classes in the story come from originally. I have no easy answer for that. Of course they should originate from the domain problem, but how? "The model is the code - the code is the model", but it probably takes a while to "get it right". Maybe code like the above could help us to see if we have understood the problem in the first place?

Now back to the question: "what about the rest of the interactions"? We have ensured that the class which is in "the center" of the particular interaction chain (the player turn) lives in a faked world (a small board and a player near go) and we finally assert that when something happens (player passes 'GO') then some state has changed (the player gets money). The nice thing is that we now know more about what functionality the dependent classes should provide. For example, IBoard needs to have a method GetIndexForPlayer and if there is a class implementing that interface, then a NotImplementedException is probably thrown from that method, in order to compile. Next step could be to start thinking on that particular method and choose to either write a mocked unit test or an "ordinary" unit test.

Of course, real acceptance tests are also needed, but the purpose and scope of those tests are quite different. At least that's what I think.

What do you think?