alternatives to long else if and switch statements

Started by
15 comments, last by wack 10 years, 10 months ago

From my experience with working with large and ugly legacy code bases, the most important thing is to use a really good IDE that lets you browse and nagivate the code with ease. And even more important, lets you easily refactor the code and be 100% confident about it. I usually take some time and just sit down with the code and refactor for two reasons.

1) To get a better overview and understanding of the code.

2) New code makes bad code worse, but refactoring can only make it better. Being a good boy scout, you need to refactor enough to at least balance the amount of new code you plan on adding.

openwar - the real-time tactical war-game platform

Advertisement

Felix: Usually I would do exactly the same, the only problem is, every single changed line needs to pass through an engineering board and needs to be justified (in terms of time/money invested) and since in the eyes of the main contractor we are doing only sustaining engineering (i.e. fix major bugs, not add features) this is hard to communicate.

BloodOrange1981:

As for code examples, I feel a little apprehensive about cutting and pasting production code on a public forum!

Yeah, don't do that. Might get you fired.

every single changed line needs to pass through an engineering board and needs to be justified (in terms of time/money invested)

Then you are basically screwed ... your hands are tied assuming that you personally aren't in a situation in which you can talk directly with the client (or whoever is paying for your time). I mean, the thing to do is have someone explain to whoever is paying for the maintenance work you are supposed to be doing that it might ultimately cost less to do the right thing and touch more code now then to screw around making little patches for the rest of time.

Yeah, the contractual and technical situation is a bit weird. We are maintaining a small part of one of the scientific racks aboard the international space station. If we make mistakes, lose communication and need an astronaut to fix the error, it may well cost the agency (which is the the customer in the end) more than a few of my annual salaries. So we do lots and lots of testing and requirements tracking and meetings for every little change, which in turn costs a lot of time and money. But I don't think this belongs here...

Yes, many of the agile practices depend on each other. If you can't release early and release often, and where it's not possible to reduce the cost of mistakes, then you have to rethink the other parts of the process too.

openwar - the real-time tactical war-game platform

It completely depends on just what code is in that switch statement. Switch statements are often not the best solution.

If your switch statement has the same kind of code in each case e.g. imagine a function that turned the numbers 1 to 7 into the strings "Monday" to "Sunday". The best option there is to use a table lookup.

It is primarily when the code for each case is fundamentally doing different things that a switch is likely the best tool.

Even then there are cases where the virtual dispatch mechanism is a more appropriate thing to use. E.g. instead of switching based on RTTI.

As for long else-if chains, generally if a single switch statement is at all possible then it is the better option.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

In this case I would not yet care too much about performance. Primary target should always be readability and maintainability of your code. At work, I have to deal with legacy code containing, in multiple places, switch statements and deeply nested if statements with in some cases more than 5000 LOC. This is a programmer's nightmare come true.

Write the simplest, most easily maintainable version you can think of (using a function pointer table or inheritance) and use that, until you have some evidence (i.e. after you have profiled your code) that this has a measurably bad impact on your program's overall performance.

Exactly the kind of situation I`m facing. My colleague thinks it`s super ugly too, and thinks the company we`ve inherited it from must have had some super eccentric coders. 300 line functions and 15000 line files. Crazy. It`s even worse when you have to insert #defines and #ifdefs in between it all to handle seperate formats.

Thanks for the advice, and good luck to you with your code too!

This kind of stuff is par for the course in old code bases. Stuff might have started out small, and then people keep on adding stuff through the years as requirements change, features are added, and bugs are fixed.

Everyone thinks the code sucks, including those who wrote it, but they find comfort in convinving themselves that they "will refactor in the future". The truth is that that day will probably never come.

The upside is that this is all manageable with a good IDE that has nice code browsing features, code folding, etc.

The best advice I can give, is just leave it alone and learn to deal with it. If the product is worth anything, there should quite frankly be better things to do than to than to restructure IF-statements.

If you want to rewrite it, you should be able to demonstrate (with cold hard numbers) that it will in fact save time or money to rewrite it. Otherwise it's just programmer masturbation.

This topic is closed to new replies.

Advertisement