Phillip Trelford's Array

POKE 36879,255

RadioHead

I’ve heard a few times that I have a great face for radio ;). Here’s 3 recordings from this year where I get my radio head on and talk about F#.

HanselminutesWhy F#? with Richard Minerich and Phillip Trelford

Interview with Scott Hanselman (March 2012)

 

InfoQ

Phil Trelford on Functional Architectures, F#

Interview with Werner Schuster (May 2012)

 

dotnetocksPhil Trelford Codes in F# 3.0

Interview with Carl Franklin & Richard Campbell (August 2012)

 

My next gig is Strangeloop later this month, hope to see you there:

strange_loop

F# for Trading (September 24th 2012)

Scrap your Boilerplate

Boilerplate is any seemingly repetitive code that shows up again and again, like getting and setting instance variables, but seems like it ought to be much simpler.

Jon Harrop recently commented on an MSDN article on Functional Programming in C++:

(from Figure 8) 60 lines of "functional" C++ in 1 line of F#: type Immutable = {d: float; s: string} http://msdn.microsoft.com/en-us/magazine/…

60 lines is a lot of boilerplate! Here Jon defines a 1 line record type to express a simple immutable type.

C#

Java and C# reduced the repetition of header files in C++. However C# code is still full of boilerplate, to the point where third party Visual Studio plugins like Resharper have become the de-facto standard for C# developers as a band-aid over the repetitive strain.

F# on the other hand is quite terse, which not only reduces the chances of RSI. it can also make the code easier to read and maintain.

Here’s 3 short examples of how F# avoids some of the boilerplate pitfalls of C#.

1. Constructors

It is common to inject dependencies into a class via the constructor. In C# these dependencies are then typically captured as fields for use by member methods:

public class VerySimpleStockTraderImpl : IAutomatedStockTrader 
{
    private readonly IStockAnalysisService analysisService;
    private readonly IOnlineBrokerageService brokerageService;
 
    public VerySimpleStockTraderImpl(
        IStockAnalysisService analysisService,
        IOnlineBrokerageService brokerageService) 
    {
        this.analysisService = analysisService;
        this.brokerageService = brokerageService;
    }
    
    public void ExecuteTrades() 
    {
        // ...
    }
}

Notice how both the analysisService and brokerageService names are referenced 3 times. I call this the local government pattern™ as everything needs to be filled out in triplicate.

F# classes specify the primary constructor as a closure over the class members so the arguments are captured in one hit:

type VerySimpleStockTraderImpl
        (analysisService:IStockAnalysisService,
         brokerageService:IOnlineBrokerageService) =
    member this.ExecuteTrades() = () // ...

2. Properties

Other parameters to constructors may specify initial values for properties. C# 3 introduced auto-implemented properties to help reduce the boilerplate, but it is still quite verbose:

public class OrderLine
{
    public OrderLine(decimal price, int quantity)
    {
        this.Price = price;
        this.Quantity = quantity;
    }

    public decimal Price { get; private set; }
    public int Quantity { get; set; }
}

F# lets you specify the initial value for a property at the property definition:

type OrderLine(price:decimal, quantity:int) =
    member val Price = price
    member val Quantity = quantity with get, set

The sample above uses the new F# 3 auto-property syntax.

3. Out parameters

In C# if you want to return more than one value from a function you must resort to out parameters:

string value = "";
if (openWith.TryGetValue("tif", out value))
{
    Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
    Console.WriteLine("Key = \"tif\" is not found.");
}

F# lets you return any number of values. C# methods using out parameters can be called from F# as if they returned multiple parameters:

match openWith.TryGetValue("tif") with
| true, value -> printfn "For key = \"tif\", value = %A." value
| false, _ -> printfn "Key = \"tif\" is not found."

Summary

F# class and property syntax dramatically reduce the boilerplate over C# class definitions. Returning multiple values is easy in F# and there’s even support for methods using C#’s out parameters. Here I’ve presented just 3 examples that help scrap the boilerplate, but this is only the tip of the iceberg.

Interested? Try F#

Blog 101

This is a meta post to coincide with posting over 100 articles over 4 years, going back over some the more popular along with some of my own personal favourites. I started blogging regularly back in 2007 on the Applied Games Group blog, here’s a selection from there: 

Then in 2008 I started this blog with the motivation of keeping a record of some of the things I’ve learned while improving my technical writing skills with a view to working on a book. This week in collaboration with Tomas Petricek I signed a book deal with Manning.

Many of the posts are written during my daily commute from Cambridgeshire down to London, which takes just over an hour. The ideas for posts come at different times, more often when I’m away from my computer taking a walk or a cycle or having a bath. Once an idea has solidified in my mind, I usually start by writing a working title, an opening sentence, a brief outline and then fill in the gaps.

Tools/Tech:

Most popular articles

More often than not it’s the articles I least expect to be popular that are. Typically early views come from Twitter, and then from link sites like Chris Alcock’s Morning Brew and Alvin Ashcraft’s Morning Dew. My biggest traffic spikes have come from CodeProject.

My picks

A selection of interesting technology and some minor rants.

Mini Games

My interest in programming started with video games, and I still enjoy writing mini games. I typically write them in Silverlight so that I can embed them in the blog.

Open Source Projects

Introductions to some of my open source projects on CodePlex.

Thanks!

Thanks for all the feedback, it really helps, don't be shy :)