delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/06/12/09:13:04

Date: Tue, 12 Jun 2001 16:14:34 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Martin Stromberg <Martin DOT Stromberg AT epl DOT ericsson DOT se>
cc: djgpp-workers AT delorie DOT com
Subject: Re: [PATCH] gcc-3.0 related fix for src/libc/posix/fcntl/fcntl.c
In-Reply-To: <200106121251.OAA13656@lws256.lu.erisoft.se>
Message-ID: <Pine.SUN.3.91.1010612160600.3699D-100000@is>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Tue, 12 Jun 2001, Martin Stromberg wrote:

> > fcntl.c:23: warning: declaration of `index' shadows global declaration
> > make.exe[3]: *** [fcntl.o] Error 1
> > make.exe[2]: *** [all_subs] Error 2
> > make.exe[1]: *** [all] Error 2
> > make.exe: *** [subs] Error 2
> > 
> > Renaming index to ind fixes the problem. Ok to commit?
> 
> I'm not too happy with such changes. The variable is an index so it
> should be allowed to be named "index". 

I disagree.  Safe coding means that you try to stay away of potential 
trouble even if meticulous analysis can prove that the trouble shouldn't 
happen in that particular case.  In other words, safe coding is about 
restraint, not anarchy for the sake of it.

For the same reason, you would generally stop when the traffic light is 
red even if there's no car anywhere in sight (and no cops ;-).

If you want a less far-fetched example, consider code that uses a local 
variable called `fprintf'.  ANSI C forbids that, but the only reason it 
doesn't forbid `index' is that the latter is a non-ANSI function.  
However, the danger and the potential trouble are the same.

> Sure it shadows another global named index. But that isn't a problem
> if we don't need to use that global.

The warning tells you that you could easily get a bug by rearranging code 
in small ways.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019