tags:

views:

211

answers:

5

I come across the following in code.

_name1
_name2
smeEGiGross:

In general, what does _name1 underscore mean in Delphi 4?

+5  A: 

I think it's just a common practice to begin variable names with underscores.

Rules for variable (and component) names in Delphi:

  • Name can be any length but Delphi uses only first 255 characters.
  • First character must be letter or underscore not a number.
  • You cannot use any special characters such as a question mark (?), but you can use an underscore (_).
  • No spaces area allowed in the name of a variable.
  • Reserved words (such as begin, end, if, program) cannot be used as variables.
  • Delphi is case insensitive – it does not matter whether capital letters are used or not. Just make sure the way variables or components are used is consistent throughout the program.
rahul
Be aware, common practice is not the same as good practice
Jorge Córdoba
Yep. I have also seen double underscores in variable names.
rahul
Gamecat
I wouldn't say it's common practice to start variable names in Delphi with an underscore. c and c++ maybe
Steve
http://edn.embarcadero.com/article/10280 is an interesting read but even Borland didn't always follow their own guidelines to the letter.
codeelegance
I start with underscore for global variables.
inzKulozik
+4  A: 

To add to the answer of phoenix:

* You can use reserved words as identifiers, but you must add a & sign: &then, 
  &unit.
I only use this if another name ís not apropriate.

I associate leading underscores with C/C++ not with Delphi. The C syntax is more character oriented (like {, }, || and &&) so the underscores fit perfectly. While the Delphi/Pascal syntax is more text oriented (begin, end, or, and) so the underscores look a bit strange as you don't expect them there.

Gamecat
Thanks @gamecat for providing this info.
rahul
+5  A: 

It's a convention to help determine scope of a variable by its name, like private class members. The original author probably uses C++ as well.

In Delphi, I prefer to prefix fields with "F", method parameters with "a" (argument) and local variables with "l".

Update:

Another place you might see underscores is after certain identifiers in code generated with WSDLImp or TLBImp to avoid confusion with existing Delphi identifiers. For example, unless you specify otherwise, "Name" is renamed (no pun intended) to "Name_".

Bruce McGee
+3  A: 

It's regularly used for scope.

I declare all my private variables with a _ at the beginning, which is more common in C#/C++.

It's for readability and doesn't necessarily mean anything.

Jak
+2  A: 

I cannot say what the author of the code you have in mind was thinking, but an underscore prefix has a fairly well recognised convention in relation to COM.

Members in a COM interface that have an underscore prefix are (or were) hidden by default by interface browsers/inspectors, such as VB's library browser etc. This is used when members are published by necessity but are not intended to be used directly.

The _AddRef and _Release members of the IUnknown interface are perhaps the most obvious example of this.

I've adopted this convention myself, so that for example if I declare a unit implementation variable that is exposed through the unit interface via an accessor function I will name the variable with an underscore prefix. It's not strictly necessary in that sort of example but it acts as documentation for myself (and anyone else reading my code that is aware of the convention, which is fairly self-evident from the context).

Another example is when I have a function pointer that may refer to different functions according to runtime conditions so the actual functions that the pointer may refer to are not intended to be called directly but are instead intended to be invoked via the function pointer.

So speaking for myself, I use it as a warning/reminder.... Proceed with caution you are not expected to reference this symbol directly.

Deltics