#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Posts
    2,568
    Rep Power
    171

    Combine multiple functions?


    Hi;

    Would it be a good idea to combine simular functions to save space?

    Or is it best to keep them seperate?

    For example I have many functions like these
    PHP Code:
    public function validate_city($city)
            {
                if(
    $city == '0')
                    {
                        
    $this->form_validation->set_message('validate_city''Please select a city');
                        return 
    false;
                    }
                else
                    {
                        return 
    true;
                    }    
            }

        public function 
    validate_suburb($suburb)
            {
                if(
    $suburb == '0')
                    {
                        
    $this->form_validation->set_message('validate_suburb''Please select a suburb');
                        return 
    false;
                    }
                else
                    {
                        return 
    true;
                    }
            } 
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Hi,

    you should definitely reduce that to a single function, because the logic is always the same:

    PHP Code:
    public function validate_selection($value$message_identifier$error_message)
    {
        if (
    $value == '0')
        {
            
    $this->form_validation->set_message($message_identifier$error_message);
            return 
    false;
        }
        else
            return 
    true;


IMN logo majestic logo threadwatch logo seochat tools logo