Use extension methods to let your enums hold the logic.

I often stumble upon code checking values on enumerations to determine the state of some object or rule. At best, this code is extracted and put in a helper or utils class of some sort. Don’t! Use extension methods instead. It let’s you provide a name for the condition (rule) that you are checking, and it put’s it together with the enumeration.

Example
Lets say I have an enumeration with storage providers. Each provider might be able to store data virtually, hence I need the possibility of determining if it’s capable of this.

[Serializable]
public enum StorageProviders
{
    LuceneIo = 0,
    LuceneVirtual = 1
}

public static class StorageProvidersExtensions
{
    public static bool IsVirtual(this StorageProviders provider)
    {
        return provider == StorageProviders.LuceneVirtual;
    }
}

I can now act on the enumeration value it self.

...connectionInfo.ProviderType.IsVirtual()...

//Daniel

Does code have to be Localized for achieving a “Ubiquitous Language”?

Recently I have encountered C# code containing names found in the business that the applications maps to. So far so good except that the names of objects, methods, properties, functions etc. were written in Swedish and of course with special characters like “Å, Ä, Ö”

My question is “Does code has to be localized for achieving an Ubiquitous Language?

In my eyes the answer is “No”, even if the corporate language in the business is Swedish, I think that the code should be written in English. Why? Well…for starters: You lose internationalization. What if developers that doesn’t understand Swedish have to work with and understand the code? Another downside is that English and Swedish will be tangled up and even if it’s a Swedish developer that reads the code he/she will probably feel confused since the “normal” language used when writing software is English. So finding e.g. Swedish verbs for functions etc. will be confusing. And for me “Confusion is the result of unclear intent of the code”.

Personally I would go with 100% English, even if we get terms that doesn’t match the Swedish counterpart to 100%, yes it will require more of the developer whom will have to have two versions of the domain-model in their mind, but rather that than unclear code. And finally to answer my question, I don’t think that the localization of the code affects the degree of how Ubiquitous Language you have achieved. What affects this is how well the structure of the domain-model maps to the business.

//Daniel