Pattern matching is a powerful and amazing characteristic of F#. Actually, is so amazing that Microsoft is starting to port it to C#.

There are different kinds of pattern matching. In this post we’re going to take a look at a partial classification active pattern that takes an argument and returns a value.

As its name denotes, is a pattern that partially classificates what you match with it. That means that doesn’t try to define all possible options but just one. This is quite convenient when using an active pattern to define a business rule of your domain, because you can split the different cases in different partial active patterns making your code much more readable.

You will identify a partial active pattern because the definition looks something like this:

let (|WithRemainder|_|) divisor divident =
    // active pattern code
You can see between brackets the name of the classification followed by the wilcard name (_), the two of them between . Just after that you can see the two arguments that the function will take.

This kind of active pattern has to return an Option value. In our case, we are going to return the remainder, if any. So our active pattern will look like:

let (|WithRemainder|_|) divisor dividend =
    let remainder = dividend % divisor
    if remainder <> 0 then Some remainder else None

How we use it? Well, the first time you see this active pattern applied can seem a bit difficult to understand (at least it was for me! :P), but is not that difficult:

match 10 with
| WithRemainder 3 r -> printfn "The remainder is %d" r
| _ -> printfn "No remainder"

The difficult part here is to see that the value in the match sentence is passed as the last argument of the active pattern. For example, in our case 3 is the divisor (first argument), 10 is the dividend (second argument) and r is what the pattern returns inside the Option type, in this case the remainder (is not the Option type, is the value itself). Once you understand that, there will be no secrets for you to create and use this kind of active pattern!

When you have some unit tests developed using NUnit 2.x your FAKE script looks like something like this:

Target "RunUnitTests" (fun _ ->
    !! (testDir + "/*.Tests.dll")
    |> NUnit (fun p ->
          {p with ToolPath = "packages/NUnit.Runners/tools/"}) )

But NUnit3 works slightly different. Instead of having a single NUnit.Runners package, that package references some other packages (runner, extensions, etc). One of those packages is NUnit.ConsoleRunner that has the exe inside the tools folder.

But changing the ToolPath in the FAKE task is not enough. You should use the new NUnit3 action, which is inside the Fake.Testing module. So, we need to open the module and change the task definition:

let nunitRunnerPath = "packages/NUnit.ConsoleRunner/tools/nunit3-console.exe"

Target "RunUnitTests" (fun _ ->
    !! (testDir + "/*.Tests.dll")
    |> NUnit3 (fun p ->
        {p with ToolPath = nunitRunnerPath}) )

And hopefully when you run the FAKE script you’ll have something like this:

NUnit3 Results

In my current project we had a hard deadline: we had to go to public beta on August and finish the transition of all users to the new system by the end of August. When we knew those dates our first reaction was this one:

Deadinle!

Depending on the environment you work the reaction to these news are usually some combination of a lot of pressure from management, working a lot of hours, drop the quality of your code, hysteria, etc.

To be honest, we had some of that, but the main reaction was the right one: scope management. If you have a deadline, and assuming that hiring new people won’t ease your life, your last chance to success is to manage the scope of your project. Decide what is really really really important, what the users can’t live without and implement that. And nothing more. We conducted some workshops to do this work (the whole team, not only the product owner and business analyst) and the result was a reasonable product backlog to develop.

Apart from that, the team increased the commitment with the quality of his code. We increased the time doing pair programming, we did some mob programming for some difficult issues, we did lots of code reviews.

I’m not going to lie you and say that we were pleased to have such a hard deadline, but I think some of the consequences of having it have been really positive for the final product. Is far better to have an incomplete product that your users can use and give feedback than a super fancy product that nobody use.

A couple of weeks ago I saw these tweets (in Spanish):

As I respect enormously Modesto and Xabi’s opinion and I trust on the skills of a Carlos Ble’s apprentice, I decided to do the kata at home. And as I keep trying to learn F#, that would be the chosen language.

I recommend you to do the kata, is a very interesting one. In case you want to do it, please stop reading now and come back when you have finished it, as I’m going to explain how I solved it.

Spoiler alert

The kata starts pretty simple: you have to model a combat system between two characters of a RPG game. First you start with simple rules and in every iteration you add a slightly more complex one. At the end of the 4th iteration I ended up with a code like this:

As you can see, I have a list of rules that should be applied when attacking and a list of rules that should be applied when healing. I have a function to traverse a list of rules and apply them, and the rules are quite small and focused in one thing. I was quite happy with the design (remember, I’m learning Functional Programming, I can be totally wrong :) )

The real fun starts in the last iteration. In this iteration a new element of the game is introduced: a Thing. A Thing is an element that has health but nothing else. It can’t heal anyone, it doesn’t belong to any faction and it can’t attack. Is just something that everybody can attack.

Looks like a simple change but introduces quite a bit of complexity in the design. I decided to use a discriminated union to model a Player, so that a player can be either a Character of a Thing.

My first attempt to change the logic of the module was focused in two things: - Simplify the logic of the different rules, so that they just only need as a parameters the information they will use. - Put the logic around being a Thing or a Character in the main function

This solution ended up with a function like this:

Full code

As you can see, the code is a bit messy. It’s true that the rules now are a bit simpler and only receive as a parameter what they need, but on the other hand we have now three different types of rules that must be traversed in the main function.

So I decided to refactor my solution a bit. What I tried to accomplish was: - Move the logic around being a Thing or a Character to the rules. Now a rule is the responsible of knowing what to do in each case. - Simplify the logic of the main function. Now, it just differentiate about an Attack and a Heal, calls the same function to traverse the list of rules (different in each case), and applies the result in a different way.

Here’s the code of the main function:

And here’s the code of a couple of rules:

I prefer this approximation. I don’t have a nested pattern matching in the main function. I only have a list of rules for each case and the rules know what to do in each case. My concerns are: - What will happen if the types of players grow? Is there any way to generalise the types (i.e. a player that could be attacked, healed, etc)? - What will happen in the number of parameters of the rules grow? Is a smell that the design is not good enough? Should I introduce a parameter object (well, in F# maybe is a parameter record type :P )?

I’d love to hear your thoughts about the design :)

In the last days I stumbled upon some articles about how to improve, be more productive, etc. Usually you can separate this kind of articles in two categories: - the articles that talk about non work-related activities you must do if you want to increase your productivity. Eat the right food, sleep the right amount of time, do exercise, etc. - the articles that talk about work-related activities. Go to meetups, read, do pet projects, etc.

As many activities (if not all) we do as a human beings, the success of a technique always depend on many factors, being us the most important one. The things that work for me maybe don’t work for you.

Both kind of articles talks about valuable things that you should definitively do but taking them as the only source of truth can lead you to a difficult position. Let me explain my feelings with a couple of examples.

Until one month ago I was working in Coventry (I live in London) from 9 to 17. That means that I had to wake up at 6:15 and be back at home at 18:45, just in time to give my son a bath, have dinner and put my son to sleep. As I’m not a super-human I need to sleep at least 7 hours (to me the right amount of hours to sleep is 8 ), so basically I had one hour to do “other things”. Yes, I could have made a thorough study plan, but I also like to do strange things like talking to my wife, watch Game of Thrones, read a fiction book or play a videogame.

The hours in the train!! You should take advantage of the hour of train you have from London to Coventry!!

That’s what the Yoda inside me shouted from time to time. I tried to listen to him and actually I could read quite a lot on the train and do some coding. But it’s something you can’t do (or at least I can’t) on a daily basis. Sometimes in the mornings (and in the afternoons too) you need to sleep because you’re so tired, or you need to talk with your workmate, or whatever. Let’s say that you can use 4 of the 8 hours you spend on the train every week. Not too much but more than 0.

Obviously going to meetups, hackathons and so on was quite difficult.

One month ago we agreed that we can work at the train, so we just need to be from 9 to 15:20 in the office. That made a big change. Now I get home at 17:15 and I have time to pick up my son at the nursery. You don’t know how happy I am to do that.

Now you can do a lot of things in the afternoon! You have two hours more! Let’s make lots of pet projects and read thousands of books.

No I can’t. And I can’t because I don’t want to. I want to use those hours to be with my kid and my wife. I prefer to pretend to be Sant Jordi and the Dragon than reading the last rant about micro-services. I want to do some exercise a couple of days a week, and to go to some meetups from time to time.

What’s my point with this article? I think that every decision that you make in your life has some trade-offs and you have to think always if you want to accept them. And that those trade-offs are much bigger when you have a family. You can’t just decide that you’re going to focus in your career and leave all the other things apart. You have to try to do some of the things of both categories, focusing more on some things now and in other things later. I agree in what both categories of articles are explaining: it’s good to go to meetups, read books, do pet projects, go to hackathons, etc. And It’s also good (and probably more important) to eat well, do exercise, spend time with your family, sleep, do meditation and so on.

But you can’t just do the things of one category, and you can’t do all of them. Accept that and choose. And probably review and think what you choose every week. Fix some things you want to do (family, some sort of exercise) and decide what you want to do with the rest of your time.

Is for that reason that every day I value more remote work. I think that (in general) it allows you a greater flexibility and it removes commuting from your life, which is always great.