Do you prefer explicit namespaces or 'using' in C++?

前端 未结 8 912
旧巷少年郎
旧巷少年郎 2020-12-06 01:16

When using C++ namespaces, do you prefer to explicitly name them, like this:

std::cout << \"Hello, world!\\n\";

Or do you prefer

8条回答
  •  甜味超标
    2020-12-06 01:38

    Extra typing is not the issue here. The problem with explicitly qualified names is the visual clutter. Let's face it, C++ syntax is untidy. No need to make this worse by needlessly making names longer and sprinkling the code generously with ::s.

    I'm with Jeff Atwood: The Best Code is No Code At All. This is so true.

    Namespace imports are a great way of reducing clutter with no drawback: As long as the scope of opened namespaces is reduced to a single compilation unit1, name conflicts, should they appear, can be resolved easily.

    Why explicit names should (in general) be more readable has always been a mystery to me. The readers should generally know the code good enough to be able to deduce semantics. If they aren't, the code needs fixing anyway.


    1) Corollary: no using in headers!

提交回复
热议问题