Difference between revisions of "Identifier"

From Free Pascal wiki
Jump to navigationJump to search
m
(→‎Declaration: insert link to declaration)
 
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
An '''identifer''' is a symbol used in a [[Pascal]] [[Program|program]] which consists of letters, numbers and certain special characters, used to indicate a specific [[Variable|variable], [[Const|constant]], [[Object|object]], [[Record|record]], [[Type|type]], [[Procedure|procedure]] or [[Function|function]].
+
{{Identifier}}
  
For example, in the following piece of code:
+
An '''identifier''' is a symbol used to unambiguously denote an object.
 +
Such objects could be for instance
 +
* modules such as a [[Program|<syntaxhighlight lang="pascal" inline>program</syntaxhighlight>]] or [[Unit|<syntaxhighlight lang="pascal" inline>unit</syntaxhighlight>]],
 +
* [[Const|constants]] or [[Variable|variables]],
 +
* [[Routine|routines]] and [[Label|labels]], or
 +
* [[Type|types]].
  
Program WittsEnd;
 
Const Y=2;
 
Var Plugh: Integer;<BR>
 
    Procedure Xyzzy;
 
    begin
 
    end;<BR>
 
Begin
 
    Plugh := Y*2;
 
end.
 
  
we can say that
+
== Declaration ==
  
*WittsEnd
+
In [[Pascal]] an identifier has to be [[Declaration|declared]] prior its first use.
*Y
+
Depending on the object’s nature the identifier declaration is crafted in one manner or another, but the general scheme <math>(\text{identifier}, \text{object})</math> stays the same (2-tuple [that means, it is ordered]).
*Plugh
 
*Xyzzy
 
  
Are all identifiers.
+
An identifier has to consist of at least one [[ASCII]] letter or underscore.
 +
After the first character ASCII digits may follow, too.
 +
In the case of <syntaxhighlight lang="pascal" inline>label</syntaxhighlight>s, pure numeric identifiers are allowed, too.
 +
 
 +
In Pascal identifiers are ''case-insensitive''.
 +
<syntaxhighlight lang="pascal" inline>Foo</syntaxhighlight>, <syntaxhighlight lang="pascal" inline>fOO</syntaxhighlight>, and <syntaxhighlight lang="pascal" inline>FoO</syntaxhighlight> identify the very same object.
 +
This rule applies only for Pascal code, though.
 +
Inside inline-assembler statements [[Asm|<syntaxhighlight lang="delphi" inline>asm</syntaxhighlight> … <syntaxhighlight lang="delphi" inline>end</syntaxhighlight>]] identifiers are case-sensitive and may have to adhere to different structures.
 +
 
 +
Identifiers must not be [[Reserved word|reserved words]].
 +
However, since [[FPC]] retroactively declared some identifiers as reserved words, it introduced the [[&#identifier escape|<syntaxhighlight lang="delphi" inline>&</syntaxhighlight>-escape]] to allow their usage as identifiers anyway.
 +
Note, that the set of reserved words depends on the active [[Compiler Mode|compiler compatibility mode]].
 +
 
 +
Identifiers are only valid in the [[Scope|scope]] they were declared in.
 +
In other [[Block|blocks]] a symbol may have a different meaning or hide an already declared identifier.
 +
 
 +
== Example ==
 +
 
 +
<syntaxhighlight lang="pascal" highlight="1,4,7,9,11,15">program identifierDemo(input, output, stdErr);
 +
 
 +
const
 +
answer = 42;
 +
 
 +
var
 +
x: integer;
 +
 
 +
procedure foo;
 +
begin
 +
x := answer;
 +
end;
 +
 
 +
begin
 +
foo;
 +
end.</syntaxhighlight>
 +
This program contains the identifiers:
 +
* <syntaxhighlight lang="pascal" inline>identifierDemo</syntaxhighlight>
 +
* <syntaxhighlight lang="pascal" inline>input</syntaxhighlight>
 +
* <syntaxhighlight lang="pascal" inline>output</syntaxhighlight>
 +
* <syntaxhighlight lang="pascal" inline>stdErr</syntaxhighlight>
 +
* <syntaxhighlight lang="pascal" inline>answer</syntaxhighlight>
 +
* <syntaxhighlight lang="pascal" inline>x</syntaxhighlight>
 +
* <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>
 +
* <syntaxhighlight lang="pascal" inline>foo</syntaxhighlight>
 +
 
 +
== Significant characters ==
 +
 
 +
FPC considers up to 127 characters of an identifier as significant.
 +
A character being significant means, that only the first <math>n</math> characters are actually considered while determining whether one object or another is being referred to.
 +
 
 +
It is allowed to write identifiers ''longer'' than 127 characters.
 +
However, if two objects are/ought to be referred by two identifiers that start off with the same 127 characters, the 128th and beyond characters are ignored.
 +
The compiler will fail with a duplicate declaration compile-time error.
 +
 
 +
Due to internal reasons, all words are capped to 255 characters, though.
 +
(This limit does not concern string literals, which are – mathematically speaking – objects.)
 +
 
 +
Historically, there were [[Compiler|compilers]] that considered only the first eight characters as significant.
 +
This evoked very cryptic identifiers.
 +
 
 +
== Choosing good identifiers ==
 +
 
 +
There are some considerations in picking a “good” identifier:
 +
* Pascal’s strong typing system inherently “stores” an associated type with every identifier. Duplicating this information in writing is discouraged. If an identifier’s nature changes during development progress, it had to be changed throughout the entire code base in order to reflect the change. Thus, write what an identifier ''means'' or is good for, not what its underlying structure is.
 +
* Keep all identifiers in one (natural) language. Unless you are writing a program for educational purposes, English is de facto ''the'' lingua franca in programming.
 +
* Do not use FPC’s entire width of permissible [[#significant characters|significant characters]].
 +
* Do not omit vowels or use other shortening strategies. It tends to produce unreadable code.
 +
* Avoid using the same words as identifiers in multiple places, unless necessary or guaranteed to be free of conflicts, or intentional but ''documented''.
 +
 
 +
== See also ==
 +
 
 +
* [[With|<syntaxhighlight lang="pascal" inline>with</syntaxhighlight>]]
 +
* [https://www.freepascal.org/docs-html/ref/refse4.html § “Identifiers” in the ''Free Pascal reference guide'']
 +
* [https://en.wikipedia.org/wiki/Identifier_(computer_languages) Article “Identifiers (computer languages)” in the ''English Wikipedia'']

Latest revision as of 22:17, 4 July 2021

Deutsch (de) English (en) suomi (fi) français (fr) русский (ru)

An identifier is a symbol used to unambiguously denote an object. Such objects could be for instance


Declaration

In Pascal an identifier has to be declared prior its first use. Depending on the object’s nature the identifier declaration is crafted in one manner or another, but the general scheme [math]\displaystyle{ (\text{identifier}, \text{object}) }[/math] stays the same (2-tuple [that means, it is ordered]).

An identifier has to consist of at least one ASCII letter or underscore. After the first character ASCII digits may follow, too. In the case of labels, pure numeric identifiers are allowed, too.

In Pascal identifiers are case-insensitive. Foo, fOO, and FoO identify the very same object. This rule applies only for Pascal code, though. Inside inline-assembler statements asm … end identifiers are case-sensitive and may have to adhere to different structures.

Identifiers must not be reserved words. However, since FPC retroactively declared some identifiers as reserved words, it introduced the &-escape to allow their usage as identifiers anyway. Note, that the set of reserved words depends on the active compiler compatibility mode.

Identifiers are only valid in the scope they were declared in. In other blocks a symbol may have a different meaning or hide an already declared identifier.

Example

program identifierDemo(input, output, stdErr);

const
	answer = 42;

var
	x: integer;

procedure foo;
begin
	x := answer;
end;

begin
	foo;
end.

This program contains the identifiers:

  • identifierDemo
  • input
  • output
  • stdErr
  • answer
  • x
  • integer
  • foo

Significant characters

FPC considers up to 127 characters of an identifier as significant. A character being significant means, that only the first [math]\displaystyle{ n }[/math] characters are actually considered while determining whether one object or another is being referred to.

It is allowed to write identifiers longer than 127 characters. However, if two objects are/ought to be referred by two identifiers that start off with the same 127 characters, the 128th and beyond characters are ignored. The compiler will fail with a duplicate declaration compile-time error.

Due to internal reasons, all words are capped to 255 characters, though. (This limit does not concern string literals, which are – mathematically speaking – objects.)

Historically, there were compilers that considered only the first eight characters as significant. This evoked very cryptic identifiers.

Choosing good identifiers

There are some considerations in picking a “good” identifier:

  • Pascal’s strong typing system inherently “stores” an associated type with every identifier. Duplicating this information in writing is discouraged. If an identifier’s nature changes during development progress, it had to be changed throughout the entire code base in order to reflect the change. Thus, write what an identifier means or is good for, not what its underlying structure is.
  • Keep all identifiers in one (natural) language. Unless you are writing a program for educational purposes, English is de facto the lingua franca in programming.
  • Do not use FPC’s entire width of permissible significant characters.
  • Do not omit vowels or use other shortening strategies. It tends to produce unreadable code.
  • Avoid using the same words as identifiers in multiple places, unless necessary or guaranteed to be free of conflicts, or intentional but documented.

See also