condition convention 'if (10 == j)...'
Alex Dupuy
dupuy at columbia.UUCP
Mon Apr 29 01:27:32 AEST 1985
In article <2140 at sun.uucp> shannon at sun.uucp (Bill Shannon) writes:
> > > To prevent silly mistakes like
> > > if (j = 10)
> > > I usually write
> > > if (10 == j)
> > > By putting the constant first, I ensure that the compiler will catch the
> > > typo.
> >
>
> [Flames...]
>
> People that write "if (10 == j)" probably also write "while (1)".
> What do you mean, "while 1"??? Are you expecting "1" to change???
> Everyone knows this should be written "for (;;)", read "forever".
>
> Bill Shannon
I agree that "if (10 == j)" looks ridiculous, but to prevent ridiculous errors
like
"if (j = 10)" /* j is set to 10 */
or
"if (j != NULL & j->next != NULL)" /* bitwise AND evaluates j _and_
j->next even if j is nil */
and to make my code more human readable (:-) I use the following #definitions:
#define and &&
#define or ||
#define is ==
#define isnt !=
#define not !
As a result, my code looks like
"if (j is 10)"
"if (j isnt NULL and j->next isnt NULL)"
which even Pascal types (like me) can read. And there is *no* way to make
those silly mistakes shown above. I even throw in another:
#define ever ;;
so I can have *real* "for (ever)" loops.
Alexander Dupuy
Ok, go ahead and flame me for not being a strict K&R type who likes C to
look like a mess of proofreaders marks (and if you think this isn't
"heretical" enough, you should see my indentation style :-).
More information about the Comp.lang.c
mailing list