A
A
Andrey Gubsky2011-04-28 13:29:41
C++ / C#
Andrey Gubsky, 2011-04-28 13:29:41

Do you use underscores to name global variables?

Today I had a discussion with a colleague.
In all the projects that I was involved in, I always set a rule - global variables begin with the _ symbol.
I.e

class A<br/>
{<br/>
 ...<br/>
 int _variable;<br/>
 ...<br/>
}

With such a name, inside a large piece of code it is always immediately clear that a global variable is used, and not a local one. It helps to understand the code faster.
What rules do you follow?
UPD. Basically it's about c#, so the variable is a member of the class. In C#, as you know, you cannot create variables outside of classes. But I'm interested in other languages ​​too :)

Answer the question

In order to leave comments, you need to log in

16 answer(s)
X
XaBoK, 2011-04-28
@XaBoK

Traditionally, the name of a private variable begins with an underscore, constants are typed in upper case (in all capital letters), and everything else is in small letters.

G
gro, 2011-04-28
@gro

The absence of global variables allows you to understand the code even better.

V
VasKravchuk, 2011-04-28
@VasKravchuk

when I use C#, if private/protected, then I start with a small letter, if the element is public, I start with a capital letter, without underlining.
I try to do as it is written in msdn guidline .
Each language has its own standards + as you like :)

R
retran, 2011-04-28
@retran

Where did you find global variables in C#?
What you have is class fields, and yes, it is customary to write them with underlining.

B
bagyr, 2011-04-28
@bagyr

No. For cases when you yourself are not sure what you are writing / your imagination has ended / some kind of conflict, there is this.
I don't like Python-style even in python itself.

A
Andrey Gubsky, 2011-04-28
@Ernado

I am using C#

M
MikhailEdoshin, 2011-04-28
@MikhailEdoshin

I have global variables called long, distinct names, and local variables are short, often single-letter.

I
int02h, 2011-04-28
@int02h

I understand from your example that you call the private fields of the class global. If so, then I name them as well.

R
rroyter, 2011-04-30
@rroyter

Interestingly, as recently as this week, this issue was discussed at a rally. I was for underscores. My arguments:

  • Grouping fields in InteliSense
  • Inability to confuse a property with its field on input (this.Prop and this.prop where prop is a field and Prop is a property)
  • Visual difference when reading (this. is reserved for methods and properties).
  • Saving 4 characters when typing (this.)

M
mark_ablov, 2011-04-28
@mark_ablov

globals (which is very rare) I name g_{name},
class methods in C++ - m_{name} or just _{name}, in php - class methods $_{name}

C
CrazySquirrel, 2011-04-28
@CrazySquirrel

php - private/protected starts with "_" (standard), applies to both properties and methods.

M
Meroving, 2011-04-29
@Meroving

the place where I do not agree with the MS convention. The this construct. I'm not happy at all. Maybe a habit. So I support underscore.

V
VasKravchuk, 2011-04-28
@VasKravchuk

Depends on the programming language.
What are you using?

C
clamaw, 2011-04-29
@clamaw

I used to use Java, now I use this. primarily.

D
dotbg, 2011-04-29
@dotbg

no. If necessary, I write this. True, I only need this in constructors

A
Atrax, 2011-04-30
@Atrax

I have a bunch of other functionality on the underscore :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question