Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using underscore in class names

Is it safe to use _ in class names?

What's the best replacement for . in naming? (Currently I use _.)

like image 326
Xaqron Avatar asked Jan 06 '11 23:01

Xaqron


People also ask

What is the use of underscore in a variable name?

Usually an underscore is used in member variables so as to distinguish between member variables, static member variables & local variables. This way the scope of the variable is visible in the variable name itself.

When to use underscore before or after a member?

Usually underscore before a member is used when the member is private. It is usefull in language that does not have a builtin way to declare members private, like python. Show activity on this post.

What are the naming conventions for underscore characters?

Naming Conventions: Underscores. The underscore character ('_') can be used in the following situations. When naming formal parameters, it should be used as the first character. In the DEL_ prefix.

How do you handle underscore names in the API?

A name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice. Let’s instantiate the above class and try to access the name and _age attributes.


2 Answers

It's safe to do so for one underscore at a time, although you shouldn't use two consecutive underscores. From the C# language spec, section 2.4.2:

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.

Personally I try to avoid type names with underscores anyway though... I'm not sure what you mean by "replacement for ." though. If you could give some context, that would help.

like image 117
Jon Skeet Avatar answered Oct 13 '22 11:10

Jon Skeet


As others have mentioned, it is safe. However, Microsoft's Class Naming Guidelines read:

Do not use the underscore character (_).

As Jon Skeet mentioned, a good alternative would be to replace . with Dot. For example, a class related to drive.google.com could be named DriveDotGoogle. (I think the DotCom portion could be omitted so as not to unnecessarily lengthen the name.)

like image 32
Daniel Avatar answered Oct 13 '22 12:10

Daniel