The Switch Statement

I first cut my teeth with OO design and development using C++ and an operating system called QNX. To be honest, it took me at least 5 years to fully make the transfer from procedural programming to OO.

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

IR35, Here it Comes Again…

IR35, Here it Comes Again…

IR35, Here it Comes Again...In 2021 the reform to IR35 Off-Payroll rules is to be rolled out to the private sector. As before the reform will only affect companies that do not meet the following attributes: an annual turnover below £10m fewer than 50 employees or a...

Solving the Resource Conundrum

Solving the Resource Conundrum

Solving the Resource ConundrumPicture this. One minute all is fine and dandy, you have access to all the resources you could possibly need, then bam an unexpected challenge arises. Suddenly you find yourself lacking the capacity to meet the new need. What are your...

Quality – An Aid to Produce Consistent Rubbish

Quality – An Aid to Produce Consistent Rubbish

Quality - An Aid to Produce Consistent RubbishAnother year has passed, and myself and a colleague have hosted a BSI auditor for our annual ISO9001/TickITplus check-up, and in fact this was more than the regular check, in that it was our 3-year re-certification audit,...

The Hazards of Legacy Systems

The Hazards of Legacy Systems

The Hazards of Legacy SystemsBeing the owner of a software system with a dedicated customer base sounds like the kind of position one would like to find themselves in. At least until it gets superseded and you have to face dealing with a legacy system. Many developers...

How to Test Without Access to The Test Environment

How to Test Without Access to The Test Environment

How to Test Without Access to The Test EnvironmentIn many of our previous articles, we have expressed the importance of achieving a high standard of testing. Potentially blocking this achievement, several factors can come together to affect the quality of your...

The Technical Workshop – How To Make Them Work For You

The Technical Workshop – How To Make Them Work For You

The Technical Workshop - How To Make Them Work For YouAnyone experienced in product design will understand just how valuable a facilitated workshop can be. Bringing together a project's key stakeholders into a single space allows for the exploration of diverse...

Developing Software for Safety Related Systems

Developing Software for Safety Related Systems

Developing Software for Safety Related SystemsSoftware systems should always be both robust and reliable, however the moment you introduce a safety element, this need for reliability increases significantly. The level of safety required is governed by the severity and...

How to Choose an Outsourcing Partner

How to Choose an Outsourcing Partner

How to Choose an Outsourcing PartnerHaving recognised a need to outsource, and worked your way through the initial preparations, you are now in a strong position to seek out a suitable partner. Choosing an outsourcing partner is no trivial affair, so taking the time...