4 ms·
This piqued my intrest. I’m a hobbyist C programmer so forgive me if this is a rudimentary question. What is the rationale/convention for naming a variable “_”?
by jiive 6y ago
This piqued my intrest. I’m a hobbyist C programmer so forgive me if this is a rudimentary question. What is the rationale/convention for naming a variable “_”? I’ve never seen this before.
For example:
struct lg_buf *lg_buf_init(struct lg_buf *_) {
_->data = NULL;
_->len = _->cap = 0;
return _;
}
- makapuf 6y agoI've seen variables named _ when you dont use it (and it's the way it's not an error not to use it after declaration in rust or go by example), but in that context I find it weird.
- nfoz 6y agoI think this is borrowing a convention from Perl, where the variable $_ is often given an implicit value of "the thing I'm talking about" when you didn't bother to give a real variable name for it. http://www.tutorialspoint.com/perl/perl_special_variables.htm http://www.tutorialspoint.com/perl/perl_special_variables.ht... (The $ sigil denotes that the variable is a scalar; all Perl variables have a sigil like that.) So this reads naturally to me: _ is the lg_buf, it doesn't need a real name like "buf", because it's just "the thing" or "it".
- rurban 6y agoAnd Perl took this convention from Prolog. In Perl it's the default context, in Prolog the default unknown symbol.
- mostlylurks 6y agoAn underscore is primarily used as an identifier to denote a variable/parameter whose name does not really matter. It's more commonly used when you're not planning on using the aforementioned variable, but it's also sometimes used when the identifier is used but its name doesn't matter. One such instance is the abbreviated scala lambda syntax, where (x:Int) => x + 2 can be abbreviated to _ + 2 in the same way that kotlin would allow abbreviating it as { it + 2 } with its equivalent default lambda parameter name, "it". In the example you quoted, the identifier denotes the sole parameter, so in a sense its name does not matter, and as such people from certain programming circles might be inclined to use an underscore instead of taking the time to come up with an appropriate name. It's not like a more descriptive name would help in that example, the type name and function name already give sufficient context for it to be perfectly clear what the parameter is for, and it's not like parameter names have any semantic significance in C.
- jiive 6y ago> It's not like a more descriptive name would help in that example, the type name and function name already give sufficient context for it to be perfectly clear what the parameter is for, and it's not like parameter names have any semantic significance in C. I agree, and I’ve been thinking about this today. There is also a minimalistic quality to this style, the _ pointer is more prominent simply by not having a real name. I like it!
- Randor 6y agoHi, The ISO C standard in section 7.1.3 states that global functions and variables in compiler/system libraries should be prefixed with an underscore to avoid the risk of conflict with names in user programs. I checked and indeed... lg_buf is a global variable. But I've never seen anyone use just the underscore prefix without a name before.
- chrisseaton 6y ago> I checked and indeed... lg_buf is a global variable. struct lg_buf is a type, not any kind of variable. And here _ is a local variable, not a global variable so can't conflict with anything else anyway. So what you've said doesn't apply for more than one reason.
- Randor 6y agoThanks, In that case I don't really know why the guy named his variable with an underscore.
- codr7 6y agoBecause it's short, easy to recognize and somewhat accepted to signify that the name doesn't matter and even 'it' in Perl. I find that using it for the default parameter (or self) simplifies the code substantially on a visual level.