Monday, May 14, 2012

How to Remove Unused Parameter Warning - part 2

I still believe in no-warnings coding policy. Old problem, new solution.

Code warnings are often signs of real problems and have to be checked each and all. Many times you can rewrite the code in a better way, but sometimes that's not possible. For example generic protocol API offers method parameters, which are not needed in certain cases. You get the "Unused Parameter" warning.

What to do?

You do NOT want to suppress all warning, because you would miss potential real problems. You do NOT want write dummy code to use parameters, because sometimes somewhere that might cause unwanted side-effects. You do NOT want to leave that warning visible forever, because... well, because that simply is against the principle of no-warnings policy. Either you follow it or you don't.

The correct solution is to silence those "false" warnings one by one. Earlier I offered a solution using #pragma. Fine solution at that time, but now my currect project has so many pragmas that it's interfering with code readability. Need a new solution:

- (IBAction)buttonClicked:(id) __unused sender
{
   [UIView animateWithDuration:0.5 animations:^(void)
     {
         self.item.alpha = 0;
     } completion:^(BOOL __unused finished)
     {
         self.item.hidden = YES;
     }];
}

This marks the unused method parameters one by one to be approved by the developer. Compiler is your helper, not master. You stay in control!

No comments:

Post a Comment