Clay Bridges
Clay Bridges

Reputation: 11860

Objective-C: technical reasons to avoid _ as a local variable name?

Consider this in the (possibly nested) scope of a method (function, block, etc.):

int _ = 42;

Are there any technical reasons to avoid a local variable named _?

Some guidance, for the purpose of this question:

† Buy me a pint, and you can tell me all about it. :)

Upvotes: 0

Views: 81

Answers (2)

zwol
zwol

Reputation: 140445

C99 §7.1.3 says that all identifiers beginning with at least one underscore are reserved for use by the implementation, as file-scope identifiers only.1 _ is an identifier that begins with at least one underscore, so you're not supposed to define it in any way at file scope.2

However, as a local variable name, _ is fair game for the application programmer. Only identifiers beginning with either two underscores, or an underscore and then an uppercase letter, are reserved unconditionally.

These rules are honored more in the breach than the observance, as the footnotes demonstrate.

1 Yes, that means the very common practice of starting "internal use only" function names with _ followed by a lowercase letter is technically a conformance violation.

2 GNU gettext is a prominent third-party library that breaks this rule; the programmer is encouraged to #define _(x) gettext(x) as shorthand.

Upvotes: 4

user3581248
user3581248

Reputation: 1014

To answer your question simply - no, there aren't any technical reasons to avoid it. Lots of other reasons though.

Upvotes: 0

Related Questions