• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: -Wall
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: -Wall


  • Subject: Re: -Wall
  • From: Alastair Houghton <email@hidden>
  • Date: Thu, 5 Jun 2008 11:56:11 +0100

On 5 Jun 2008, at 01:15, Stuart Malin wrote:

I'm curious of the efficacy of doing so. For instance, even with - Wall, some of my lazy coding styles, e.g.:

	if (anInstance = [enumerator nextObject]) doSomethingWithAnInstance;

generated warnings and I was led to code such as:

if ((anInstance = [enumerator nextObject]) != nil) doSomethingWithAnInstance;

I guess this makes explicit my intent, and so can be argued this is better coding practice...

FWIW, it's sufficient to write

  if ((anInstance = [enumerator nextObject)) doSomethingWithAnInstance;

The extra "!= nil" isn't required, though some people think it's clearer (it isn't *my* cup of tea, mind, not least because I think attempts to make C "easier to read" just encourage people not to learn C properly.)

It's quite possible to write warning-free code even with relatively high warning levels (I tend to go for "-W -Wall" and I have a zero- tolerance approach to warnings), which brings me to:

On 5 Jun 2008, at 01:33, Jean-Daniel Dupas wrote:

Some warning like unused param should be ignored (and so disabled).

You're right in general that some warnings are a little pointless, but the unused parameter warning *is* actually a useful one. It has occasionally saved me considerable headaches where either (a) I'd forgotten to implement part of a function I was writing, or (b) I'd mis-spelt one of the parameter names, and it just so happened that there was already a variable with the other name I'd accidentally used.


On 5 Jun 2008, at 02:53, Alex Curylo wrote:

Option A) which I use since it works with compilers from that other, lesser, platform most people write code for:

- (void)emptyMethod:(NSNotification*)notification
{
  (void)notification;
}

Or the slightly better version that works everywhere *and* makes it clear that something is unused:


  #define UNUSED(x)	(void)(x)

  ...

  - (void)emptyMethod:(NSNotification *)aNotification
  {
    UNUSED (aNotification);
  }

Option B) when you can assume the requirement of compilers which do not suck:

- (void)emptyMethod:(NSNotification*)notification
{
#pragma unused (notification)
}

The advantage (which nobody seems to have mentioned) of the pragma or the GNU __attribute__((__unused__)) being that the compiler should then tell you if you've declared something unused and it, in reality, *is* being used somewhere.


It's a little unfortunate that it isn't possible to #define something to a pragma prior to C99, but as of C99 you can even do something like (written in Mail and not tested):

  #if __STDC_VERSION__ >= 199901L && __APPLE__
  #define UNUSED_PRAGMA(x) _Pragma(#x)
  #define UNUSED(x)        UNUSED_PRAGMA(unused (x))
  #else
  #define UNUSED(x)        (void)(x)
  #endif

which will use the pragma where it's supported and fall back to the old-fashioned cast-to-void where it isn't.

Kind regards,

Alastair.

--
http://alastairs-place.net


_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: -Wall
      • From: Alex Curylo <email@hidden>
References: 
 >Re: -Wall (From: Alex Curylo <email@hidden>)

  • Prev by Date: Option click at function name, does not work well (Xcode 2.5)
  • Next by Date: Re: -Wall
  • Previous by thread: Re: -Wall
  • Next by thread: Re: -Wall
  • Index(es):
    • Date
    • Thread