How to avoid large switch statements?

I have an internal application in which one function contains too many switching cases.

It is developed in php. This special function is used to record changes to the database, as well as to store the history of individual field values. Thus, this is what takes place for each field, because for different fields it is necessary to apply different things.

switch ($item){ case 'item1': do_something(); case 'item2': do_something_different(): } 

Is there a design pattern in such cases. The function for each element also does not look so promising.

Update: pastebin link

+4
source share
4 answers

This is just not a good feature. It should be three functions: edit_name , edit_manager and edit_liscencedata . you can move all things that repeat between cases into the constructor of the Change class that you must define.

+9
source

You don't talk very much about your code with this sample, but using a state machine is another option. You can take an array and assign the element names as keys, and the values ​​will be a function.

 $machine = array('item1' => do_something, 'item_2' => do_something_different); $machine['item1'](); 

Then it even opens to connect, where you can include a file that reads:

 $machine['item3'] = do_something_else_else; 
+3
source

From a code fragment, provided that it is obvious to you that you are faced with a rather serious case of confusing responsibilities and encoding folder inserts due to lack of factoring. Your editing function processes the material with at least three different layers and three separate objects. The refactoring operations that I most likely applied will extract the Validator classes for each of the different data sets that you process, the model classes are similar for interacting with the database in each case, and the commands for combining the three together: receive data, pass them through validators, then interact with your respective Models to save data. Models will then generate change records based on successful operations.

If you are not inclined to make such significant changes right now, the good first steps will be to separate the different parts of your editing function into other, better-distributed functions and only worry about classes and polymorphism when you deal with this first hurdle. To better understand why your code is broken and what you can do to fix it, I recommend reading something like Refactoring (Martin Fowler) or perhaps a pragmatic programmer.

+1
source

Just a thought, but instead of a switch statement, you can have a capture system available for this ...

so for a list of possible things like

 item1, item2, ..., itemN 

you can try something like

 function item1action ( ) { /* ... */ } function item2action ( ) { /* ... */ } function itemNaction ( ) { /* ... */ } 

and have some default method, for example

 function itemDefaultAction ( ) { /* ... */ } 

so for the previous switch statement you could do

 $function = "{$item}action"; if (! function_exists($function)) { return itemDefaultAction(); } call_user_func($function); return call_user_func($function) 
0
source

All Articles