Page 1 of 1

Switch or Separate Page?

Posted: Wed Jun 06, 2007 7:04 am
by psurrena
At which point, or is there one, should you stop using the switch statement? I like it when I have 3 or so "subpages" of similar content. Is there a rule of thumb or does it not matter?

Regards,
Peter

Posted: Wed Jun 06, 2007 7:51 am
by feyd
If you have large amounts of code in each case, switch may not be best as it becomes harder to read often. Switches are often used for quick decision forks.

Posted: Mon Jun 11, 2007 4:46 pm
by BDKR
It's hard to say when but my feeling is that only functionality that is related should be in the same switch. For example, if you are building some reservation functionality, only processes that are related (in some way) should be included in that switch. Try thinking in terms of Responsibility Driven Design.

As for the readability of the switch, it can be very clear if you insist on abstraction. Here is what you don't do as an example.

Code: Select all

switch($some_action_var)
    {
    case 1:
        /*
        ......
        ......
        300 lines of code here
        ......
        ......
        */
        break;

    case 2:
        /* 
        .....
        .....
        Another 300 lines of code here
        .....
        .....
        */
        break;
    }
See what I'm getting at here? Instead, do something like this.....

Code: Select all

switch($some_action_var)
    {
    case 1:
        { $object->method(); break; }
    case 2;
        { function(); break; }
    }
Now you've got a lot cleaner code all of a sudden. :wink:

Lastly, if you are so inclined, take a gander at the MVC pattern. A controller (the "C" in MVC) is a switch that's mapped onto an object (or at least it is in Rails :lol: ). Now that's not the most correct way to put really. I should say that a controller is an object and methods are analogous to case statements.

Cheers,
BDKR

Re: Switch or Separate Page?

Posted: Mon Jun 11, 2007 5:10 pm
by Christopher
psurrena wrote:At which point, or is there one, should you stop using the switch statement? I like it when I have 3 or so "subpages" of similar content. Is there a rule of thumb or does it not matter?
There are a number of implicit questions here:

1. Should you use a switch statement for this in the first place?

2. If you don't use a switch, then what would you use?

3. What are considered best practice solutions to your problem?