Switch or Separate Page?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Switch or Separate Page?

Post 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
Last edited by psurrena on Wed Jun 06, 2007 8:00 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Last edited by BDKR on Mon Jun 11, 2007 5:31 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Switch or Separate Page?

Post 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?
(#10850)
Post Reply