How to better organize code in C ++ projects

I'm currently trying to better organize my code.

To do this, I used namespaces, grouping classes by components, each of which has a specific role and several interfaces (in fact, abstract classes).

It seemed to me that this is very good, especially when I had to rewrite the entire component, and I had almost no effect on the rest. (I find it would be a lot harder with a bunch of mixed classes and methods)

But I'm not 100% satisfied with this. Especially I would like to make a better separation between the interfaces, the public face of the components and their implementation behind. I think that the “interface” of the component itself should be clearer, I mean, the new visitor should easily understand which interfaces he should implement, which interfaces he can use and which part of the implementation.

Soon, I launched a larger project involving up to 5 developers, and I would like to clearly understand this issue.

what about you? how do you do this? How do you organize your code?

+4
source share
5 answers

Especially I would like to make better the separation between the interfaces, the public face of the components and their implementation behind.

I think you are looking for a Facade pattern:

A facade is an object that provides a simplified interface for most of the code, such as a class library. - Wikipedia

You can also watch Mediator and Builder if you have complex interactions in your classes.

The Pimpl racetrack (also known as the compiler firewall) is also useful for hiding implementation details and reducing build time. I prefer to use Pimpl over class classes + factory when I don't need polymorphism. Be careful not to use it. Do not use Pimpl for lightweight types that are usually allocated on the stack (for example, a three-dimensional dot or a complex number). Use it for larger, longer-lived classes that have dependencies on other classes / libraries that you would like to hide from the user.

In large-scale projects, it is important not to use the #include directives in the header file when a simple direct declaration is executed. Place the #include directives in the header file if absolutely necessary (prefer to put #includes in the implementation files). If done correctly, the correct #include discipline will significantly reduce compilation time. The Pimpl idiom can help move #includes from header files to corresponding implementation files.

A coherent set of classes / functions can be grouped into its own namespace and placed in a subdirectory of your source tree (the subdirectory must have the same name as the library namespace). You can then create a sub-project / makefile of the static library for this package and link it to your main application. This is what I consider a “package” in the UML jargon. In an ideal package, classes are closely related to each other, but freely related to classes outside the package. It is useful to draw dependency diagrams of your packages to make sure there are no circular dependencies.

+3
source

There are two general approaches:

If, in addition to the public interface, you only need helper functions, just put them in an unnamed namespace in the implementation file:

// header: class MyClass { // interface etc. }; // source file: namespace { void someHelper() {} } // ... MyClass implementation 

If you find that you want to hide member functions, consider using the PIMPL idiom:

 class MyClassImpl; // forward declaration class MyClass { public: // public interface private: MyClassImpl* pimpl; }; 

MyClassImpl implements the functionality, and MyClass redirects calls to the open interface for private implementation.

+4
source

You can find some suggestions in the Large Scale C ++ Software Design . It's a bit dated (published in 1996), but still valuable, with pointers to structuring the code to minimize "recompiling the world when one header file changes."

+1
source

Herb Sutter's “ What's in the Class? Interface Principle ” article presents some ideas that many don't seem to think about when designing interfaces. This is a bit dated (1998), but there is something useful.

0
source

First, declare variables that you can use them in a single line declaration, as well.

  char Name[100],Name2[100],Name3[100]; using namespace std; int main(){ } 

and if you have a long piece of code that can be used outside the program, make it a new function. likeso.

 void Return(char Word[100]){ cout<<Word; cin.ignore(); } int main(){ Return("Hello"); } 

And I suggest any external functions and declarations that you put in the header file and link it like this Dev-C ++ #include "Resource.h"

-6
source

Source: https://habr.com/ru/post/1299034/


All Articles