- Use namespaces based on the path, and not invent special namespaces
for a single file. This matches with the style guide "choose the name
based on the project's path" and also the dominant usage at Google.
This means that in general we shouldn't have random "file_util" and
"download_drag_util" namespaces. In some cases, classes with static
functions may make sense. In other cases, if you find yourself needing
to group functions like this, it probably means your function names
aren't good enough or your directory structure (and the namespaces
derived from it) don't make sense. As an example, file_util::Foo()
isn't super helpful. One approach might be to move everything
file-related into src/base/filesystem and use base::filesystem::Foo().
- Exceptions to the above rule in common usage at Google are
"myfeature::internal" (meaning "even though this is in a header file
you can use, you shouldn't be calling this") and "myfeature::subtle"
(meaning "even though this is in a header file you can use, you're not
smart enough to use it properly"). The "subtle" one normally indicates
there's a safer way to do what you want for most common uses.
- "Library" toplevel projects like base, app, net, ipc, printing, and
webkit (as opposed to "leaf" projects like chrome) should always use a
namespace matching their directory name. This means that everything in
src/base (yes, even string16) should be in the "base" namespace. Since
these projects can be re-used in different projects, making sure
everything is unique and easy to identify is especially important.
- Subdirectories in these library toplevel projects may or may not use
a separate namespace, but if they do it should be of the form
<toplevel_dir>::<nested_dir> as in "base::win::FooBar()". You don't
need the nested namespace if it doesn't make sense: some
subdirectories are made more for organizing files and don't
necessarily justify separate namespaces, and some subdirectories may
have a naming scheme that separates them without a namespace (all
names in src/net/url_request starts with "URLRequest" so a namespace
"net::url_request::URLRequest" would be redundant). You should be
consistent, either using a nested namespace for everything in a
directory, or only using the parent namespace.
- "Leaf" projects like Chrome and test_shell that aren't built into
anything else may or may not use namespaces based on their root,
depending on project style. It would be nice to require consistent
usage, but it's not practical to add the "chrome" namespace to all
3000 .cc files in src/chrome, and doing so wouldn't get us much, since
it's not designed to be built into anything else.
- If your feature in src/chrome has more than a couple of files, it's
good practice to add a namespace corresponding to that feature (which
should be the same as the directory name). Good examples are "history"
and "downloads". In this case, treat your feature like a library
toplevel project above and be consistent.
- In general, you don't need to use the very general "chrome" or
"browser" namespace unless its usage clarifies something. They can be
useful in some cases as a way of calling out that something like a
constant is the "chrome blahblah" when it might appear overly general
or unclear otherwise, but doesn't help anything for most normal code.
Another example is the code in browser/net which would be better as
"browser::net" so as to not be confused with the "net" toplevel
module's namespace.
- "Leaf" projects like Chrome and test_shell that aren't built into
anything else may or may not use namespaces based on their root,
depending on project style. It would be nice to require consistent
usage, but it's not practical to add the "chrome" namespace to all
3000 .cc files in src/chrome, and doing so wouldn't get us much, since
it's not designed to be built into anything else.
- If your feature in src/chrome has more than a couple of files, it's
good practice to add a namespace corresponding to that feature (which
should be the same as the directory name). Good examples are "history"
and "downloads". In this case, treat your feature like a library
toplevel project above and be consistent.
- In general, you don't need to use the very general "chrome" or
"browser" namespace unless its usage clarifies something. They can be
useful in some cases as a way of calling out that something like a
constant is the "chrome blahblah" when it might appear overly general
or unclear otherwise, but doesn't help anything for most normal code.
Another example is the code in browser/net which would be better as
"browser::net" so as to not be confused with the "net" toplevel
module's namespace.
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
That said, it seems to me that although we agree that it would be nice to require consistent usage, we are giving up on it because it's too much effort for too little gain. While I agree that it's probably not worth the effort to actively spend developers' time moving code into the chrome namespace, I think that it's reasonable to say that all new code under chrome/ should go into the chrome namespace.
while Chrome is currently considered "leaf", I can see some of the code under chrome/ being used outside of chrome/.
PK
Elimination of exceptions => simpler rules. Instead of having to think about library vs leaf projects, just have the simple rule that all new code goes into the toplevel namespace based on path.Rather than asking what the gain is of being consistent, I'd rather ask, what's the gain of being inconsistent? I guess I'm mainly questioning the rationale that because we have a lot of chrome code that isn't in the chrome namespace, new code doesn't have to go into a namespace.
Again, I don't feel too strongly about it, so hopefully you won't engage me in too much debate over this detail, and if you disagree, will just say you disagree, which I'm more than willing to accept :)
Frankly the "leaf" project rule was reverse-engineered from the
"realistically nobody is going to fix chrome" position. We are on a
path to get everything in base, app, net, and gfx basically using the
right namespaces in the foreseeable, if not too-close future. I'd
rather have no-namespace for Chrome than a forever "random bits of the
code use the Chrome namespace so you never know whether to type
chrome:: or not" situation.
Brett
Namespace usage has been a bit ad-hoc. After discussing what to do
with a number of people, the following is an attempt to reconcile the
style guide, common usage in Chrome and at Google, what would be most
clear, and what's practical to do:
- Use namespaces based on the path, and not invent special namespaces
for a single file. This matches with the style guide "choose the name
based on the project's path" and also the dominant usage at Google.
This means that in general we shouldn't have random "file_util" and
"download_drag_util" namespaces. In some cases, classes with static
functions may make sense. In other cases, if you find yourself needing
to group functions like this, it probably means your function names
aren't good enough or your directory structure (and the namespaces
derived from it) don't make sense. As an example, file_util::Foo()
isn't super helpful. One approach might be to move everything
file-related into src/base/filesystem and use base::filesystem::Foo().
I think the advice to avoid dumping ground files (like file_util.h) is still relevant and is different than the issue you are raising?
-Darin
--
I think the advice to avoid dumping ground files (like file_util.h) is still relevant and is different than the issue you are raising?
On Wed, Jun 29, 2016 at 5:40 PM, Darin Fisher <da...@chromium.org> wrote:I think the advice to avoid dumping ground files (like file_util.h) is still relevant and is different than the issue you are raising?
Ya, I'm asking about the advice which says to not make a namespace for a single file, the other option is to leave them in a more general namespace (which makes things less clear), or make a class of only static methods (which the google styleguide explicitly calls out as a no).Concrete examples:https://cs.chromium.org/chromium/src/cc/base/math_util.h - A class of only-static methods to group them under a scope.vshttps://cs.chromium.org/chromium/src/content/common/gpu/client/command_buffer_metrics.h - A namespace for the file to group methods/enums under a scope.I did the latter one recently after reading this part of the Google style guide, but I've seen reviewers asking people not to make a namespace and citing this thread more recently.
On Wed, Jun 29, 2016 at 5:40 PM, Darin Fisher <da...@chromium.org> wrote:I think the advice to avoid dumping ground files (like file_util.h) is still relevant and is different than the issue you are raising?
Ya, I'm asking about the advice which says to not make a namespace for a single file, the other option is to leave them in a more general namespace (which makes things less clear), or make a class of only static methods (which the google styleguide explicitly calls out as a no).Concrete examples:https://cs.chromium.org/chromium/src/cc/base/math_util.h - A class of only-static methods to group them under a scope.vshttps://cs.chromium.org/chromium/src/content/common/gpu/client/command_buffer_metrics.h - A namespace for the file to group methods/enums under a scope.I did the latter one recently after reading this part of the Google style guide, but I've seen reviewers asking people not to make a namespace and citing this thread more recently.
On Wed, Jun 29, 2016 at 5:40 PM, Darin Fisher <da...@chromium.org> wrote:I think the advice to avoid dumping ground files (like file_util.h) is still relevant and is different than the issue you are raising?
Ya, I'm asking about the advice which says to not make a namespace for a single file, the other option is to leave them in a more general namespace (which makes things less clear), or make a class of only static methods (which the google styleguide explicitly calls out as a no).Concrete examples:https://cs.chromium.org/chromium/src/cc/base/math_util.h - A class of only-static methods to group them under a scope.vshttps://cs.chromium.org/chromium/src/content/common/gpu/client/command_buffer_metrics.h - A namespace for the file to group methods/enums under a scope.I did the latter one recently after reading this part of the Google style guide, but I've seen reviewers asking people not to make a namespace and citing this thread more recently.