EllisLab text mark
Advanced Search
     
Code Committing Help/Advice
Posted: 20 November 2012 10:14 PM   [ Ignore ]
Joined: 2012-09-10
2 posts

So, I would say that I am pretty new to CI. Been using it for a couple of months and only have been coding for 2 years. Since I am still so new and have mainly worked on really small “local” projects and ideas I am starting to feel a bit uncomfortable with the amount of comments I have placed within my first large project. So, to see what I mean, let me give you an small example of a method from a controller…

public function mileage_submit(){
  
/* Load any helpers for this page */
  
$this->load->helper('form');

  
/* Load any models need for this page */
  
$this->load->model('user'); //used for retrieving user name using CI_SESSION Data
  
$this->load->model('mileage'); // Holds the queries for mileage log entries

  //Load data variables for this page
  
$data['title'"Rayco Mileage Log";
  
$data['css'base_url('/css/style.css'); // Loads the correct stylesheet for the page
  
$submit $this->input->post('submit'); //used to check if submit button was pressed.
  
$date $this->input->post('start'); //used to check if a starting od was entered.
  
$end $this->input->post('end'); // used to check if a ending od was entered.
  
$name $this->user->getName(); // gets the logged in users full name 

  /* When clicking the submit button, pass the $_POST data to the model */
  
if($submit == TRUE && $end == FALSE)
   $q 
$this->mileage->full(
     
$this->user->getName(),
     
$this->input->post('date'TRUE),
     
$this->input->post('start'TRUE),
     
$this->input->post('end'TRUE),
     
$this->input->post('notes'TRUE)
     ); 
   
}else{
   $q 
$this->mileage->update(
     
$this->user->getName(),
     
$this->input->post('date'TRUE),
     
$this->input->post('end'TRUE),
     
$this->input->post('notes'TRUE)
     );
  


  
/* Load the files needed for the view */
  
$this->load->view('includes/head'$data);
  
$this->load->view('includes/header');
  
$this->load->view('mileage_submit');
  
$this->load->view('includes/footer');
 

I’m sure there is a way I can improve this or neaten it up. What are your suggestions?

 
Posted: 21 November 2012 01:44 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts

Add comments that explain WHY you are doing something, where necessary.

Don’t add comments that anyone who understands how to write code can see for themselves.
(ie, $this->load->model(‘my_model’); // loads the model )

Use your variable names to explain your code where you can.

There are some other things in your code people can help with, but that addresses your question on comments

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 21 November 2012 01:46 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-17
1415 posts

I’d recommend using the form validation library anytime you are processing a form. Keep in mind that you have complete access to post() in your model, and that’s the easiest place to do form validation anyways. Community Auth has quite a few examples of form processing and validation done in the models. Couldn’t hurt to check that out if your new to CI.

 Signature 

Brian
Brian’s Web Design - Temecula
Community Auth - CodeIgniter Authentication Application

 
Posted: 21 November 2012 02:36 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2009-04-15
453 posts
$date $this->input->post('start'TRUE); 

Why pass things into a variable, and then not use them?

Don’t call the same function over and over, use your variables.

Also - a shortcut:

$fields $this->input->post(nullTRUE); 

 
cleans your entire $_POST and then sets it to an array where you can access it all

 Signature 

Code By Jeff

Mahana Messaging Library

Problem with your query? Did you run

$this->db->last_query(); 

before you came to the forums for help?

 
Posted: 21 November 2012 08:47 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-09-10
2 posts

Thanks for the input guys. I think I know what my biggest problem is and I guess all of us have went through it in some way or another. Again, this is my first project that I would call big, and I tend to get ADHD after something works correctly. Instead of stopping, taking a step back, and look over the code I just wrote to see if I could re-factor some things to make it more efficient, I get excited. Then I think to myself, “Cool! What if I do this to that!?” And I move on.

Again, thanks!

 
Posted: 21 November 2012 09:34 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2010-08-24
24 posts

I’d recommend docblock style comments so other people and yourself can quickly tell what the method parameters and return type is - although this is not really necessary for controller methods in my opinion. I only really add comments within methods if it’s doing something unusual.

So above each method use something like:

/**
  * Description of method
  * 
  * @param  int
  * @param  array
  * @access public
  * @return  boolean
  */