0

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:

  • I know _ generally prefixes Objective-C instance variables. Leave that aside. Commentary on other clashes with convention welcomed.
  • I like pretty code too, but statements of taste or pure opinion (e.g. "It's {confusing, unreadable, unmaintainable}") are strongly discouraged here†.
  • I'm primarily interested in answering this for Objective-C, but answers related to C or C++ are also encouraged.

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

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
  • The only downside I see, is it's not a descriptive name for a variable, expect for that, it's a valid C variable name: http://en.wikibooks.org/wiki/C_Programming/Variables#Naming_Variables – Sash Zats May 23 '14 at 10:59
  • In fact it is a very good practice if you want to be a one-liner and obfuscated code (both at the same time). – Jean-Baptiste Yunès May 23 '14 at 11:22
  • I'm looking for technical facts. While what you guys say may be good practice, a lot of programming is craft, and I would still consider them "statements of taste or pure opinion". – Clay Bridges May 23 '14 at 15:16
  • Question for whoever close-voted this as "primarily opinion-based": did you read the parts about "Are there any _technical_ reasons" and "statements of opinion discouraged"? – Clay Bridges May 23 '14 at 15:26

2 Answers2

4

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.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • Thanks, this is exactly the kind of information I was looking for. I'll very likely be green checking you in a day or two. – Clay Bridges May 23 '14 at 15:28
0

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

user3581248
  • 964
  • 10
  • 22