Register | Login

Stacking Code

public interface IBlog { string Dump(Stream consciousness); }

Roy Osherove's TDD Kata 1

Tuesday, 13 August, 2013 @ 11:30 AM < Adam Boddington
Tags: C#, Kata, Linq

I've been doing Roy's string calculator kata every day for the last few days. This morning's iteration took me just over an hour, but I suspect that's because I went a bit nuts trying to get the delimiter code out of my Calculator class. Here's what my latest version looks like.

using System;
using System.Linq;

namespace StackingCode.Katas.OsheroveTddKata1
{
    // http://osherove.com/tdd-kata-1/
    public class Calculator
    {
        public int Add(string numbers)
        {
            if (numbers == string.Empty)
                return 0;

            DelimiterStrategy strategy = DelimiterStrategy.Select(numbers);
            string[] delimiters = strategy.DetermineDelimiters(ref numbers);

            return numbers
                .Split(delimiters, StringSplitOptions.None)
                .CheckForMissingNumbers()
                .Select(int.Parse)
                .CheckForNegativeNumbers()
                .IgnoreBigNumbers()
                .Sum();
        }
    }
}

There's heavy use of Linq in there, which seems to be my style these days, and a few extension methods, which I know some people won't like but I think it makes the code more readable.

You can see what the rest of the code looks like over on Bitbucket. If you're interested in how it evolved, you can check out the log as well. There's a commit after each test went green and whenever a refactoring took place.

There are 0 comments.


Comments

Leave a Comment

Please register or login to leave a comment.


Older
Readable Code with Extension Methods

Newer
Priority Queue

Older
Readable Code with Extension Methods

Newer
Priority Queue

browse with Pivot


About


Projects

Building Neno


RSS
Recent Posts

Codility Nitrogenium Challenge
OS X Lock
HACT '13
Codility Challenges
Priority Queue


Tags

Architecture (13)
ASP.NET (2)
ASP.NET MVC (13)
Brisbane Flood (1)
Building Neno (38)
C# (4)
Challenges (3)
Collections (1)
Communicator (1)
Concurrency Control (2)
Configuration (1)
CSS (5)
DataAnnotations (2)
Database (1)
DotNetOpenAuth (2)
Entity Framework (1)
FluentNHibernate (2)
Inversion of Control (5)
JavaScript (1)
jQuery (4)
Kata (2)
Linq (7)
Markdown (4)
Mercurial (5)
NHibernate (20)
Ninject (2)
OpenID (3)
OS X (1)
Pivot (6)
PowerShell (8)
Prettify (2)
RSS (1)
Spring (3)
SQL Server (5)
T-SQL (2)
Validation (2)
Vim (1)
Visual Studio (2)
Windows Forms (3)
Windows Service (1)


Archives


Powered by Neno, ASP.NET MVC, NHibernate, and small furry mammals. Copyright 2010 - 2011 Adam Boddington.
Version 1.0 Alpha (d9e7e4b68c07), Build Date Sunday, 30 January, 2011 @ 11:37 AM