Difference between revisions of "Identifier"

From Lazarus wiki
Jump to navigationJump to search
(→‎example: add new external links)
m (Fixed syntax highlighting)
Line 8: Line 8:
 
* [[Type|types]].
 
* [[Type|types]].
  
== declaration ==
+
 
 +
== Declaration ==
 +
 
 
In [[Pascal]] an identifier has to be declared prior its first use.
 
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>(\text{identifier}, \text{object})</math> stays the same (2-tuple [that means, it is ordered]).
 
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]).
Line 28: Line 30:
 
In other [[Block|blocks]] a symbol may have a different meaning or hide an already declared identifier.
 
In other [[Block|blocks]] a symbol may have a different meaning or hide an already declared identifier.
  
== example ==
+
== Example ==
 +
 
 
<syntaxhighlight lang="pascal" highlight="1,4,7,9,11,15">program identifierDemo(input, output, stdErr);
 
<syntaxhighlight lang="pascal" highlight="1,4,7,9,11,15">program identifierDemo(input, output, stdErr);
  
Line 55: Line 58:
 
* <syntaxhighlight lang="pascal" inline>foo</syntaxhighlight>
 
* <syntaxhighlight lang="pascal" inline>foo</syntaxhighlight>
  
== significant characters ==
+
== Significant characters ==
 +
 
 
FPC considers up to 127 characters of an identifier as significant.
 
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.
 
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.
Line 69: Line 73:
 
This evoked very cryptic identifiers.
 
This evoked very cryptic identifiers.
  
== choosing good identifiers ==
+
== Choosing good identifiers ==
 +
 
 
There are some considerations in picking a “good” identifier:
 
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.
 
* 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.
Line 77: Line 82:
 
* Avoid using the same words as identifiers in multiple places, unless necessary or guaranteed to be free of conflicts, or intentional but ''documented''.
 
* 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 ==
+
== See also ==
 +
 
 
* [https://www.freepascal.org/docs-html/ref/refse4.html § “Identifiers” in the ''Free Pascal reference guide'']
 
* [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'']
 
* [https://en.wikipedia.org/wiki/Identifier_(computer_languages) Article “Identifiers (computer languages)” in the ''English Wikipedia'']

Revision as of 10:57, 17 February 2020

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