Welcome Guest, Not a member yet? Register   Sign In
Switch for code organizing?
#1

[eluser]James Pax[/eluser]
Hey guyz,

I'm trying to make my code more organized and efficient so I don't end up badly when the classes and methods are to many to cope with.

So I thought I might split a Controller into different sectionslike this

Code:
public function section1($type)
{

      switch($type)

      case 'addItem':
      
      #code

      break;


      ...


      default:


      break;

}

instead of:

Code:
public function addItem($item)
{

#code

}

...

I intend on passing the variables though post method to the controller,

my thoughts are will it be more efficient like Binary Search Tree or is it just useless spaghetti coding lol

good tips on organizing my code are very very welcome Big Grin
#2

[eluser]bradym[/eluser]
I would not suggest putting everything in one method and using switch to execute the right code. It could get very difficult to maintain as you add functionality, and later when you come back to it (or a co-worker needs to edit your code) it will be more difficult to see at a glance what is going on.

When you separate your code into methods that each have a specific function and are called where needed, it is easier to follow later, and is also easier to re-use from other parts of your application.

I really don't think you'd get any efficiency boost by having everything in one method, and any efficiency gained by doing that is offset by the costs mentioned above.

Brady




Theme © iAndrew 2016 - Forum software by © MyBB