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
Switch or Separate Page?
Moderator: General Moderators
Switch or Separate Page?
Last edited by psurrena on Wed Jun 06, 2007 8:00 am, edited 1 time in total.
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.
See what I'm getting at here? Instead, do something like this.....
Now you've got a lot cleaner code all of a sudden.
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
). 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
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;
}Code: Select all
switch($some_action_var)
{
case 1:
{ $object->method(); break; }
case 2;
{ function(); break; }
}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
Cheers,
BDKR
Last edited by BDKR on Mon Jun 11, 2007 5:31 pm, edited 1 time in total.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Switch or Separate Page?
There are a number of implicit questions here: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?
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)