Embrace Inheritance

In the last week I've been working on writing a tutorial for our MvcTools component. For this tutorial I've also written a very simple "HelloMvc" application , not only to have something to write the tutorial about, but also to test how the component would do in a more real life situation, outside of the unit testing framework.

I realized during the development of this sample application, is that while writing software, you can never really implement things so that it does exactly what everybody wants and expects. During the development of our MvcTools component we're run into a few of those occasions. One of those is the naming conventions our default controller uses for action's methods. In our first implementation we would always require the method naming of "doActionName()".

Some of our users might want to do something else, but would now have to rewrite the whole class. That is of course unnecessary, as Object Oriented programming excels at reuse of code. However, in order for inheritance to work, all the functionality need to be separated as much as possible into their own methods.

In the re-factored version we now have split out the method name algorithm to its own method, createActionMethodName(). This allows for easier customization of code by inheriting from this class and just overriding this method. It also makes testing easier, as now it is possible to test the name generating algorithm. To illustrate this, we actually found an issue while writing the tests for this new method.

Comments

Hi Derick! I would have some notes on the second solution:

  • I am not sure if it is a good practice to automatically expand the request into variables. For first view maybe it can confuse somebody reading an action code, but of course it is not a big issue while the programmer should know that this is how this framework works. But I think it is much useful if these variables are used for the following purpose: if you set a variables it will be automatically accessible in the view layer.

  • I usually don't like to modify the request, just validate it, and if it is not valid pass back to the user. So I am not sure if it is neat that preg_replace in creating the action name. And maybe it would be logical to do it the dev mode section.

  • While the class's own methods are all protected maybe it would be sexier to define a __call method which throw the not found exception. And it would be faster for the normal flow while not the bad case is the general.

Add Comment

Name:
Email:

Will not be posted. Please leave empty instead of filling in garbage though!
Comment:

Please follow the reStructured Text format. Do not use the comment form to report issues in software, use the relevant issue tracker. I will not answer them here.


All comments are moderated

Life Line