Thursday, September 11, 2003

Paradigm shift

Not really a paradigm shift, but fixing my kludgy start. I've gone down the very non-OO approach of large switch statements to control flow:

switch (node.Name)
{
case "BinaryProcessor":
m_Blocks.Add (new Processors.BinaryProcessor());
break;

case "Parser":
m_Blocks.Add (new Parsers.Parser());
break;

case "Processor":
m_Blocks.Add (new Processors.Processor());
break;
//...
}


This thing of beauty had a fraternal twin elsewhere in the code:

object curData = ...;
string str;
byte[] bytes;

if (block is Parsers.IParser)
{
((Parsers.IParser)block).Parse ((byte[])curData, out str);
curData = str;
}
else if (block is Renderers.IRenderer)
{
((Renderers.IRenderer)block).Render ((string)curData, out bytes);
curData = bytes;
}
else if (block is Processors.IBinaryProcessor)
{
((Processors.IBinaryProcessor)block).Process ((byte[])curData, out bytes);
curData = bytes;
}
//...


Eeeewwww. Even as I was typing this I was shaking my head. You know how it goes though. First it's just 1 case or if just to make sure you don't slip in something nasty. Then it becomes 2 -- it's soo much easier than OO-ing the code. Laziness kicks in and Shazam! you end up with spaghetti. Just today I found myself seeing if unions were supported in .NET which finally snapped me out of it.

It's now time to do this properly.

I have to admit it was somewhat nostalgic cutting "university-code" again. All I need is to sprinkle a few printf(""); here and there to "make it work".

No comments: