Philip Lantz said:
Tim said:
Philip said:
pete wrote:
Tim Rentsch wrote:
pete writes:
Philip Lantz wrote:
[..snip..snip..]
5. while (*s != '\0')
n = n * 10 + (*s - '0');
If n is of type int and also not negative,
then you could write without having to use parentheses
n = n * 10 - '0' + *s;
with no possibility of overflow.
Is there some reason you wouldn't write
n = *s-'0' + n*10; /* or add more spacing, if preferred */
?
Not any good reason.
I have an aesthetic preference
for writing the more strongly associated arithmetic operators first.
I think aesthetics *is* a good reason for writing code a certain way.
[snip elaboration]
Sure, but not the only one. In writing, for example, it is my
preference not to split an infinitive. Sometimes, though, it's
awkward to just blindly follow that rule. In the particular
case here (ie, in assigning to n), IMO there is very little
difference aesthetically between the two writings, and the
second one even might be a little easier to digest when reading
it. Partly I was curious how much of the choice was an actual
conscious process and how much was doing things a particular way
just because that's the way the author is used to doing them.
It was conscious, because I had to consider whether to add the
parentheses. In my usual work environment, they wouldn't
matter, so I would very likely leave them out. In this
newsgroup, little omissions like that can get you attacked
without mercy.
So I did think about whether to put in the
parentheses, and that caused me to carefully consider the whole
expression.
Sorry, I wasn't clear. What I was (and am) curious about is
the choice between putting the multiplication term first or
second. Were both of those among the alternatives you
considered? (And, unimportant side issue: how about 10*n
instead of n*10?)
I stand by my choice, although I do recognize that, as you
said, different people weigh the factors differently.
At least as interesting as what choice was made is what factors
were considered. Your comment about the (putative) reaction in
the work environment, for example, isn't one I would have thought
of right off the bat.
However, I honestly don't see the factors that cause you you
prefer your formulation,
I mentioned one in the posting. Did you not see that? And are
there really no other possible factors that you could think of?
or why you said (in another post in this thread) that
there's no reason to choose
while ('\0' != *s)
over
while (*s != '\0')
(some people think there is, as I'm sure you know, but I
don't),
Not exactly. I have seen reasons stated for preferring
if(0==*s) // or some other constant besides 0
over
if(*s==0)
but I don't remember that reasoning being extended to the '!='
operator. It is of course possible to make an indirect argument
for the '!=' case, based on the proposed argument for the '=='
case, but I don't remember it (either the case or the indirect
argument) being discussed. So I still don't know of any direct
reason to prefer 0 != *s to *s != 0.
but you
said that there are reasons other than aesthetics to choose
n = *s-'0' + n*10;
over
n = n * 10 + (*s - '0');
(Or did I misunderstand that?)
What I said was the writing with the '*' done second might be
easier to digest; because that formulation doesn't have (or
need) parentheses, they don't have to be scanned, and no mental
effort is required for why they might be necessary, unlike the
formulation that does have parentheses. Surely you can imagine
other plausible motivations (eg, related to workplace reactions)
for distinguishing the two, besides just aesthetic concerns --
yes?
If I may be philosophical for a moment, IME reactions on style
issues (considering this case purely a style issue, which of
course it isn't, at least not purely) tend to fall into one of
two camps, namely: one, style choices are purely a matter of
preference, just follow whatever the local customs are; and two,
there are definite reasons for preferring one style over another,
and the preferred style should be followed without exception. I
believe neither of these camps is right. In many cases there is
no reason to prefer strongly one style over another, and when
that holds consistency with local custom should dominate. In
other cases there are good and compelling reasons for preferring
one style over another, and in those cases it is important to set
or influence local custom to go in the right direction. And
finally, it's almost never true that following a single style
choice in every circumstance, and without any alternative or
exceptions, is the best course of action. When advocating a
particular style choice (for a class of situations, but one
particular choice to apply to those situations), it's important
to discuss the limits or possible exceptional circumstances when
an alternate choice might be better. But that's kind of a
digression from the topic here, so please excuse my indulgence.