phppythonrubyoopself-healing

OO Patterns and/or Structured Approach for "Self Healing"?


Many web application projects I've been involved with reach a point where

  1. The application expects persisted data to be in a particular format

  2. The application will barf if the persisted data strays from that format

  3. Old "mystery code" is persisting data in the bad format

This usually results in the application developers cluttering the model code with lots of validation conditionals. That is

function save()
{
    if($model->getSomeProp() == 'bad value')
    {
        $model->setSomeProp('good default value');
    }

    return parent::save();
}        

Are there better patterns and/or systems for dealing with these situations, with said patterns and/or systems not relying on having the developers writing perfect migration scripts and/or validation code for every release? My specific interest is in how other developers approach cleaning up these sorts of (in my experience) common long-term problems.

Specifically looking for a LAMP Stack/PHP solution, but solutions and approaches from other common middleware languages/platforms (ruby, python, etc.) are more than welcome.


Solution

  • We use configuration-file provided list of steps that should be performed on every processed item. That enables validation, slight changes of data, lookups, retrieval and merging of certain attributes from external sources etc.

    Although right now it is based on a set of Ruby classes that implement abstract Step, working according to yaml configuration, I guess that in the next rewrite I would go with pure Ruby DSL.

    So at the end, you would have something like this:

    HealingProcessor.on(impure_data) {
      replace_bad_value :field => :some_prop, :bad_value => 'bad value', :good_value => 'good_default_value'
      # etc
    }