CodeIgniter Forums
Code Committing Help/Advice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Code Committing Help/Advice (/showthread.php?tid=55997)



Code Committing Help/Advice - El Forum - 11-20-2012

[eluser]Unknown[/eluser]
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...

Code:
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?


Code Committing Help/Advice - El Forum - 11-20-2012

[eluser]jmadsen[/eluser]
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


Code Committing Help/Advice - El Forum - 11-20-2012

[eluser]skunkbad[/eluser]
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.


Code Committing Help/Advice - El Forum - 11-20-2012

[eluser]jmadsen[/eluser]
Code:
$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:

Code:
$fields = $this->input->post(null, TRUE);

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


Code Committing Help/Advice - El Forum - 11-21-2012

[eluser]Unknown[/eluser]
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!


Code Committing Help/Advice - El Forum - 11-21-2012

[eluser]Vega[/eluser]
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:

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