0

Since I am not C++ ninja, as I see dependencies always creeps into my programs. Someone may have asked similar question before, but I want more direct responses. I ask C++ ninjas out there If they can suggest me good references for Idioms supported in C++ to minimize inter dependencies of code.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
NekDil
  • 85
  • 5
  • 3
    I think the tags `c++` and `language-agnostic` are mutually exclusive, so I removed the latter. About *Someone may have asked similar question before, but I want more direct responses* - have you done a search? Please list the related questions and tell us what part of the answers don't satisfy you. Until you do, I vote to close as not constructive. – Björn Pollex Nov 02 '11 at 14:03
  • C++ ninja..I like the sound of that! – MGZero Nov 02 '11 at 14:04
  • 4
    A true ninja would not reveal himself! – hochl Nov 02 '11 at 14:05
  • 2
    @hochl: Nor his dependencies :-) – Kerrek SB Nov 02 '11 at 14:07
  • language agnostic was probably wrong in the sense that i tagged C++ too. I have done a search but didnt find quite useful answers. See http://stackoverflow.com/questions/42308/tool-to-track-include-dependencies, I use `g++ -MM` option to check `#include` dependcies. – NekDil Nov 02 '11 at 14:09

3 Answers3

3

Dependency Injection or some similar mechanism can help decouple the layers of your program. It's, of course, not the universal hammer. As mentioned by parapura, PIMPL is another practice, as is the use of pure virtual base classes as interfaces (similar to COM). You can take it to an even higher level and break parts of your program (where it makes sense to) into its own services accessible via REST or some custom protocol.

None of these are going to supplant the real work you need to do, which is to identify your true separation of concerns in your program. I find it useful to be strict and constantly ask myself "why do I need to expose this" even when it seems obvious I should. Sometimes the answers surprise me after a lot of thought.

So, first, analyze, think, and separate the concerns, then start using various methods and tools to figure out how to achieve it.

Kevin Hsu
  • 1,726
  • 11
  • 14
1

Large-Scale C++ Software Design is a good resource.

Some quick tips to reduce dependencies.

  1. Forward declaration when possible.

  2. Use PIMPL

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
0

Encapsulation. One creates closed classes which are comunicating trough interfaces. All implementation is closed inside (private variable names), for each class a .cpp and .h file with header guards. More about...

6D65
  • 775
  • 6
  • 14