Sunday, July 25, 2010

Who Cares? (Software Craftsmanship - Meeting 2)

I dare to say WE DO... WE CARE !!!

This Wednesday (on 21.07.2010) we had a second Software Craftsmanship meeting.

More than 50 "hungry" software craftsmen came to share their knowledge and practice their software skills.

We reviewed bad code, identified Code Smells and suggested how to improve it.

It is well known that the best way to communicate between Software Professionals is to write and read code together. Therefore we WROTE code in Dojo Style.

I am really excited to see such a community that truly cares about its craft.

If you thought there are none like you, think again...

It doesn't make any difference what type of technology you are using; There are solid Software Principles that are universal and a technology is just one of the tools you should know how to apply.

It's really up to us...

If you are for clean, maintainable and readable code, professionalism (craftsmanship over crap, [also here]) and thorough knowledge, then your place is with us.

Join our group, come to our sessions (every ~1.5 months), share your knowledge and resharpen your skills.

Here are the slides from the meeting.




In addition, you can also find below, my suggestion to the Roman Numeral exercise we did.

(Actually, I can refactor the solution even further, but in the real world scenario I think such a solution will be an overkill).
public class ShortRomanNumeral
{
    internal struct RomanMapper
    {
        public int Value { get; set; }
        public string Presentation { get; set; }
    }

    private const int MinShortRomanValue = 0;

    private const int MaxShortRomanValue = 3999;
    private const string zeroRomanPresentation = "Nulla";

    private readonly RomanMapper[] shortRomanMappers = 
    {
        new RomanMapper(){Value = 1000,  Presentation = "M"},
        new RomanMapper()Value = 900,   Presentation = "CM"},
        new RomanMapper(){Value = 500,   Presentation = "D"},
        new RomanMapper(){Value = 400,   Presentation = "CD"},
        new RomanMapper(){Value = 100,   Presentation = "C"},
        new RomanMapper(){Value = 90,    Presentation = "XC"},
        new RomanMapper(){Value = 50,    Presentation = "L"},
        new RomanMapper(){Value = 40,    Presentation = "XL"},
        new RomanMapper(){Value = 10,    Presentation = "X"},
        new RomanMapper(){Value = 9,     Presentation = "IX"},
        new RomanMapper(){Value = 5,     Presentation = "V"},
        new RomanMapper()Value = 4,     Presentation = "IV"},
        new RomanMapper(){Value = 1,     Presentation = "I"}
    };

    private readonly int value;
    private readonly string presentation;

    public ShortRomanNumeral(int value)
    {
        if (value < MinShortRomanValue || value > MaxShortRomanValue)
        {
            throw new ArgumentException();
        }
        else
        {
            this.value = value;
            presentation = CalculateRomanPresentation();
        }
    }

    private string CalculateRomanPresentation()
    {
        if (value > 0)
        {
            return NonZeroRomanPresentation();
        }
        return zeroRomanPresentation;
    }

    private string NonZeroRomanPresentation()
    {
        int currentValue = value;
        StringBuilder presentation = new StringBuilder();

        foreach (RomanMapper romanMapper in shortRomanMappers)
        {
            while (currentValue - romanMapper.Value >= 0)
            {
currentValue -= romanMapper.Value;
                presentation.Append(romanMapper.Presentation);
            }
        }

       return presentation.ToString();
    }

    public override string ToString()
    {
        return presentation;
    }
}



And here are the Unit Tests:

[TestCase(0, "Nulla")]
[TestCase(1, "I")]
[TestCase(2, "II")]
[TestCase(3, "III")]
[TestCase(4, "IV")]
[TestCase(5, "V")]
[TestCase(6, "VI")]
[TestCase(8, "VIII")]
[TestCase(9, "IX")]
[TestCase(10, "X")]
[TestCase(17, "XVII")]
[TestCase(23, "XXIII")]
[TestCase(47, "XLVII")]
[TestCase(89, "LXXXIX")]
[TestCase(3999, "MMMCMXCIX")]


public void ToString_OfANumber_ReturnsRomanPresentation(int number, string romanPresentation)
{

    Assert.AreEqual(romanPresentation, new ShortRomanNumeral(number).ToString());
}


[TestCase(-1)]
[TestCase(-2)]
[ExpectedException(typeof(ArgumentException))]
public void ToString_OfANegativeNumber_ThrowsArgumentException(int number)
{
    new ShortRomanNumeral(number);
}


[TestCase(4000)]
[TestCase(5000)]
[ExpectedException(typeof(ArgumentException))]
public void ToString_OfANumberGreaterThan3999_ThrowsArgumentException(int number)
{
    new ShortRomanNumeral(number);
}


No comments:

Post a Comment