Angersock

We should've stopped--but let's keep going and see what happens.

Unreferenced Function Parameters and You

What is an URFP?

Oftentimes it is useful to declare a function to match a particular signature but without making use of the parameters it consumes. Inevitably, this causes a bunch of griping from static analysis tools and compilers.

A short example, urfp.c:

1
2
3
4
5
6
7
8
void foo( int bar ) {
    return;
}

int main (int argc, char** argv) {
    foo(42);
    return 0;
}

And when we feed it to gcc, we get:

1
2
3
4
5
6
7
8
9
10
11
12
13
angersock@devbox:~$ gcc urfp.c  -Wunused-parameter
urfp.c: In function foo:
urfp.c:1:15: warning: unused parameter bar [-Wunused-parameter]
 void foo( int bar ) {
                    ^
urfp.c: In function main:
fp.c:5:15: warning: unused parameter argc [-Wunused-parameter]
 int main( int argc, char** argv) {
                ^
urfp.c:5:28: warning: unused parameter argv [-Wunused-parameter]
 int main( int argc, char** argv) {
                            ^

Why does this happen—other than the compiler flag, I mean?

Most frequently in some kind of OOP language we find ourselves overriding a method on a class. That override may not use all of the arguments supplied by the caller: for example, a stub update( float dt) method in a game entity’s logic.

Fixing URFP In C/C++

So, to fix this, we define a simple macro:

1
#define URFP(x) ((void)x)

Then, we just use it at the top of a function to shut up the compiler.

1
2
3
4
5
6
7
8
9
10
void foo( int bar ) {
    URFP(bar);
    return;
}

int main( int argc, char** argv) {
    URFP(argc);
    URFP(argv);
    return 0;
}

Note that in C++ this isn’t always safe, because of type overloading. One can imagine a situation wherein some clever person has overridden the void typecast operator on their class, and so when you URFP it away it still does something unseemly instead of disappearing. This is a failure of the C++ language.

In Javascript

JSHint and JSLint will both complain to you if you don’t disable the behavior.

In JSHint, the appropriate line to use can be found in the docs:

1
2
3
4
5
6
{
"unused" : "strict"  // Unused variables:
                     //   true     : all variables, last function parameter
                     //   "vars"   : all variables only
                     //   "strict" : all variables, all function parameters
}

I tend to always leave this at strict, because I prefer to get any help from the linter that I can.

The problem is, of course, that this will make the linter scream bloody murder whenever you run it over your source code. So, a moral equivalent to the URFP above is:

1
function URFP( x ) { /* jshint expr:true */ x; }

This will let you consume the variable in the function and do nothing, and then the jshint expr directive will keep the linter from complaining about the lack of invocation or assignment.