Editorials

Anorexic Code

Many of the current languages for software development are object based opposed to procedural based. The major difference between the two types of languages is that object based languages contain the procedures implementing the capability of those objects. Procedural languages contain procedures that externally manipulate types. Types are simply complex data constructs, with no internal characteristics other than properties.

This was the major philosophy behind object oriented programming, to have the complex type and all the things that the type could do, be encapsulated into an object. This allowed the programmer to plug types together in meaningful ways, and not duplicate code in different procedures doing the same thing to manipulate data. Objects contained business rules, data validation, state management, serialization capabilities, etc.

Martin Fowler likes to talk about what he calls the Anemic Data Model. This model consists of domain objects having little business logic. Instead domain objects consist mostly of public properties, representing little more than the classic stuct. Fowler puts it, “there is hardly any behavior in these objects.”

How does this work? Instead of encapsulating the procedural code in the objects the system consists of a layer of service objects manipulating the object.

Most of the examples I have seen for the application layer have a great deal of business logic, and the objects themselves know and do very little. So, the application layer becomes very intelligent, and must always be used to manipulate the objects. Much code that should be common to all instances of an object may be duplicated in different application service layers, or additional objects are maintained to manipulate the business objects.

Some of this may seem too much like different philosophies in software design. After taking a look at Fowlers blog where he explains things much better and in more detail, do you think this is the kind of change we need to be making in our objects? Have we become advocates of anemic objects without being aware?

Tell us what you think. Share your thoughts online, or drop me an Email at btaylor@sswug.org.

Cheers,

Ben