How do you all feel about class composition? say im making a class i would want to publish to the public for use...well if i perform class composition this means i would also have to implement the other classes/structs so the programmer can even properly use my class. Im just curious...would u prefer to stay away from composition or use it always when you see suitable for this type of situation?

Recommended Answers

All 2 Replies

If it is a complicated structure, there is probably not a better way than to require the user to use multiple of your classes (the classes inside (composing) the big class). Sounds fine to me.

Usually if it makes sense to you, it will make sense to others, given the proper documentation. Whether your code is to be published or not does not make a difference because you should always write your code as if it was to be published. Put care into thinking about solutions to your problem. Composition is generally good practice and most programmers are very accustomed to it, and it is often the better alternative compared to multiple inheritance or semantically ill-conceived class hierarchies. Often, when composition is useful, generic policy classes are an even better alternative (look up the subject). Also, it is a good idea to provide a default composition if it makes sense to do so, it will ease the "typical" user's burden.

Remember that you never know when you might come back around, much later, and look at your own code and try to understand what you did or how to reuse it. So, I always say, there is little difference between another programmer and your-future-self. So, whether you want to publish the code or not, you still want to organize it and to document it properly. But no need to go crazy with the documentation, you don't need to have like a comment for every line of code. Mostly, the best thing is to just give a good clear (doxygen) comment block to each function you have (with all the tags filled, like: \pre, \post, \param, \return, \author, \date, \throw, \test, \todo, etc.). That also has the added benefit of forcing you to clearly explain the purpose of a particular function or class, if you have trouble doing that, you should question your software's design.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.