Part of the EllisLab Network
   
 
Controller code.. your opinion?
Posted: 16 October 2008 11:31 AM   [ Ignore ]  
Summer Student
Total Posts:  11
Joined  10-16-2008

Hello there, i had a disscussion with my friend about code align in controllers. Consider these examples:

A) separate functions

function add_user() {
  // code goes gere
}

function edit_user(){
}

function del_user(){
}

B) topic-related function

function user($action){
switch($action) {
 
  case ‘add’:
  // code goes here
  break;

  case ‘edit’:
  break;
 
case ‘del’:
  break;
}
}

Which one is the better way how to organize code in controllers and why is that so?
Thx for answers smile

Profile
 
 
Posted: 16 October 2008 11:39 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  374
Joined  05-04-2008

I always use the first version, it’s probably because when I used procedural code I had different files for each action, and it just stuck. I can see the benefits of having all the edit functions in one controller, for example.

Although it would get messy if you had a modular structure like I use now, which is why I stick with the first. But then again I would have the whole function in a model and just run it from the controller.

 Signature 

[ Adam Griffiths - Freelance Web Applications Developer ]
[ Follow me on Twitter ]

Profile
 
 
Posted: 16 October 2008 01:39 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  09-09-2008

With CI I use the first one, but before I always used switches.

Profile
 
 
Posted: 16 October 2008 02:03 PM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

The first way makes you write code that is spread out more which makes it easier to eyes.
The second way you can look for code that is similar in the different actions and you can make it DRY.

I use the second way and put the code in the cases up to ten lines per case, if it’s more i use methods so it’s a third way smile

function user($action)
{
   
switch($action)
   
{
      
case 'add': break;
      case 
'edit'$this->_edit(); break;
      case 
'delete': break;
   
}
}

function _edit()
{

Profile
 
 
Posted: 16 October 2008 04:54 PM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  686
Joined  05-28-2008

I use the first. Always. Personally, I think it’s a better coding practice. I also think the first way is a tad faster. Not sure though.

 Signature 

Bramme.net webdevelopment
If I had a nickel for every time someone told me that my idea for melting down coins to make a giant robotic parrot was a bad idea, I would have one kicka$$ giant robotic parrot.

Profile
 
 
Posted: 17 October 2008 12:15 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  74
Joined  09-13-2007

I would use A as well. It seems to me to be easier to maintain specific functions than have larger switched functions.  Besides, the whole point of object oriented programming is to group functions and variables into data types.  Effectively giving you the badass version of method B.

 Signature 

www.daytonnolan.com

Profile
 
 
Posted: 17 October 2008 12:29 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  180
Joined  10-28-2006

Method A.  No one has mentioned this, but using style A allows for very exact naming in both your functions and your urls if that matters.

Using style A as it is above, you can have nice clean urls like:

http://mydomain.com/users
http://mydomain.com/users/add
http://mydomain.com/users/edit
http://mydomain.com/users/delete

You get the idea. Using your imagination a little, CI becomes one heck of a search engine optimization tool since it allows you to craft how your urls will appear.

 Signature 

“Clean, Simple & Elegant Web Design”

Profile
 
 
Posted: 17 October 2008 05:43 PM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

Joe you have to use routing with method A you have nice urls with method B

Profile
 
 
Posted: 17 October 2008 06:24 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  119
Joined  06-06-2007

I did something similar today. I had a controller method for adding/editing records, using the same html form.

Bu I wanted urls like admin/edit and admin/new, so I did:

function record($id NULL){
  
// if $id == null then it's an add, else it's an edit

  // show the form etc
}

function add(){
  $this
->record();
}

function edit($id){
  $this
->record($id);

Keeps the code in one place but looks nice too!

Mei

 Signature 

Web Development in north Wales
mei.gwilym.net

Profile
 
 
Posted: 17 October 2008 06:40 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  180
Joined  10-28-2006

sorry, see below

 Signature 

“Clean, Simple & Elegant Web Design”

Profile
 
 
Posted: 17 October 2008 06:43 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  180
Joined  10-28-2006
xwero - 17 October 2008 09:43 PM

Joe you have to use routing with method A you have nice urls with method B

Aren’t we always?  Doesn’t the routing begin with the index.php examining our uri segments?

 Signature 

“Clean, Simple & Elegant Web Design”

Profile
 
 
Posted: 17 October 2008 07:50 PM   [ Ignore ]   [ # 11 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

With B you don’t have to route to use the user/add segments, for A you have to use the route

$route['(user)/(add|edit|delete)''users/$2_$1'

I’m using the code from the starter topic.

Profile
 
 
Posted: 21 October 2008 03:50 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  11
Joined  10-16-2008

Thanks guys

Profile
 
 
Posted: 21 October 2008 05:26 AM   [ Ignore ]   [ # 13 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  268
Joined  07-31-2008

just a note: I use add and edit often in the same function call (often it is also the same view with the same form). If an id has been transfered, I would need to mutate a DB-record, otherwise I do an insert.

 Signature 

RostElyn - Russian Language in Switzerland
TableEditor: No more tirying CRUD-code

Profile
 
 
Posted: 21 October 2008 09:42 AM   [ Ignore ]   [ # 14 ]  
Grad Student
Avatar
Rank
Total Posts:  78
Joined  09-13-2006

Hello,

from my point of view, if you think about CI and MVC style, the controller class is the “topic” (who), the controller method is the “action” (what), and the other segments are the arguments.
That’s why I’ll go for option A.

Profile
 
 
Posted: 21 October 2008 10:15 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  686
Joined  05-24-2007

@dready This works perfectly for relatively small controllers (<100 functions) but for larger controllers I find method B to be preffered. Of course, this is just how I like to work.

One can also split controllers but then the theory/practice of topic-method-arguments is busted.

So for small controllers I go for method A, Large controllers I go for method B, extremely large controllers I go for method C grin

 Signature 

The art of managing is to explain tomorrow why yesterdays solution doesn’t work today.
livecodes.eu

Profile