Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

variables beginning with "_"

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the meaning of prepending an underscore in a variable name, such as _myValue ?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no inherent meaning. Variable names are arbitrary.
[ November 08, 2004: Message edited by: Paul Sturrock ]
 
Bill Keller
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I've seen underscores used, they suggest some pattern or intended meaning by convention. For example, variables using all upper case letters indicate a constant value, such as PI. It seems that is not the case for _size.
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally, variables beginning with an underscore are system variables, often compiler-generated. Comes from C. Usually, programmers should avoid starting a variable name with the underscore in case there's a system variable with the same name.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bill Keller:
When I've seen underscores used, they suggest some pattern or intended meaning by convention.

While it is true that there is no inherent meaning in prepending underscores -- just as there is no inherent meaning to all-caps -- there certainly is personal/group convention.

I've seen it used for the following.
  • Instance variable members
  • Constructor and setter arguments
  • All method arguments (only saw this once)

  • Personally, I dislike the practice for a couple reasons. First, the underscore is a PITA to type and easy to miss when reading code, and it looks ugly as a prefix/suffix. I did say "personally."

    As well, modern IDEs can color-code member and local variables differently if you find you're getting confused. However, in that case I'd say the method is too long and/or not documented well enough.

    For the majority (?) of people, using variable prefixes to denote scope and type has lost favor as OOP has gained in popularity. Nothing screams "exposed implementation details" like a variable named "mi_size".

    In case we ever work together, please don't pick up this style.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic