Uncategorized

C# needs to allow us to write code in a more functional, immutable style

Posted on Updated on

Since C# 6.0 is now feature-locked (and I can’t wait) it’s time to start talking about something that might be in the next version, assumedly called C# 7.0.

The big feature for me would be to get C# to help us write immutable code. That means they need to make it easy to write immutable classes (see my last post on Record types) and immutable local variables too.

I propose the following syntax:

Read the rest of this entry »

An introduction to F#: Part 1

Posted on Updated on

A few days ago I ran an introduction to F# course for colleagues at work. The idea of the course was to explain the basics of F# including syntax, programming style and the FSI window, as well as to get people excited. I didn’t correctly judge how long the presentation would take (people were asking lots of questions as I went, which is good!), so I ended up splitting it up into two parts. Here I am publishing a rough written copy of the presentation, compiled from my notes that I worked from as I talked, for you to peruse. It’s somewhat rough, but for those who were there it should serve as a reminder. Next time we will talk through some worked examples of Fizzbuzz & fizzbuzzbaz, we will cover types (classes, structs, interfaces, enums, records, units of measure and discriminated unions). We will also go through some application design, such as: MVC, DDD, CQRS, WTFBBQ.

Read the rest of this entry »

Finding overlapping rectangles

Posted on Updated on

This is a classic quick programming puzzle: write a function that, given two rectangles, will determine if the triangles overlap. I found this puzzle over on http://leetcode.com/2011/05/determine-if-two-rectangles-overlap.html, and thought it was worth talking about because the solution is wrong, and it was interesting to me because it shows how thinking about a domain can influence your implementation of behaviour.

The mistake made by the original author is with one of the assumptions / interpretations made during their reasoning process. They said “when two rectangles overlap, one rectangle’s corner point(s) must [be contained within] the other rectangle”. This appears reasonable, given the example below:

Read the rest of this entry »

Programming interview questions

Posted on Updated on

At my last company I was a team lead and, as such, was one of those responsible for interviewing new candidates. Although I didn’t enjoy the process much, I did become interested in the sorts of technical questions / exercises we could set to get a good judge of a candidate’s abilities.

Developer interviews typically cross a number of disciplines (often arranged in stages) that may include:

  1. a CV / résumé screening
  2. a telephone interview
  3. face-to-face questions and answers
  4. a technical exercise or written test to be done at home, or to be done in front of the interviewers

Read the rest of this entry »

SOLID, CQRS and functional dependency injection

Posted on Updated on

I was prompted to write this after reading Mark Seemann’s post (http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/)

From http://kozmic.net/2012/10/23/ioc-container-solves-a-problem-you-might-not-have-but-its-a-nice-problem-to-have/:

Fol­low­ing Sin­gle Respon­si­bil­ity Prin­ci­ple will lead you down the road of hav­ing plenty of small classes.

Open Close Prin­ci­ple will lead you down the way of encap­su­lat­ing the core logic of each type in it, and then del­e­gat­ing all other work to its depen­den­cies, caus­ing most of those small classes to have mul­ti­ple dependencies.

Read the rest of this entry »

Exceptions and error logging

Posted on Updated on

At my current work I see an awful lot of code that looks like this:

public class ServiceA : IServiceA
{
    private readonly ILogger _logger;

    public ServiceA(ILogger logger)
    {
        _logger = logger;
    }

    public void DoIt()
    {
        try
        {
            // Do something...
        }
        catch(Exception e)
        {
            _logger.LogError(e);
            throw;
        }
    }    
}

At first blush this looks reasonable: it has constructor-based dependency injection, an abstraction, error logging… What could my issue be?

Read the rest of this entry »

A functional Fizzbuzz

Posted on Updated on

After reading Scott Wlashin’s post (http://fsharpforfunandprofit.com/posts/railway-oriented-programming-carbonated/) on writing FizzBuzz in a more functional style and with the ability to have more rules (other than 3 => fizz, 5 => buzz, etc) I decided to give it a go myself. This represents the most idiomatic F# I could come up with, and I think it reads pretty well. It uses partial application, list operations, shorthand match-expressions-as-functions and option types.

let carbonate rules i =    
    rules
    |> List.choose (fun (divisor, replacement) ->
        if i % divisor = 0 then Some replacement else None)
    |> function [] -> string i | list -> String.concat "" list
        
let carbonateAll rules = List.map (carbonate rules)
    
let fizzbuzzbazz = carbonateAll [3, "fizz"; 5, "buzz"; 7, "bazz"]

fizzbuzzbazz [100..110] |> printfn "%A"

See if you can follow how it works.