The Switch Statement
I went through the “normal” stage of thinking that I understood OO, which I term procedural code with classes wrapping them up. After constant development, I finally got that basically in OO everything is an object. This was brought home to me when I worked on a project where a “CMessageUnsignedInt” class was created.
The class involved was a message ID, to signify events within the system. Other than the “event” (unsigned int) there was no other payload. I was the test team manager at the time and hence not involved in the design and development, but I told them that it was a mistake and that they should specialise out each event as an object. I had many discussions with the development team leader about this.
The system was large and complex, with several subsystems involved; this meant that the events (CMessageUnsingedInt) were being processed in many places. To distinguish which actual event it was, there were many switch statements, some of which were subsets of the complete list. So to add a new event, you either had to tell everyone what the new event was and what it meant, or to go rummaging through the code yourself.
This has, subsequently, led me to a bold statement! The switch statement does not belong in an OO project. If you have one, your design is flawed. My reasoning is this, and I will base it on the “message” paradigm, as it easy to understand – typically a message is an input either externally (Serial/IP etc) or between subsystem/separate threads of execution, but it applies across the board.
So when a message is received, what do you do with it? Well, in the example cited above, we should have had specialised a “CEventMessage” from which all of the actual events are specialised. Each CEventMessage has an abstract “IsThisYours” method. All of the event messages are registered with a factory. When a message is received you iterate through the factory asking “IsThisYours”. If it is, then a valid object is returned which you process accordingly.
The example cited is trivial, but imagine if the message has a payload. Now the specialised “IsThisYours” method can also unserialise the data and return a validated and correct object.
In addition, sometimes the “message” won’t have an ID (if coming from an external source), it may just be a string of bytes, “IsThisYours” can attempt to deconstruct the bytes and determine if it is valid to that Class.
Anyway, I’ve rambled a bit! My essential point is this: In OO everything is a class and the information belongs in that class. If you have a switch statement, then the data is now outside of the class. Create an abstraction, put it in a factory and iterate through it. Now all you need to do to add a new message, is add a new message. Clearly something somewhere will have to do something with it, but often the message can be treated generically (data logging is the simplest example) but, with the addition of additional abstract methods, displayed on a UI etc.
Clearly with .Net and Type Identification from a loaded DLL adding a message is even simpler.
I worked under the above development team leader on a subsequent project. There was a team of five of us. When I left after three years development I “grepped” all of the source. Not one switch statement to be found.
More From The Blog
Cyber Resilience Starts With Software Engineering
What the UK Cyber Security & Resilience Bill and Software Security Code of Practice Mean for Organisations Operating Critical SystemsFor many years, cyber resilience has largely been viewed through the lens of security controls, monitoring, governance and...
AI Obsolescence: Is your Algorithm as accurate today as it was yesterday?
So far in our series on software obsolescence, we’ve covered the lifespan disparity between digital and physical systems in critical infrastructure, and the inescapable link between cybersecurity and obsolescence. But there’s another area that we specialise in, with...
Cybersecurity, obsolescence – and why testing alongside system development is the key to secure infrastructure
In our 2024 paper, The Challenge of Obsolescence in UK Critical Infrastructure, we highlighted an area of growing concern; the intersection of cybersecurity and obsolescence. The UK’s National Cyber Security Centre (NCSC) wrote that 2023 has seen the addition of...


