Let’s say you needed to initialize a buffer to be 260 bytes long, and they all should be 255. Now, you’re using a high-level language, like C#, to talk to a legacy device, so you might have to jump through some hoops to deal with the device API, but how hard could it be? A better question might be, “how hard can you make it?”

There’s an old saying: “fast, good, or cheap: pick two”. Massimo’s employer doesn’t want to be greedy, so they consistently pick one: cheap, which means they get code like this:

class ContainerClass : ISnippedAutogeneratedBase {
    // Not shown: 6200 lines doing everything and its opposite in several different flavours and variations.
    /*************************************************************************************************
    * This method initializes our BRAND A or BRAND B device memory block
    * with all cells written to FF
    *
    *************************************************************************************************/
    private DeviceResult InitializeDeviceMemory()
    {
        try
        {
            int Block  = 1;
            bool res = false;
            int Destination = Project.Properties.Settings.Default.DST;

            List<byte> Out = new List<byte>();

            for (int i = 0; i < 26; i++)
            {
                if (Out.Count < 260)
                {
                    for (int k = Out.Count; k < 260; k++)
                    {
                        Out.Add(255);
                    }
                }

                int Start = i * 10;

                //byte[] data = new byte[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                //List<byte> values = new List<byte>(){0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
                List<byte> values = new List<byte>() { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                for (int z = 0; z < 10; z++)
                {
                    //data[z] = Out[z + Start];
                    values[z] = Out[z + Start];
                }

                res = WriteTenMemoryLocations(Destination, (byte)Block, (byte)Start, values);
            }

            if (res) return DeviceResult.OK;
            else return DeviceResult.UnknownError;
        }
        catch (Exception ex)
        {
            Logs.WriteLog(ex, "ContainerClass.InitializeDeviceMemory");
            return DeviceResult.UnknownError;
        }
    }
}

The first thing I want to note is the interface this class implements- ISnippedAutogeneratedBase. You might think to yourself, “Oh, so this code was autogenerated,” but no- the interface was autogenerated.

The obvious attraction here is the loops. At some point, someone wrote the highly specific WriteTenMemoryLocations method. Since setting 260 memory locations means calling that method 26 times, we need loops.

But why do we need Out? The most puzzling thing in this code is that they build all 260 copies of the number 255 into the Out list, and then take ten element slices out of that list to populate values- even though they’re all the same value. At some point, thanks to the comments, we know that values was being defaulted to ten elements of 255, making everything involving the Out list pointless and redundant.

That’s the obvious WTF, but it’s not the best part. Notice this line:

int Destination = Project.Properties.Settings.Default.DST;

Once upon a time, someone wanted to be able to configure whether the device was in daylight saving time or not, so the flag was created. But no one ever actually implemented the feature, so the flag was left at its default value of 62- which also just happens to be the memory address where we want to initialize these values.

[Advertisement] Easily create complex server configurations and orchestrations using both the intuitive, drag-and-drop editor and the text/script editor.  Find out more and download today!