Re: CriticalAlert
Re: CriticalAlert
- Subject: Re: CriticalAlert
- From: Steve Bird <email@hidden>
- Date: Thu, 20 Dec 2001 15:52:23 -0500
On Thursday, December 20, 2001, at 02:59 , Esteban wrote:
A coding style technique I've learned recently (gosh, i can't believe it
took me so long and I've been coding for 3 years in C/C++) is to
include the fixed value you are checking against first in the
conditional.
In this way if you mistyped and used only one = instead of the usual ==
you will get a compiler error, because the fixed value can not be
reassigned.
For example,
you can do
if( NSAlertAlternateReturn = alertResult)
at this point because you forgot to use == instead of =, the compiler
will b*tch at you, and tell you how brilliant you are ;)
then you'll just fix that right then to the correct way
if( NSAlertAlternateReturn == alertResult)
From experience, a lot of times the error of using the single = instead
of == is pretty common, so I've forced myself to do things the other way
(check the constant value against the variable value in a condition,
instead of the variable value against the constant) thus avoiding
problems, if I accidently type = and not ==
And of course if you make the error of just doing
if(alertResult = NSAlertAlternateReturn)
You'll be pulling your hair out for a while because everything compiles
fine, and your code "looks" right, and it DOESN'T WORK!!!! $%#@! (its
happened to me quite a lot)
Hehe ;)
Anyways we all go through that error of using a = instead of a == so
don't beat up yourself, just be careful and I suggest change your
technique, so at least the brainless compiler can help you fix your code
before runtime ;)
-Esteban
--- Coming into C from years of Pascal (where ':=' means assignment), I
was frustrated by the language's different use of a common (in every other
language) comparison syntax ( If errCode = 0 ), and the compiler's
acceptance of it (at least CodeWarrior could be set to warn you).
I read the suggestions of others (one company I knew even has it as a
rule) to use the constant first, i.e., If (0 == errCode) .
I hated the way it reads.
I still hate the way it reads.
It's not the way I think.
Chances are it's not the way you think, either.
If you buy something for 69 cents, and the clerk gives you 31 cents
change, do you think "is 31 cents the change I got?". I don't. I think
"is the change I got 31 cents?"
I came to the conclusion that anyone (me included) who couldn't remember
to use double equals, wouldn't remember to put the constant first, either.
It was a struggle to remember to put the constant first. It was a
lesser struggle to check the double-equals than to re-arrange the
arguments to the comparison operator.
And now it's second nature.
-------------------------------------------------------------------------------------
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
www.Culverson.com (toll free) 1-877-676-8175