Try some monads today

One quality I like in code is easy/quick grokability – from the work grok – to understand thoroughly and intuitively. That is usually enhanced if the code consists of well spaced and familiar lines of code with minimal interruptions – like if/else statements that need their own branches in the method.Every one of these interruptions break the reading of the code and the brain has to make note and focus to see if there’s anything worth noting in the branch inside a method. What can help in structuring code better for grokability are monads. Continue reading
Advertisements

Ifs (Booleans and enums)

I am almost always dismayed when I see a method with too many if statements. More often than not, this is a precursor to similar conditions in other methods in the same or other classes, using the same condition to switch the actions to be called. This is an anti pattern I’ve seen repeated at far too many places. I think the anti if campaign mirrors my thought process about what needs to be done. Continue reading

Avoid Agile Fatigue

This month’s Agile Yorkshire meeting had a talk by Nick McKenna, the CEO of McKenna Consultants, on an unusual topic: There Is No Agile (text of the talk from his blog). A well spoken and engaging speaker, Nick kept it engrossing all the way. In brief, he argued that there is no such thing as an Agile way/process of doing things. Every team must reach their own ‘Agile way’ by trial and error – discard/change the practices that are not working or make sense so as to get the best value. Continue reading

Randomise your unit tests – lists

Following on from the previous post about benefits of randomising unit test inputs, the following deals with the unit testing of lists.
Create a list with random number of entries using the following extension method. The following uses the ForEach extension method for IEnumerable.
       public static IList ListFor(this Random random, 
            int min = MinForList, int max = MaxForList)
            where T : new()
        {
            var list = new List();
            Enumerable.Range(1, random.Next(min, max))
                      .ForEach(x => list.Add(new T()));
            return list;
        }

Continue reading

Randomise your unit tests – numeric calculations

When unit testing it’s helpful to ensure that the expected value for a unit test should not be hard coded. For numeric calculations, this usually entails creating a random set of inputs, calculating the expected value in a way that is different to how it’ll be implemented, then calling the method under test to get the actual value for comparison. This process has the added advantage of changing the inputs every time it runs, so the chances of false positive/negative are minimal.

Continue reading