It is unpleasant. But the variables that have these __
are usually private and static, so their scope is limited
to the class. And their use is often limited to a few
places in the class.
Their base name may also be something that
you may want to use anyway as a local variable name,
so adding those __ keeps that name from conflicting
with the use of the same name later.
Not trying to justify it, but these are reasons people
have stated.

David Jordan
Object Identity, Inc.


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Andrew Oliver
Sent: Thursday, May 05, 2005 11:10 AM
To: Research Triangle Java User's Group mailing list.
Subject: Re: [Juglist] Variables starting with underscore

but don't you hate it when people use __s?  It makes things rather 
unpleasant to type.

A. Kevin Baynes wrote:
> Thank you all.
> 
> I've seen this convention in other languages without realizing it. 
> 
> For example, in PHP (a C-decendent) web scripts, the PHP engine parses
> a Request into a number of objects which are available at the script
> level or 'class' level : $_REQUEST, $_GET, $_POST. Since all PHP
> variables start with a $, it would translate to _REQUEST, _GET, _POST.
> PHP defines some 'magic constants' which begin (and end) with two
> underscores : __FILE__, __CLASS__, etc. These last seem to be static
> class variables, which is consistent with David's response.
> 
> In Python, private class variables begin with two underscores. And
> there are certain class members which are of a 'static' nature that
> begin with two underscores : __init__() and __doc__.
> 
> In a similar vein, in Ruby it seems this principle is followed, but
> the @ sign is used in place of the underscore. Class variables begin
> with an @ and static class variables begin with @@. The only double
> underscores I found in Ruby were class.__id__ and __send__.
> 
> Sorry for the dissertation, but this seems like an interesting coding
> convention that has been adopted by a number of languages, and exists
> in Java only as an artifact... a coding meme.
> 
> ~akb
> 
> 
> 
> On 5/5/05, David Jordan <[EMAIL PROTECTED]> wrote:
> 
>>Actually, the use of _ at the beginning of names goes
>>back further than Windows C++ use. C compilers in the
>>early 1980s would put underscores at the beginning of
>>names for variables that they would generate, variables
>>represented at the assembly language level.
>>It was done in an attempt to have names that would not
>>conflict with names provided by the program, I believe.
>>
>>When Bjarne developed cfront, the program that translated
>>C++ to C, he used __ at the beginning of some of the variable
>>names generated at the C level. He was in the same group
>>as Kernighan and Ritchie and probably picked up the
>>technique from them. (just thinking back about those days
>>working with Bjarne brings back fond memories...)
>>
>>In the late 1980s, some organizations in Bell Labs established
>>C++ coding guidelines, some of which I participated in.
>>They would specify that __ would indicate just to
>>the programmer that the variable was a class static variable.
>>
>>It was also popular then to overload method names and instead
>>of having getMember() and setMember(), they would just have
>>two methods named member(), one as a void that took an argument
>>to set the attribute and another that returned the value of
>>the member. The field itself in the class had a single _,
>>so the naming was consistent and the programmer new that a
>>variable with the _ was a private field of the class.
>>Some of these conventions were used because the developers
>>had been examining the cfront-generated C code and decided
>>to emulate the techniques of Bjarne. But Bjarne had used
>>the techniques his C friends had used before "C with classes"
>>entered Bjarne's plans.
>>
>>David Jordan
>>Object Identity, Inc.
>>
>>
>>-----Original Message-----
>>From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
>>Behalf Of Michael Czeiszperger
>>Sent: Wednesday, May 04, 2005 7:49 PM
>>To: A. Kevin Baynes; Research Triangle Java User's Group mailing list.
>>Subject: Re: [Juglist] Variables starting with underscore
>>
>>On May 4, 2005, at 5:38 PM, A. Kevin Baynes wrote:
>>
>>
>>>I've noticed in some Java code I've read, variables starting with a
>>>single underscore or two underscores.
>>>
>>>private static WeakHashMap __servers = new WeakHashMap();
>>>
>>>I'm guessing that this is a coding convention from a preceding
>>>language(s) (C/C++, perhaps?), and that perhaps it is used to
>>>distiguish variable scope (globals, perhaps?)
>>>
>>>Can someone school me on the rules?
>>
>>In my experience the underscore thing is a Windows C++ convention,
>>and is used to distinguish class member variables.  It can be useful
>>as a convention if your IDE can't automatically tell the scope of a
>>variable.  Never seen a double underscore though.
>>
>>___________________________________________________________________
>>F is for Fire that burns down the whole town!
>>U is for Uranium ...  BOMBS!!
>>N is for No Survivors!!
>>
>>_______________________________________________
>>Juglist mailing list
>>[email protected]
>>http://trijug.org/mailman/listinfo/juglist_trijug.org
>>
>>_______________________________________________
>>Juglist mailing list
>>[email protected]
>>http://trijug.org/mailman/listinfo/juglist_trijug.org
>>
> 
> 
> 


_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org


_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to