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.

Additional Resources