7.4.2.  AntiPatterns

[ fromfile: antipatterns.xml id: antipatterns ]

AntiPattern is a term first coined by [Koenig95] to describe a commonly used programming practice that has proved to be ineffective, inefficient, or otherwise counterproductive. Several antiPatterns arose as solutions to recurring problems and have been picked up and passed on by students and other inexperienced programmers. There is an informative and evolving article on this subject in Wikipedia that organizes and briefly describes a substantial number of antiPatterns. Discussing examples of antiPatterns may help programmers avoid such pitfalls. Following is a small selection of named antiPatterns from the Wikipedia article.

In Figure 7.3, Customer includes member functions for importing and exporting its individual data members in XML format.

Figure 7.3.  AntiPattern Example

AntiPattern Example

getWidget() provides a special GUI widget that the user can use to enter data from a graphical application. In addition, there are friend functions for input/output via iostream.[60]

This class is a model because it holds onto data and represents some abstract entity. However, this class also contains view code because of the createWidget() and getWidget() member functions. In addition, it contains serialization code specific to particular I/O streams.[61] That is too much responsibility for a data model. This is an example of the Interface Bloat antiPattern (and perhaps a few others also).

The problems that interface bloat can lead to become immediately apparent when you implement other data model classes such as Address, ShoppingCart, Catalog, CatalogItem, etc. Each of them would also need these methods:

This could lead to the use of Copy-and-Paste programming, another antiPattern.

If you ever change the data structure, corresponding changes would need to be made to all presentation and I/O methods. Bugs are likely to be introduced when maintaining this code. If Customer were reflective, meaning that it had the ability to determine useful things about its own members (e.g., How many properties? What are their names? What are their types? How can they be loaded/stored? What are the child objects?), then you could define a generic way to read and write objects that would work for Customer and any other similarly reflective class. Chapter 12 discusses an example that shows how to write more general-purpose code with reflection.



[60] Friends are discussed in Section 2.6.

[61] Serialization is the process of converting an object's data to a form that permits the data to be stored in a file or transmitted across a network so that it can later be used to reconstruct the object. Section 7.4.1 discusses the Serializer Pattern.