Hendrik Schober
9/28/2008 8:01:00 PM
Mark Casternoff wrote:
> I'm getting back into C++ after a long hiatus (they didn't have
> namespaces back then).
>
> I know this question is completely subjective, but I'd be interested in
> hearing which is
> the "better" style and what the pros and cons are (I'm using cout as
> example, but it really
> applies to any similar construct):
>
> 1) using std::cout;
> cout << "This is a test";
>
> 2) std::cout << "This is a test";
>
> The difference being prefixing every cout with 'std' or declaring it
> ahead of time. The second
> method could be used if cout existed in more than one spot, but how
> often is that an issue?
>
> I've seen both methods in code, but I'm seeing #2 a lot more frequently.
Here's my EUR 0.02:
I have heard lots of complaints that #2 produces longish, unreadable
code from people who shy from it and never used it.
OTOH, I have used #2 for about a decade now and worked on projects
where it was required that /all/ identifiers have to be within some
namespaces and that /all/ identifiers are to be addressed with their
fully qualified name. So I /know/ that it isn't hard to read code
which fully qualifies all identifiers after a short while of looking
at such code (that's a "short" as in "weeks" or "months"). And this
isn't only my personal POV. Most of the time I worked under the rule
that using declarations (and even using directives!) where allowed
within any local scopes not bigger than a function, but this was
rarely ever used by anyone (with "rarely" meaning "far less than 1%
of the project's code"). I suppose that was because, after a short
while, people who really tried to work that way liked code better
that used fully qualified identifiers. (I abhor code that uses a
naked 'list' beside a naked 'cout', as I have no idea whether that
is 'std::list' or some other identifier.)
What I've learned, though, is to avoid creating namespaces which are
long and hard to type right even after weeks ('StringUtility') of
usage. :)
HTH,
Schobi