Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I generally use switch statements to simplify a block of multiple if statements - for example - returning a feedback string to a user based on a multiple choice input.

I also tend to construct classes so that there is one "management" method, to avoid sequential steps or chained method invocation within other methods of the class. I've found this helps to keep methods flexible and focussed - i.e.

class MyClass{

    // this method does nothing more than invoke the relevant method
    // depending on the status following the previous. It's entire purpose
    // is to control application flow
    public function manageFlow($input){
        $status = $this->stepOne($input);
        if($status == false){
            //exit routine
        }

        $status = $this->stepTwo($input);
        if($status == false){
            //exit routine
       }
    }

    // this method has several sequential steps implemented direcly within it, 
    // for example to a user logging in
    // it makes it impossible to re-use any of the intermediary steps
    public function tangledFlow($input){
         if($input == 'something){
         //100 lines of code
         }
         //then handing on to the next bit
         if($this->User->Authenticated){
         //another 100 lines...
         }
    }
}

Then it occurred to me that I could use a switch statement to control this kind of sequential execution - so my question is: has anyone used a switch statement for this kind of flow control?

share|improve this question
1  
Forgive my ignorance, but I don't understand your question. Are you saying that you don't like sequential statements and methods which invoke other methods? What do you mean by "management" method? –  gablin Nov 3 '10 at 9:43
    
I am not averse to sequential methods - but I think that methods with sequential control should not also contain implementation of the steps in the sequence. My question is not related to this however, more to the concept of using a switch statement in place of the "manageFlow" method –  sunwukung Nov 3 '10 at 11:14
    
A switch can be ok, but if it gets too large, then I prefer to dispatch on a dictionary lookup rather than on a switch. The general approach here is multi-method dispatch. Sometimes having many class hierarchies is the right approach; sometimes it is not. –  Job Dec 9 '11 at 5:09

3 Answers 3

up vote 2 down vote accepted

What you're describing is a state machine, and yes, many people do use switch statements to implement those. They're usually cleaner, but it can be more difficult to follow the logic flow.

share|improve this answer
    
I knew there would be a name for it somewhere. Are there any advantages in using state machines? –  sunwukung Nov 3 '10 at 14:20
    
You can detect certain processing errors by maintaining a list of possible transitions, so your process doesn't skip a step. I often write code generators that generate state machines, because it tends to be easier to verify the generated code. –  TMN Nov 4 '10 at 16:25

Switch statement is conditional statement used to perform different actions based on different conditions. I.e. Switch statement is used to select one of many blocks of code to be executed.

The Switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body.

From my own blog post :

Syntax :

switch (expression)
        {
           case label_1:
             //code to be executed if expression = label_1 ;
           break;
           case label_2:
             //code to be executed if expression = label_2;
           break;
             /*.............
             .............*/
          default:
            //code to be executed if expression is not equal to all label_1, label_2,... etc.
        }
share|improve this answer

In this case, it looks like you're using PHP, so using a switch statement won't have a huge impact on the performance of the code to use a switch vs. if statements in this situation, and there aren't major syntax issues you'd need to worry about.

So, that said, I would say do what you think is going to be more maintainable. If your team favors switch statements over trains of ifs, then use that. If the opposite is true, use that.

You should absolutely be using manageFlow over tangledFlow for your method design, however. tangledFlow tries to do too many things, and as such reduces maintainability (and readability!).

share|improve this answer
    
Of course, I would never advocate using "tangledFlow" - I only wish the people who wrote the project I'm picking up felt the same way! –  sunwukung Nov 3 '10 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.