Making your apps future-proof

As we all know almost nothing changes as fast as the IT sector.
So in order to keep maintance work on your apps to a minimum, we should make sure that it will still run in several years. We don’t want to spend too much time refactoring and applying new standards. Otherwise we could re-create the whole app from scratch…

Probably the easiest way to keep the refactoring to a minimum is:

  • Use as much wrapper functions as possible (using form helper instead of normal html for example)
  • DRY – don’t repeat yourself (shared function/file for every piece of code you use more that once)

This way you only need to make (minor) changes at a single location. Everywhere you use those wrapper functions the code will automatically be up to date again.

Examples

SQL

// a simple find query via wrapper
$this->find('all', array('order'=>array('field'=>'ASC')));

If one day the SQL syntax for ordering changes from "order by" to "order from" the only thing thats needs to be changed would be the corresponding piece of code in the cakephp core. If you’d used manual queries you would need to run through all your code in order to replace all the bits and pieces containing the old syntax.

PHP

//You might use a specific method in order to sort sth for example
$x = $this->MyLib->doSomething($y);

If your class uses some PHP5.x only functions you will be glad that you have an isolated method you can quickly port to PHP6. Plus it’s easy to test.

HTML
With XHTML elements as well as HTML5 approaching (and god knows what else…) the syntax of HTML will change over and over. In many cases it does make sense to use helpers to ouput HTML. The overhead in time is minimal. The time saving in the future maximal.

Concrete scenarios

HTML Forms with Form Helper
See this source for infos about the new HTML5 elements.

$this->Form->input('number', array('min'=>2, 'max'=>10, 'step'=>2));

Most browsers cannot use all the HTML5 elements yet. But with wrapper functions we can switch in the future with one simply change. We could also enable it for browsers that support it and provide fallbacks (html/js/css) for older browsers. As soon as all browsers understand it we can turn off those fallbacks in the future. Everything in one spot.

PHP
Excerpt from a baked controller of mine (using custom bake templates – see my other article):

function admin_edit($id = null) {
        if (empty($id) || !($discount = $this->Discount->find('first', array('conditions'=>array('Discount.id'=>$id))))) {
            $this->Common->flashMessage(__('invalid record', true), 'error');
            $this->Common->autoRedirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Discount->save($this->data)) {
                $var = $this->data['Discount']['name'];
                $this->Common->flashMessage(sprintf(__('record edit %s saved', true), h($var)), 'success');
                $this->Common->postRedirect(array('action' => 'index'));
            } else {
                $this->Common->flashMessage(__('formContainsErrors', true), 'error');
            }
        }
        if (empty($this->data)) {
            $this->data = $discount;
        }
    }

As you can see we have two methods that are used in all controllers:

  • autoRedirect: trying to redirect back to the previous page (using the referer) if possible
  • postRedirect: redirecting back to the content using correct headers

Now, postRedirect is the interesting part.
As of right now quite a few browsers don’t support the new XHTML status code 303 which is supposed to be used in this case. But maybe in a few months we can simply switch from 302 to 303 by changing this method

Here is the method:

    /**
     * should be a 303, but:
     * Note: Many pre-HTTP/1.1 user agents do not understand the 303 status. When interoperability with such clients is a concern, the 302 status code may be used instead, since most user agents react to a 302 response as described here for 303.
     * @see http://en.wikipedia.org/wiki/Post/Redirect/Get
     * TODO: change to 303 with backwardscompatability for older browsers...
     */
    public function postRedirect($whereTo, $status = 302) {
        $this->Controller->redirect($whereTo, $status);
    }

Of course there are many other examples why it is always good to wrap even stuff as simple as this redirect in wrapper functions.

Outline

Similar to the above for HTML/PHP it is also a good idea to use frameworks and wrappers for JS as well.
For example Jquery/Prototype to only name two big ones.
This is at least as important due to the many different browser versions out there. It really helps to know that those frameworks provide a simple solution that works on all those browsers. Ideally you should only have to update the framework lib in order to be safe for the changes of the future.

Feel free to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.