Data Driven Development

Recently I am frequently hearing the question “What is the Data Driven Development?” Let’s look into the problem with help of example. We will create an aquarium. And it is not a joke.

Let’s formulate our goal at the beginning. We want to make a model of the aquarium with small fishes in it.

We will create the aquarium by the traditional OOP way. Our model consists from two main things – the aquarium itself and all objects which can be placed in the aquarium. In our case these objects are small fishes and plants. Any fish can swim, eat food and do many other things.

So the object hierarchy can be as at the picture below.

If we need to create new fish or plant we have to inherit from correct object and overload few methods only. The program architecture we got is very flexible. For example it is easy to add a toy submarine in the aquarium.

However such solution has a problem. Let we have need to add a new property to the fish, for example it can be a text description. We have to look up all our fish’s classes and add there correct description for each fish. What shell we do if it is necessary to create 20 different fishes? Making of 20 classes which are almost the same is not most interesting work. What about adding of two new properties to these 20 classes? What to do if there will be 5 properties and 50 classes? It is obvious we need to change something in our architecture.

The good solution of this problem is making unnecessary of inheritance for each type of fish. We can create a single class which will be initialized from the table instead of inheritance. It can look as below (C++).

enum FISH_FOOD
{
INSECTS = 0x01,
WORMS = 0x02,
GRUBS = 0x04,
};

enum BEHAVIOR
{
SHOAL = 0x01,
CAMOUFLAGE = 0x02,
FEAR = 0x04,
};

struct FISH
{
const char * name;
int effective_size;
int food;
int behavior;
} fish[] = {
{"guppy", 10, INSECTS GRUBS, SHOAL},
{"neon", 12, WORMS INSECTS, SHOAL CAMOUFLAGE FEAR},
{"golden fish", 8, GRUBS, FEAR},
{NULL, 0, 0}
};


class Fish
{
void Eat() {}
public:
Fish(const char * name) {}
void Move() {}
};


int main()
{
Fish guppy1("guppy");
Fish guppy2("guppy");
return 0;
}

In this case we have to add only one line of data to the table to make the new type of fish. An experts, who asking to add new fishes in our aquarium, can make there requirements in the form we can place to the program without changing.

This simple example illustrates what is Data Driven Method. It has disadvantages. Main disadvantage is loosing of flexibility. We could create unique behavior for each fish before. Now such unique behavior will be pressed by the borders of few parameters for one algorithm. Of course, we can create “switch-case” instructions which return flexibility we loose. However, excess of “switch-case” leads to heavily supported code.

3 thoughts on “Data Driven Development”

  1. What about using sets of strategies, where you can define a strategy by the combined value of the fish properties?

    For instance, if a fearful fish is afraid of grub, you need to combine it with the food strategy, but mostly you wouldn’t.

    So it would be much easier to modify.

    Like

  2. Did you mean (in terms of our aquarium) next thing:
    Let’s define strategy as

    struct STRATEGY {
    int stragey_name;
    int food;
    int behavior;
    } strategies[] =
    {
    {AGRESSIVE, GRUBS | WORMS | INSECTS, SHOAL},
    {COMMON, GRUBS | WORMS | INSECTS, SHOAL},
    {AFRAID, GRUBS, 0}
    }

    And “fish” will

    struct FISH
    {
    const char * fish_name;
    int effective_size;
    int strategy;
    } fishes[] =
    {
    {“guppy”, 10, AGRESSIVE},
    {“golden fish”, 6, AFRAID},
    }

    In such case by the extracting few common strategies we can significantly improve our code. Most of the same data from the “fish” table will be removed and replaced by one reference to the strategy. Moreover, if we add new property to the strategy we will have fewer places to modify. I think it is good next step of aquarium development.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s