Monday, 14 January 2013

Tutorial - Write less code but write it better


One of the main problems with so many application developers is that they are happy to sit and write the same CRUD functions again and again and again.  To my fellow developers I say No More!!!


CRUD stands for CREATE, RETRIEVE, UPDATE & DELETE.  The basic code that we keep writing again and again.  About 10 years ago I wrote the CMS package for Libertas Solutions it was a massive undertaking taking me just under 4 man years work of work and all inside of a 2 year period.  I was working 16-20 hours a day solid for 2 years to produce the CMS.  It was and is completely modular allowing the creation of new modules and had three different experiences depending on the level of functionality that you wanted which was aimed at Small, Medium & Enterprise companies.  It was one application that could server all our clients content in one go it was WAI level 3 compliant and Passed Site Morses WAI level 1 completely first time.  The problem was that each module had massive functions to do the CRUD and I wrote 70 modules in the 2 years that I worked on the project.




One thing that I was sure of was that there had to be a better way of coding one that would produce better and more easily read code.  I think most developers when they hit that wall will come up with what is undoubtably the next step in the thinking of developers and that is to atomise the data you are wanting to save.  You normally end up 1 or 2 tables for your system one table will store each field of an object as a row in the database.  Brilliant you say I can now just dump information into this table and extract it at will.  and while you can do this there is one major flaw searching the data is a pain in the arse.  You can end up with 30+ inner joins on a database just to pull the information you need.  the real solution is actually more straight forward.



First you need to realise that the database model you were using originally is actually the correct one for the job what you weren't doing what managing the handling of the records correctly.


Lets say we have a table that stores the following fields about a person

TABLE Person
   id - Primary auto-incrementing key
   name - the name of a person

now lets create a class that represents that table.

Class Person extends DatabaseModel{
    private $primaryKey = "id";
    private $fields = Array("id"=>DB_INT,"name"=>"DB_STRING");

    public $id = null;
    public $name = null;
}

now lets say we build a function in our database class that looks something basically like this

define("
DB_INT", 0);
define("DB_STRING", 1);
define("
DB_DATETIME", 2);

class database{
....
function save($obj){

    if($obj->getPrimaryKeyValue()<0){
          $sql_insert = "........."
          foreach($obj->getFields as $field=>$type){
          }
          return $this->dbObj->execute($sql);
    } else {
         //  do an update using the fields.
    }
}
....
}

With this type of coding style you will write small simpler code ( no it doesn't work with joins its for when you wan to extract record X edit it and save it)

$person = new Person();
$person->name = "Bill Gates";
$db->save($person);

done