switch or nested if statements

Looking for some advice here, mostly to what some much more experienced programmers would see as the pro and cons of two different approaches. Just and FYI all the code that I have as of now works, I am just looking for advice on approaches, as this is all still pretty new to me.

I am writing a program that will control a automated batch reactor. There will be set points that will be able to be edited from the software.

The user interface is from a keypad that I drew up on a touchscreen display. It works great.
I don't know at the moment how many set points I will have, but it's safe to say it will be greater then 10.

Method #1

User is on the edit set points menu and selects the option to edit the lower pH set point.
This would call the function editSetpoint("pHL");

Function editSetpoint calls my keypad() function to draw the keypad to the screen. It then further displays information based on the string value that I sent to the function.

Now the user hits the confirm button and it calls errorCheck(keyedInput, "pHL")
keyedInput is the value the user input, the function returns a boolean value.

Now my error checking works, it uses nested if statements. First it has to check for the parameter "pHL" then does the various error checks.

Method #2

I was thinking of utilizing switch statements. I would define a variable such as

#define pHL = 1

This would allow me to use switch statements in my errorCheck function.

Is there any real advantage to one approach to the other? To me method #2 looks neater, but am curious to learn more about the two approaches.

Thanks in advance for any input!

I always figured switch:case operated faster as didn't have to go
0? no
1? no
2? no
3? no
4? yes

It could just go:
jump to (#4).

Hi,
Fortunately I'm 15 years away from CodeStyleWars at IBM...

This is a semi-religious question with a diversity of opinions.

So here's Mine, to start:

  • If the complexity is 3 or 4 decisions or so, nested 'if' may be clearer to read.
  • If the complexity is higher, the "switch" statement may be clearer to read.
  • There is little run-time effect between the choices with most processors/compilers

If you have time to worry about this stuff, write it both ways, test it, and then develop your own style for the future.

One problem with if's is that if one of them changes the thing being tested, it could trigger something too soon vs waiting for the next pass at the top of the test.
Or maybe that's more of an issue for a sequence of if's.

There are few things uglier in this life than 20 if{} statements in a row :slight_smile:

If you are checking a string for validity the neat way to do it is to have an array of valid strings and a function that scans the array looking for the one the user entered. Bear in mind that if you are comparing strings a switch() won't work anyway.

If you are checking numbers a switch() is good, but still ugly if there are a lot of options. In this case I would normally have an array of function pointers and use the number to index into that array (with some appropriate bounds checking first of course).

None of these options will make any real difference to a UI as that's really slow anyway, it's partly personal preference and partly writing clear and easily maintainable code.

Graynomad:
There are few things uglier in this life than 20 if{} statements in a row :slight_smile:

I find user-interface code always ends up ugly. There's simply no way around testing for all the different things. If on menu A there's a maximum value and menu B is setting something else then the maximum is different, but depends on something you set on A but only while the mode switch is "auto" and...

Try to make it as consistent as possible across all parameters. If most of them have a maximum and minimum, you may consider putting that into a function and then you only need to write that code once, but use many different calls to the same function.

If on menu A there's a maximum value and menu B is setting something else then the maximum is different, but depends on something you set on A but only while the mode switch is "auto" and...

:slight_smile:

There is a fundamental difference beween SWITCH/CASE and IF. With SWITCH/CASE the values must be known at compile time. With IF they can vary while the program runs.

…R

clarity vs speed really, if it is clear and clean in your code when the nested 'if' statements exist and speed isn't an issue then that is ideal.

If it isn't clear and looks messy use a switch statement. I have been coding for 8 years now and that is the general methodology in industry.