0

Am i misunderstanding something?

Say i have model method below:

public function product($user_id,$action){
 $this->db->select('product_id');
 $this->db->from('products');
 $this->db->where('user_id', $user_id);
 $query = $this->db->get();

 $counter = 0;
 switch($action){

 case 'ids':
  foreach($query->result() as $row){
  data['ids'][$counter] =  $row->product_id;}
  return $data;
 break;

 case 'count':
 return $query->num_rows();
 break;
}
}

Is the above considered a reusable method since i can use it for 2 purposes?

Or is it not, and is it better to just separate them into two single methods?

Is a reusable method more like this:

public function get_id($table,$value){
 $id = $table.'_id';
 $query = $this->db->get_where($table, array($id => $value));
}

I use simple names for my table and the PK(ID). For example, 'user' table would have 'user_id'. 'product' table would have 'product_id'

Dween
  • 15
  • 6

1 Answers1

0

To be honest I've never heard of a method being called reusable. I've heard of classes (CI context: models and libraries) but never just particular methods. Generally the hallmark of something being reusable is that dependencies are managed and preventing tight coupling. For classes in CI that is kind of difficult to do and can also depend on your code structure as CI apps tend to be inherently tightly coupled.

What I can tell you is that a hallmark of good code from methods to classes is separation of principles. A class that performs CRUD operations on a user shouldn't handle auth functions like logging a user in - the two can interact but shouldn't be together. By extension, a method shouldn't try and do too much. In your case you are not only making your code more verbose by using a switch statement, but also making it harder to maintain down the line by having two different and distinct operations handled by the same method.

Code like this can also be a problem:

public function get_id($table,$value){
 $id = $table.'_id';
 $query = $this->db->get_where($table, array($id => $value));
}

Because if you decide to change one table to not have _id at the end then you would have to either (1) introduce a new function only for that table (2) remove the _id marker and update the parameter $table for every item using this method. Why not just pass the $table name in its entirety and forgo this extra step here?

So in my opinion although all methods are reusable as long as dependencies are managed, other coding practices should be taken into consideration. Introducing new methods costs you nothing.

Alex
  • 9,215
  • 8
  • 39
  • 82
  • So in other words, i'm better off just creating new methods for each new functions im going to add. Should i also separate model methods? I have an insert function with a central table that is referenced by 3-4 other table via Foreignkey. Should i make them separate methods? public function insert_central_table() public function insert_second_table() etc. – Dween Mar 20 '18 at 05:10
  • Why not just make different models with crud functionality for a specific table. So table central gets a model, second table gets a model... That's the orm approach. TBH it ultimately depends on how complex you see the system becoming. Sometimes for throw away sites where quick and dirty will do, it might not even be necessary to be so anal about things. – Alex Mar 20 '18 at 05:18
  • To specify each tables model would have a insert update delete and get function – Alex Mar 20 '18 at 05:20
  • I created multiple model classes but i based creating them on the functionality. For example: for publishing something then i would name it publish_model. For reviewing, review_model. Ofcourse doing this i encounter problems like functions that overlap with other functions. So i made a main model where all those overlaps go there. For example. getting data from tables. – Dween Mar 20 '18 at 05:33
  • I would say that as long as you feel that class isn't too large or unwieldy you should be fine. The ORM approach isn't for everyone. At the end of the day, only you can really be the judge as to what rules/practices you should break or bend. – Alex Mar 20 '18 at 05:37