BLOG

What Time Is It?

29 February 2012 by Stuart Cam

Time is an interesting concept, perhaps even something we take for granted in our universe! Leap years and daylight savings are just two human-imposed time counting quirks that crop up now and then, and can cause all manner of problems with poorly designed systems.

var oldDate = Convert.ToDateTime(DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + (DateTime.Now.Year - this.TimeAtAddress));

The above code will work absolutely fine, until you hit a leap year, like today. The code is then at risk of failing if TimeAtAddress is not divisible by 4, in the process littering your error logs with random failures and likely impacting your business in some negative way.

When dealing with DateTime objects ALWAYS use the built in modifiers, for example .AddYears() and .AddMonths(). It'll save you the heartache of having to fix simple date bugs.

When developing applications it's always worth considering how you handle your representation of time, and often it's worth introducing a time abstraction simply to buy you flexibility around testing.

I've used classes such as the one below to push the calculation of the current date into an abstraction that allows you to override the calculation should you need to.

public static class DateHelper
{
    public static Func DateTimeGenerator = () => DateTime.Now;

    public static DateTime Now()
    {
        return DateTimeGenerator();
    }
}

Usage:

var now = DateHelper.Now(); // will get the current date

DateHelper.DateTimeGenerator = () => DateTime.Now.AddDays(5);
var nowPlus5Days = DateHelper.Now(); // will be the current date time plus 5 days

This becomes particularly useful in scenarios where you'd like to setup some test data for something that happened in the past, in order to validate something that occurs in the future. With some clever use of IDisposable it's possible to construct setups for tests such as:

[Test]
public void A_test(){

	// we are at current time.
	
	using (var _ = DateTime.ShiftDays(-30))
	{
		// we are now 30 days ago.
	}
	
	// we are at current time.
}

If you are feeling really fancy you could start nesting the using() statements and skip back and forth through time as you see fit.

Tags:

Categories: C Sharp

C# Content Spinning Algorithm

14 August 2011 by Stuart Cam

There is a well known technique within black-hat SEO called content spinning. Content spinning involves writing an article using a specific syntax which allows a text processor to generate random variations. These variations can then be used for multiple purposes without fear of being labelled as duplicate content.

I was searching for a C# algorithm which I could re-purpose into a nAnt script, but a few searches on the internet gave no results. I found a python version which seemed good enough, so I rewrote it for C#.

public static class Spinner
{
    private readonly static Random Randomizer = new Random();

    public static string Spin(string content)
    {
        const char OPEN_BRACE = '{';
        const char CLOSE_BRACE = '}';
        const char DELIMITER = '|';

        var start = content.IndexOf(OPEN_BRACE);
        var end = content.IndexOf(CLOSE_BRACE);

        if (start == -1 && end == -1 || start == -1 || end < start)
        {
            return content;
        }

        if (end == -1)
        {
            throw new ArgumentException("Unbalanced brace.");
        }

        var substring = content.Substring(start + 1, content.Length - (start + 1));
        var rest = Spin(substring);
        end = rest.IndexOf(CLOSE_BRACE);

        if (end == -1)
        {
            throw new ArgumentException("Unbalanced brace.");
        }

        var splits = rest.Substring(0, end).Split(DELIMITER);
        var item = splits[Randomizer.Next(0, splits.Length)];
        return content.Substring(0, start) + item + Spin(rest.Substring(end + 1, rest.Length - (end + 1)));
    }
}

Usage:

Spinner.Spin("{Hi|Hello|Good morning}, my name is {Matt|Bob} and I {certainly |might |should |}have {something {important|special} to {say|eat|share}|a favorite {toy|book|poem|song}}.");

Tags: , , , ,

Categories: .NET | C Sharp

New start up launched

25 January 2011 by Stuart Cam

So, this is the first update on this blog for quite a while and you are about to find out why it's been so quiet around here.

I have been very busy working on a new start up with my business partner, Valere Speranza. If you have ever founded a start up you can appreciate just how much time they consume - all of it. It gives me great pleasure to announce Lead Magpie!

Lead Magpie : The marketplace for UK mortgage leads.

Check out the site at http://www.leadmagpie.com to learn more.

Tags: ,

Categories: General

CQRS

04 September 2010 by Stuart Cam

There seems to be a lot of noise around CQRS and DDDD these days:

Tags: ,

Categories: .NET


© Codebrain 2014. All Rights Reserved. Registered in England: 07744920. VAT: GB 119 4078 13