Skip to content

Basic C++ & Singleton_registry - Suggestions to GrantRostig from an expert. #2

@grantrostig

Description

@grantrostig

https://github.com/cppmsg/cpp_by_example/blob/master/designp_singleton/singleton_gof_registry.hpp
I've taken a look at the code in that header file.
As for some suggestions:

  1. That .hpp file has both declarations in it as well as function definitions. I'd suggest moving those function definitions into one or more source files or prefixing them with the "inline" keyword.
  2. The formatting is such that some lines have multiple statements on the same line. It's more common to see only a single statement per line and will be easier to read for people used to that - like myself.
  3. I see that file has a "using namespace..." statement in it. Those pollute the namespace for any users of that header file. I suggest removing it.
  4. The code uses a mix of qualified uses of standard library names and unqualified uses. Use only namespace qualified names for types from the standard library. So prefix mentions of types like "string" with "std" so those instead appear like "std::string".
  5. Come up with a more consistent formatting style and use it everywhere within that file. Qt Creator can be configured to auto format to some standard styles and that could be used to help for that.
  6. Remove the include of <bits/stdc++.h> from that file.
  7. Specifically include the standard library header files that are needed for that header file to stand alone. So for instance, there'd be includes like for "", "", "", "".
  8. Don't use "endl". That appends a newline AND forces a flush of the stream buffer. If you really want both, use the newline character ('\n') or string ("\n") and follow it with an explicit "std::flush".
  9. Pick a max column width no more than like 120 for the code and then enforce that with newlines.
  10. "Singleton_gof_registry::_registry" does not need to be a pointer to a "Registry". That's basically a pointer to a pointer to a dynamically allocated array. Make it just of type "Registry" and then it doesn't need to be "new" allocated on first use.
  11. Don't mix uses of like "end()" member function values with "cend()" member function values.
  12. If you want to provide exclusive access to one section of code so another thread can't access those same variables, those sections need to all use the same "std::mutex" (or a different mechanism than "std::mutex" has to be used instead). So add "mtx" as a private member variable of "Singleton_gof_registry" and then the three member functions which use a "std::mutex" would all use the "mtx" member variable instead.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions