Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention between namespaces and classes in C++? [closed]

Creating a library named "snap", I want a class named "log" which I put in a sub-namespace. Is it okay if the namespace is also "log"? (I know that it technically works, I'm wondering about proper naming conventions, what would you do?)

namespace snap
{
  namespace log
  {
    class log {
      ...
      enum log_level_t { ..., LOG_LEVEL_ERROR, ... };
      ...
    };
  }
}

The problem is that we end up with things like these:

snap::log::log::log_level_t ll(snap::log::log::LOG_LEVEL_ERROR);

Does that look strange to you? (i.e. double ::log::log)

like image 822
Alexis Wilke Avatar asked Nov 29 '25 05:11

Alexis Wilke


1 Answers

I don't know why you've defined the enum inside the class. If you have dedicated a `namespace to logging, then define all the logging related stuffs in the namespace. So I would prefer this instead:

namespace snap
{
  namespace log
  {
     enum log_level_t { ..., LOG_LEVEL_ERROR, ... };
     class log { };
  }
}

Moreover, since namespace log itself implies the things which are defiined inside it, are related to logging (or ought to be logging related), I feel the part log in log_level_t seems repetitive. I would further refactor the above into this:

namespace snap
{
  namespace logging //renamed
  {
     enum class level_t{verbose, info, error, ... }; //renamed, and used enum class!

     class logger { ... }; //renamed
  }
}

Or maybe, I would choose the name severity instead of level_t. Anyway, the usage becomes a bit better in my opinion:

logger.write(snap::logging::severity::error, message, etc);

Or you could write some friendly function, so that you could write this:

logger.verbose(message, etc);
logger.info(message, etc);
logger.error(message, etc);
//etc

Hope that helps.

like image 108
Nawaz Avatar answered Nov 30 '25 20:11

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!