0

I am building an interface in C which defines some function types with predefined signatures. Some implementations of this interface may not use some of the passed variables.

Is it a good / reasonable practice to use a Python-style single underscore in implementations that don't use certain arguments? The purpose is readability, but I don't know if that would confuse other devs reading the code even more.

E.g.:

// Interface definition

typedef int (*do_something_fn_t)(int a, float b);

typedef struct {
    char *name;
    do_something_fn_t do_something_fn;
} MyInterface;


// Implementation

int complex_calculation (int a, float _)
{ return a + 1; }

const MyInterface super_useful_int = {
    .name = "Super useful interface",
    .do_something_fn = complex_calculation,
};
user3758232
  • 758
  • 5
  • 19
  • 1
    Personally, I'd give it a name like "unused". And then at the top of the function, to silence compiler warnings about an unused variable, I'd throw in `(void) unused;` – Christian Gibbons Apr 05 '22 at 16:29
  • 1
    I'd perhaps use `unused` instead - or enable the next C standard with `-std=c2x` and remove the identifier completely: `int complex_calculation (int a, float)` – Ted Lyngmo Apr 05 '22 at 16:29
  • 2
    I disagree with the duplicate here. This question is asking about a good naming convention for an unused variable, which the author incidentally is currently using `_`, but the underscore really isn't at the crux of the question. – Christian Gibbons Apr 05 '22 at 16:40
  • @ChristianGibbons Yes, closing it for likely attracting opinion based answers would probably be more appropriate. – Ted Lyngmo Apr 05 '22 at 16:44
  • 1
    @TedLyngmo Oops. Fixed. – user3758232 Apr 05 '22 at 16:48
  • I think it's wiser to keep the name of the unused parameter; if it's named properly it carries information that you may want to get when you study the function in isolation, even if the parameter is unused. For example, a function used for calculating the number of elements in a sequence, reduction style, may have the signature `int Increment(int accumulator, int element)` and then when you see that the second parameter is the current element it is easier to understand why it is unused. – August Karlstrom Apr 06 '22 at 15:14

0 Answers0