Home‎ > ‎

Evil Programming Techniques

1) Using field initialization to do shitloads of work

public class EvilManager
{
    private static object[] _CacheOfEvilShit = PopulateEvilCache();

    private static object[] PopulateEvilCache()
    {
        object[] _cache = new object[long.MaxValue];
        for (long l = 0; l < long.MaxValue; l++)
        {
            ... time consuming operation here ...
        }
        return _cache;
    }
}

This class's initialization will hog the thread the first time the class is instantiated, or if any of its static members is accessed, even if the EvilCache is never used.

2) Singleton? What singleton? I see no... oh THAT singleton

private class HiddenEvilSingleton
{
    private HiddenEvilSingleton _instance;

    public static HiddenEvilSingleton GetInstance()
    {
        if (_instance == null)
            _instance = new HiddenEvilSingleton();

        return _instance;
    }

    private List _minions = new List();

    public void RegisterMinion(SeeminglyInnocentFront minion)
    {
        _minions.Add(minion);
    }
}

public class SeeminglyInnocentFront
{
    public SeeminglyInnocentFront()
    {
        HiddenEvilSingleton.GetInstance().RegisterMinion(this);
    }
}

 You can hear the unsuspecting consumers scratching their heads and saying "Now where's that darn memory leak coming from? I'm sure I released every reference after I was done with it."
Comments