On 24/01/16 14:06, Alf P. Steinbach wrote:
> On 1/24/2016 12:28 PM, David Brown wrote:
>> On 23/01/16 18:51, Alf P. Steinbach wrote:
>>>
>>> Lowercase macro names can work when the source code is pretty small. And
>>> we've inherited a few such from the early C days, including `assert` and
>>> `errno` (in C++ the latter is guaranteed to be a macro, e.g. C++11
>>> §19.4/1). But as the source code size increases, so does the risk of
>>> unintended substitutions, and modern code gets large fast.
>>>
>>
>> Why should there be any "inadvertent text substitution" ?
>
> Sorry, I fail to understand that question. It /sounds/ like a
> rhetorical question meant to indicate that you know of a way to avoid
> the problem, and that you're next going to describe that way.
>
>
>> Use macros
>> where appropriate, and in a way that is appropriate - don't use them
>> when they are not appropriate, and don't define them badly.
>
> But this is not a description of a way to avoid the problem.
What problem? "Inadvertent text substitution" just means "I've made a
mistake in my coding that happens to involve a macro", as far as I can
see. It is not fundamentally different from any other kind of mistake.
Now, I am all in favour of programming habits that reduce the risk of
mistakes - and I am quite happy with "don't use macros unless they make
a significant benefit to the code" as such a habit.
What I /don't/ see is the idea that macros are so specially bad that it
is appropriate to disrupt the flow of the code and make them stand out
above all other identifiers.
>
>
>> Using all-caps for macros is just pandering to bad programmers and bad
>> code. We should aim to weed out such bad code, not make it stand out.
>
> I.e., the Boost programmers, including e.g. Dave Abrahams, are in your
> opinion “bad programmers”, producing “bad code”. Hm. I think this
> argument, which involves some /name calling/, is very unconvincing.
>
If these folks /need/ to use all-caps names for macros, then they are
doing something wrong.
Namespace collisions happen. It's almost inevitable. Macros certainly
don't help the matter because they don't follow scope rules, but they
are not the only type of collision possible.
The way to avoid problems with namespace collisions (through macros or
anything else) is, I think, threefold. First, make sure that your
library/header suppliers know what they are doing, and don't use common
names for anything in a wider scope than necessary. This applies to
macros, but it also applies to global functions, types, enumerators,
etc., just as much. Second, make sure that /you/ know what you are
doing, and understand what symbols exist at the different scopes and
namespaces you are using. Third, use tools (such as a good editor or
IDE) that help you get things right.
>
> There is, however, an alternative for those who don't want to use the
> all uppercase convention for macros. Namely, Herb Sutter's advice [1]“If
> you have to write a macro, try to give it an unusual and hard-to-spell
> name that will be less likely to tromp on other names.”.
That sounds like reasonable advice.
>
> The all uppercase convention serves that uniqueness purpose in a simple
> way, usually combined with some short namespace-like prefix.
I understand that. Naming conventions can be a useful thing, and
namespace-like prefixes are an age-old tool for this purpose.
My point is nothing more, and nothing less, than that an all-caps
convention is not necessary for macros, and that this convention
interferes with clear programming. I don't think macros as a class need
a naming convention. And I think that using all-caps encourages the
belief (not followed by everyone here) that macros are someone dangerous
and scary, and need to stand out so that they can be treated with
special care.
>
> When you don't use the all uppercase macro name convention you have to
> compensate with e.g. less collision-likely longer prefixes or suffixes,
> which approach reduces readability, but is a technical possibility.
>
It is all a balance, and will depend on the kind of code being written,
and the size of the project.