Archive for category Programmer

Response to Readability

JP put out another post requesting comments on a Readable (Fluent) approach he’s currently using. I tried to leave a comment, but the system wouldn’t accept anything I used for the captcha… so here goes:

JP writes: [csharp] Run.the<wire_up_global_error_handling>() .then<initialize_the_container_for_the_user_interface>() .then<initialize_the_user_interface_registry>() .then<initialize_the_ui_images_registry>() .then<initialize_the_main_menus>() .execute(); [/csharp]

Here are my humble comments:

  • “Run” & “execute” seems redundant
  • The underscores are nice to look at but CamelCasing is also easy to read (for me), and easier to type … I suppose if you’re using your AutoHotKey ninja tricks like JP does, this would be minimum overhead.

So, for the full comparison in code:

[csharp] Start.by<wireUpGlobalErrorHandling>() .and<initializeTheContainerForTheUserInterface>() .and<initializeTheUserInterfaceRegistry>() .and<initializeTheUiImagesRegistry>() .finally<initializeTheMainMenus>(); [/csharp]

Hopefully JP gets this via a trackback.

Tags: , , ,

Resharper 4.0 Release Candidate Available

JetBrains has just posted their Release Candidate for Resharper 4.0.

I’m excited to see this and already have my C# tax ($99) lined up for it’s official release!

I’m officially a convert from the CodeRush/Refactor Pro camp… I just found Resharper more discoverable than CodeRush.  CodeRush definitely has a slicker UI, but Resharper lets me work more like myself (renames/namespaces/interface-based architecture) in Visual Studio.

Tags:

Multiple Views (redux)

Jeremy posted an article on retrieving filtered results on collections, using the perennial Animal kingdom example. Great post.

As I was looking at this again, it just started looking “smelly”. First thought, can we do this cleaner with Generics?

public IEnumerable AnimalsList() where AnimalType:class
{
  foreach (IAnimal animal in _animals)
  {
    AnimalType testAnimal = animal as AnimalType;
    if (testAnimal != null)
      yield return testAnimal;
  }
}

Oh yeah… generics are just the coolest thing.

But, this still smelled to me (I didn’t like all the filter code inside the function). Sprinkling some functional programming goodness (thank you Dustin), it turned into this.

public delegate bool FilterAnimalPredicate(System.Type type);

/// snip

public IEnumerable AnimalList(FilterAnimalPredicate filter)
{
  foreach (IAnimal animal in _animals)
  {
    if (filter(animal.GetType()))
    {
      yield return animal;
    }
  }
}

Admittedly, I have cheated … we are no longer using a Property, and hence the unit tests had to change. But I’d take this approach as it feels much cleaner.

Thanks again to Jeremy for sparking this post.

Here’s the final Test Code:

using System;
using System.Collections;
using System.Collections.Generic;
using MbUnit.Framework;

namespace MultipleIterators.Test
{
    [TestFixture]
    public class ZooTest
    {

        private static bool IsElephant(System.Type type)
        {
            return (type is Elephant);
        }

        private static bool IsZebra(System.Type type)
        {
            return (type is Zebra);
        }

        [Test]
        public void Can_add_elephants_to_the_zoo()
        {
            Zoo zoo = new Zoo();
            zoo.AddAnimal(new Elephant());
            int animalCounter = 0;

            foreach (IAnimal animal in zoo.Animals){
                animalCounter++;
            }
            Assert.AreEqual(1, animalCounter);
        }

        [Test]
        public void Can_add_zebras_to_the_zoo()
        {
            Zoo zoo = new Zoo();
            zoo.AddAnimal(new Zebra());
            int animalCounter = 0;

            foreach (IAnimal animal in zoo.Animals){
                animalCounter++;
            }
            Assert.AreEqual(1, animalCounter);
        }

        [Test]
        public void Can_get_just_the_zebras_out_of_the_zoo()
        {
            Zoo zoo = new Zoo();
            zoo.AddAnimal(new Zebra());
            zoo.AddAnimal(new Elephant());

            foreach (Zebra zebra in zoo.AnimalsList<Zebra>()){
                Assert.IsTrue(zebra is Zebra);
            }
        }

        [Test]
        public void Can_get_just_the_elephants_out_of_the_zoo()
        {
            Zoo zoo = new Zoo();
            zoo.AddAnimal(new Zebra());
            zoo.AddAnimal(new Elephant());

            foreach (Elephant elephant in zoo.AnimalsList<Elephant>()){
                Assert.IsTrue(elephant is Elephant);
            }
        }

        [Test]
        public void Can_get_just_the_elephants_out_of_the_zoo_using_predicate()
        {
            Zoo zoo = new Zoo();
            zoo.AddAnimal(new Zebra());
            zoo.AddAnimal(new Elephant());

            foreach (Elephant elephant in zoo.AnimalList(IsElephant)){
                Assert.IsTrue(elephant is Elephant);
            }
        }
    }
}

Tags: , , ,