Welcome Guest, Not a member yet? Register   Sign In
Implications in managing logged in status with if vs if-then-else
#1

[eluser]stuartr[/eluser]
I am using TankAuth for managing log ins and access to functions, and have a quick question.

Sometimes I check the log in status with a "if not logged in => redirect", before going on to complete the controller actions. Sometimes I use a full if then else statement where the logged in actions are contained within the else statement (see below for examples of each). Apart from the fact that method 2 seems more complete, is there any reason I shouldn't be using method 1. What are the pros and cons of each (if any?)

TIA

Method 1 - If statement
Code:
function method1()
{
  if(!$this->tank_auth->is_admin_logged_in())
  {
   // Admin not logged in
      redirect('admin/admin_login');
  }
   $data['recent_schools'] = $this->admin_model->get_registered_schools(30);
  
   // Get filter lists
   $data['country_list'] = $this->school_model->get_country_options();
        
   $this->lang->load('common');
   $this->lang->load('admin/admin_common');
   $this->lang->load('admin/schools');
         $this->_load_a_page('admin/new_schools', $data);
}

Method 2 - If then else
Code:
function method2()
{
  if(!$this->tank_auth->is_admin_logged_in())
  {
   // Admin not logged in
      redirect('admin/admin_login');
  }
  else
  {  
   $data['recent_schools'] = $this->admin_model->get_registered_schools(30);
  
   // Get filter lists
   $data['country_list'] = $this->school_model->get_country_options();
        
   $this->lang->load('common');
   $this->lang->load('admin/admin_common');
   $this->lang->load('admin/schools');
         $this->_load_a_page('admin/new_schools', $data);
  }
}
#2

[eluser]Unknown[/eluser]
I saw you haven't had a reply in days, so I figured I would at least offer up my opinion, and it is only my opinion.

I personally always use an if/else statement, especially when dealing with anything that could be construed as security. To rephrase, I never leave off the else. Several reasons for this:

1. I don't want to take the risk of somehow the if (without the else) being skipped over and my script continue to run at its own will.

2. Easier to read later on when you come back to edit your code.

3. It just seems logical to me. To me it's the equivalent of using a try/catch without the catch.

I'm not going to give a spill about it's best practice or dive into any concrete theory, but it's only six extra characters for a little extra peace at night when you lay your head down on your pillow.

Happy coding!

#3

[eluser]Ckirk[/eluser]
Hadn't seen this so I'll add my view.

If the action is to go no further in the current function then method 1 is always my method of choice. That decisive action means that the code will go no further so putting an 'else' condition afterwards is pointless. Might be pretty but it serves no purpose
#4

[eluser]Pert[/eluser]
Have to agree with Ckirk on this one.

Better example, instead of try/catch is returning a function result.

You could set a return value and the final line in the function returns that value.

Or you could make a decision early in the function that you have come to final conclusion and you can return this value without having to execute any following code.




Theme © iAndrew 2016 - Forum software by © MyBB