Sponsored Link •
|
Summary
A very important programming principle is that of separation of concerns. A class should have one clearly defined responsibility. When this is violated problems arise, for instance in the STL.
Advertisement
|
One of the design problems with the STL, is that the containers have multiple responsibilities . Understanding the STL limitations can allow you to more effectively use it, and become a better coder.
In the STL the containers are actually factory classes. They manage object lifespans,and memory allocation. This makes them behave rather strangely as abstract data types (ADT, e.g. stack, list, array, deque). For instance: you might think that you could create a reference to an object within an STL container, but unfortunately the object can be easily moved invalidating your reference silently. This kind of behaviour is frustrating at first, but logical when you consider that an STL collection has to grow/shrink to accomodate insertion/deletion.
In order to deal with this I apply the principle of separation of concerns: I create separate classes for my objects: an object lifetime manager, and an ADT. Usually the object manager is implemented as a linked list of arrays. I then create the appropriate ADT (e.g. tree, linked list, heap) which my algorithms operate on. To quote David Wheeler out of context:
"Any problem in computer science can be solved with another layer of indirection."
Do you have other examples of problems which are solved through application of the principle of separation of concerns?
Have an opinion? Readers have already posted 17 comments about this weblog entry. Why not add yours?
If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.
Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com. |
Sponsored Links
|