0

Using primitive data types to represent domain ideas is code smell called Primitive Obsession. For example using string data type to represent human name - it is recomended to avoid this by defining domain specific value object with apropriate methods.

But what about pointers?

Is it not primitive obsession to use CItem* or shared_ptr instead of more domain specific types like CItemHandle, CItemLink etc. ?

Edit:

My viewpoint with this question is that usefulness of type is not only about what behaviors it allows, but also about what behavior it don't allows. Primitive type gives you some set of constraints. User defined types can give you different set of constraints. Thanks to this, user defined type can clearly model domain by using domain constraint and domaing wording in its interface and this also helps with promoting right thinking and in result right decision when making changes in system. User defined type which encapsulates pointer simply gives you more precisse control than pointer itself to enforce proper use of it.

3
  • 1
    What disadvantages does the pointer have, or rather, what problems would a CItemHandle class solve? Using a bare string for a human name is bad because, among other things, the type is non-descriptive, it may allow meaningless operations, and it can facilitate semantic confusions. Not because some deity proclaimed that Thou Shalt Not Use Primitives.
    – user7043
    Feb 8, 2015 at 1:57
  • 1
    There is no pattern or anti-pattern that is an absolute, including this one. Just because using primitive types can be an anti-pattern will never mean that that is always the case. Case in point, C++ pointers are a form of responsibility management, which is why we have things like shared_ptr, weak_ptr, and unique_ptr.
    – greyfade
    Feb 8, 2015 at 2:00
  • To answer your question, you must first ask: what do you gain from using a user-defined type over a primitive type? You must answer this by saying what behaviors you will be able to add to your proposed user-defined type, such that it will enhance the functionality of your software. Therefore, to answer your original question, please think about what behaviors you would like to add to CItem* by replacing it with UDT. Thanks.
    – rwong
    Feb 8, 2015 at 3:10

1 Answer 1

2

Object pointers are not primitives in the sense that strings or ints are. A PersonName* is not a Genus* and its not a ProductDecription*. Unsurprisingly, the equivalent to primitives for pointers is pointers to primitives.

Adding an extra level of indirection won't change that either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.