The Naming of Things - writing more dynamic code

Here's a "mini-tutorial" that shows a trick with using the alias parameter when loading models. But really, one of the points I'd like you to see is the importance of naming your tables and objects in a consistent manner.

Notice I said, "consistent" manner - not "correct" manner. I'm really not one who insists that code be named and styled a specific way - leave that for Python, thank you very much. But if you believe in DRY techniques and keeping your code small and concise, which I hope you do, then using a consistent pattern of names can really save you time spent on the more boring aspects of your work.

Take, for example, the following fairly common scenario:

You have a users table and 4-5 different types of users, all of which have different enough meta data that you prefer to make separate tables for each type to store it. So, you add a field "type" to the users table and then do something like this:

switch ($user_type)
	case 'member':
		$user = $this->member_model->get_member($user_id);
	case 'manager':
		$user = $this->manager_model->get_manager($user_id);
	case 'salesman':
		$user = $this->salesman_model->get_salesman($user_id);

Now - you're a programmer. I don't care what ability level you are at, what language you use, whether you work with jquery or Oracle databases. There are certain "universal programming truths" you have to become aware of if you want to move up and on. Spotting patterns is one of the more important ones. You should have looked at that code and thought, "Damn! That's the same code written over and over. Let's get rid of that."

But how do we refactor it? The answer is what we've already done - look for the pattern.

$user_type is "member". So what this is:

	case 'member':
		$user = $this->member_model->get_member($user_id);

is really just:

	$user = $this->$user_type.'_model'->get_{$user_type}($user_id);

And that's pretty clever, isn't it? Except...while we were able to do this thanks to some good, consistent naming of our model and our get_ function, there's an awful lot of concatenating names together, isn't there? Writing a page full of that is not really the cleanest, and guaranteed to send the best programmer cursing at an endless loop of T_VARIABLE errors.

Let's do two things:

Do we need to call our functions get_member(), get_manager(), etc? Since we are on separate models, why not just call them all get()? (Those of you already using a MY_Model get extra points)

Second, let's use the CodeIgniter "alias" parameter I mentioned above. So now we have:

	$this->load->model($user_type.'_model', 'user_type_model');
	$user = $this->user_type_model->get($user_id);

The second parameter for loading a model is an object alias, so whatever the user type is, we will load THAT model and then call the matching get() function that exists on ALL of them...and we've just shrunk all our messy conditional logic down to two lines of code. All because we did a little planning about how we would call things before we started coding them.

(BTW - to read more about this syntax:

	$user = $this->$user_type.'_model'->get_{$user_type}($user_id);

check & have a look at your System/libraries/Profiler.php for an excellent usage example.

Hope that helped!

Contact me