Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to let gcc warn me when I use = in conditions

Reply
Thread Tools

how to let gcc warn me when I use = in conditions

 
 
Kenny McCormack
Guest
Posts: n/a
 
      06-26-2005
In article <(E-Mail Removed). com>,
Greg <(E-Mail Removed)> wrote:
....
>> However this is an incomplete solution (you don't always have a non-lvalue
>> available) that results in less readable and less consistent code. Many
>> compilers will diagnose this given suitable options and there are other
>> "lint" tools available which will do so. This is much more relaible than
>> source tricks like that above and is a much better approach.
>>
>> Lawrence

>
>Absolutely, switching the left and right sides of an equality test as
>it is conventionally written makes the expression less readable [until
>the pattern becomes familiar]. The more readable the code, the more
>likely its errors will be overlooked. "Readable" text describes text
>that fits a familiar pattern, and which the brain can read faster
>because it can skip many of the words by filling in their most likely
>form and meaning. In this case an assignment expression can "read" like
>an equality test, so the mistake can go undetected.


Very interesting. (Rest of very eloquent defense of your very clearly
non-mainstream position - deleted only to save space)

Mainstream thought today is that source code should be *readable* by humans
and *understandable* by computers. This is so that you can spend less on
humans.

My $.02: People who maintain the converse of your (Greg's) position should
not be programming in C. (*)

(*) Read this carefully - this is not a typo.

P.S. If you really care about this "problem" (which hasn't been a problem
for me since about the 3rd week of my time as a C programmer), just do:

#define EQUALS ==

and use that for equality tests.

P.P.S. This discussion is antique anyway, since every (*) C compiler since
about 1989 has issued a warning about this (yes, sometimes you do have to
find the right compiler option for it)

(*) Don't bother writing in with obscure exceptions to this generalization.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      06-26-2005
In article <(E-Mail Removed). com>,
Greg <(E-Mail Removed)> wrote:
>There are benefits to a less readable syntax even in the case when the
>left hand expression is not a constant. The brain will have to scan the
>expression much more closely (because it does not fit the expected
>pattern), in order to parse it. And the closer scrutiny is more likely
>to catch an assignment error.


>The motivation to write readable code is to lighten the workload on its
>reader; but it does so by essentially inviting the reader to gloss over
>its mistakes. The computer executing the code will overlook no error;
>so the reader should approach the source code as the computer does and
>have to examine each line to understand its meaning.


Say, in your opinion, which font is best for your APL programs?
--
'The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".' -- Tony Mantler
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      06-27-2005
CBFalconer <(E-Mail Removed)> wrote:

> Richard Bos wrote:
> > CBFalconer <(E-Mail Removed)> wrote:
> >
> >> if (EOF == ch) ....

> >
> > Also, this is semantically the wrong message. You don't want to
> > inspect EOF, and see if its current value is that of ch; you want
> > to know whether the current character is End-Of-File.

>
> I think the word ordering is only unusual to native English
> speakers,


*looks at headers*

Erm, I don't think so...

> and we can get used to it.
>
> EOF, if that is the value of ch, then ....
> else BLAH, if that is the value of ch, then ....


In Japanese or Forth, maybe I could. Not in C, though.

Richard
 
Reply With Quote
 
Rob Thorpe
Guest
Posts: n/a
 
      06-27-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> Sometimes, I write = instead of == in if conditions by mistakes. Is
> there any way the gcc compiler can give me a warning?


It's very useful to have an editor that warns about this.

For example, if you use GNU Emacs and enable cwarn-mode it will
highlight single "=" signs in if-statements in bright red.

This is very useful, but can be irritating if you use them
deliberately.

 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      06-27-2005
(E-Mail Removed) wrote:
> Sometimes, I write = instead of == in if conditions by mistakes. Is
> there any way the gcc compiler can give me a warning?


Use a lint tool such as Splint (http://www.splint.org/).
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      06-27-2005
In article <(E-Mail Removed) .com>,
Rob Thorpe <(E-Mail Removed)> wrote:
>(E-Mail Removed) wrote:
>> Hi,
>>
>> Sometimes, I write = instead of == in if conditions by mistakes. Is
>> there any way the gcc compiler can give me a warning?

>
>It's very useful to have an editor that warns about this.
>
>For example, if you use GNU Emacs and enable cwarn-mode it will
>highlight single "=" signs in if-statements in bright red.
>
>This is very useful, but can be irritating if you use them
>deliberately.


I must say I really don't understand this thread at all.
I mean, I misspell printf more often than I use = for == or vice versa.

And my compiler prompts warns me of either error quite nicely thank you.

 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      06-27-2005
On Fri, 24 Jun 2005 02:27:12 GMT, CBFalconer <(E-Mail Removed)>
wrote:

>Alan Balmer wrote:
>> CBFalconer <(E-Mail Removed)> wrote:
>>> "(E-Mail Removed)" wrote:
>>>>
>>>> Sometimes, I write = instead of == in if conditions by mistakes.
>>>> Is there any way the gcc compiler can give me a warning?
>>>
>>> Simple. Get in the habit of putting the constant first. I.e:
>>>
>>> int ch;
>>>
>>> ....
>>> if (EOF == ch) ....
>>>
>>> will complain loudly if you write = in place of ==. Similarly:
>>>
>>> while (EOF != (ch = getc(fp))) {
>>> /* do unspeakable things to and with ch */
>>> }

>>
>> Now, all I have to do is somehow guarantee that all if statements
>> have a constant.

>
>Well, those that don't can usually legitimately use the '=' in
>place of '=='.


Weak justification of your practices is not sufficient reason to make
code less obvious. Unless you're practicing for the IOCCC.

>If we carry this reasoning to the extreme the
>compiler should execute:
>
> fprintf(stderr, "WARNING \"%s\" may be invalid\n",
> current_token);
>
>after each lexical extraction from the source! I envision
>marketing difficulties.


Nonsense. Issuing a warning for all cases, as many (most?) compilers
can be asked to do, is no more likely to lead to this than your notion
of issuing the warning for only some cases.

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      06-27-2005
On 26 Jun 2005 06:52:00 -0700, "Greg" <(E-Mail Removed)> wrote:

>
>
>Lawrence Kirby wrote:
>> On Thu, 23 Jun 2005 18:12:55 -0700, Greg wrote:
>>
>> >
>> >
>> > Alfred Z. Newmane wrote:
>> >> Stan R. wrote:
>> >> > (E-Mail Removed) wrote:
>> >> >> Hi,
>> >> >>
>> >> >> Sometimes, I write = instead of == in if conditions by mistakes. Is
>> >> >> there any way the gcc compiler can give me a warning?
>> >> >
>> >> > What version are you using?
>> >>
>> >> From what I've seen, both 2.9x and 3.x behave the same way in this
>> >> respect.
>> >
>> > You can also get in the habit of placing the rvalue (or constant value)
>> > on the left of a comparison expression:
>> >
>> > if (0 == myVar)
>> > {
>> > ...
>> > }
>> >
>> > Now the code won't compile if a = replaces the ==.

>>
>> However this is an incomplete solution (you don't always have a non-lvalue
>> available) that results in less readable and less consistent code. Many
>> compilers will diagnose this given suitable options and there are other
>> "lint" tools available which will do so. This is much more relaible than
>> source tricks like that above and is a much better approach.
>>
>> Lawrence

>
>Absolutely, switching the left and right sides of an equality test as
>it is conventionally written makes the expression less readable [until
>the pattern becomes familiar]. The more readable the code, the more
>likely its errors will be overlooked.


So, once the pattern becomes familiar, it's just as likely the error
will be overlooked? Why bother?

>"Readable" text describes text
>that fits a familiar pattern, and which the brain can read faster
>because it can skip many of the words by filling in their most likely
>form and meaning. In this case an assignment expression can "read" like
>an equality test, so the mistake can go undetected.


Nope. As you say, the brain recognizes patterns. "A = B" is a
different pattern from "A == B", and sticks out like the proverbial
sore thumb.
>
>Note that switching the right and left operands makes the expression no
>less understandable. Source code should be written to be
>understandable, posts to USENET should be written to be readable. The
>computer does not execute a source program as if it were reading a
>novel. So a person reading the source of a program, should not read it
>in the same way as they would a novel.


If this actually means anything, I think you should present it to
Donald Knuth
>
>There are benefits to a less readable syntax even in the case when the
>left hand expression is not a constant. The brain will have to scan the
>expression much more closely (because it does not fit the expected
>pattern), in order to parse it. And the closer scrutiny is more likely
>to catch an assignment error.
>
>The motivation to write readable code is to lighten the workload on its
>reader; but it does so by essentially inviting the reader to gloss over
>its mistakes. The computer executing the code will overlook no error;
>so the reader should approach the source code as the computer does and
>have to examine each line to understand its meaning.


Or, perhaps, allow the computer to do the job?

> Because whether a
>set of source code is readable or not, matters far less than whether it
>is understandably correct.
>
>Greg


--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      06-27-2005
On 27 Jun 2005 02:55:20 -0700, "Rob Thorpe"
<(E-Mail Removed)> wrote:

>(E-Mail Removed) wrote:
>> Hi,
>>
>> Sometimes, I write = instead of == in if conditions by mistakes. Is
>> there any way the gcc compiler can give me a warning?

>
>It's very useful to have an editor that warns about this.
>
>For example, if you use GNU Emacs and enable cwarn-mode it will
>highlight single "=" signs in if-statements in bright red.
>
>This is very useful, but can be irritating if you use them
>deliberately.


Another reason not to use them deliberately.

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      06-27-2005
In article <(E-Mail Removed)>,
Alan Balmer <(E-Mail Removed)> wrote:
....
>>If we carry this reasoning to the extreme the
>>compiler should execute:
>>
>> fprintf(stderr, "WARNING \"%s\" may be invalid\n",
>> current_token);
>>
>>after each lexical extraction from the source! I envision
>>marketing difficulties.

>
>Nonsense. Issuing a warning for all cases, as many (most?) compilers
>can be asked to do, is no more likely to lead to this than your notion
>of issuing the warning for only some cases.


Whoosh!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
do any compilers notice/warn on always false expressions used as conditions David Resnick C Programming 8 11-18-2010 11:43 PM
Why GCC does warn me when I using gets() function for accessing file Cuthbert C Programming 89 09-11-2006 02:59 PM
Re: how to let gcc warn me when I use = in conditions Alfred Z. Newmane C++ 11 06-27-2005 08:44 PM
Let or not let the text float Luigi Donatello Asero HTML 6 01-15-2004 04:08 PM



Advertisments