You are here: Home > PLC C++ > PLC Chapter III – Names, Binding, and Scopes

PLC Chapter III – Names, Binding, and Scopes

A. Naming Convention
There are various things that have to be paid attention to when giving name for a variable, class, and such in C++, such as the following:

– Naming should be unique and a variable should not have the same name with other variables in the same scope.
– Reserved words and keywords may not be used for a variable name.
– Naming can only consist of alphabets (A-Z, a-z), numbers (-0-9), dollar symbol ($), and underscore (_).
– The first character of the name should be either an alphabet or an underscore.
– Names in C++ are case-sensitive, so, for example, the variable index and Index are different from each other. This may cause a problem in both readibility and writability as it considers similar names as different.
– While there are no limit in character length, during execution the program will only recognize the first 32 characters of the name.
– It is advised to use relevant, short, and clear names for variables, classes, etc., so when the coder or other people read the codes again, they would not be confused of what are the use of several variables and the variables would be easier to use.

Pay attention to the following snippet of codes:
int main() {

int 2nama, nim, _alamat, for;
cin>> 2nama >> NIM >> _alamat >> for;

}
There are a few mistakes that can be seen from the codes above:

– During the declaration (line 1), the variable 2nama violated the rule that a name can only be started with alphabets or underscore, not numbers. The variable for can’t be used too, as for is already a keyword used for looping function.
– During the value input of the variables (line 2), same as line 1, variables 2nama and for violate the rule. In addition, athe variable NIM would cause an error too as the compiler does not recognize that name, because C++ is a case-sensitive language, so it considers the variable nim declared on the first line different from the variable NIM used on the second line.


B. Binding
Binding is the association between an entity and an attribute, such as the association of a variable and its attributes like name, address, data type, value, lifetime, and scope. Binding time is the time at which binding takes place.
The examples of binding time:

o Language design time
o Language implementation time
o Program translation / compile time
o Link edit time
o Load time
o Run time

For a variable to be able to be referenced in a program, first it need to be bound to a type, which is called type binding. It can be differentiated into two kinds of type binding:

o Static type binding, binding that took place during compile time. The data type that the variable is bound to is decided either by directly specifying a data type to the variable name (explicit) or by default associating a variable with a type during compile time (implicit). In C++, Explicit static type binding is usually used.

o Dynamic type binding, where a variable is bound to a type when it is assigned a value in an assignment statement (at run time, take the type of the value on the right-hand side). By using dynamic type binding, it’s more flexible compared to static type binding, but error detection is harder and prone to typing errors. In C++, dynamic type binding can be used with the help of template by using generics.


C. Scope
The scope of a variable is the range of statements over which it is visible. The scope rules of a language determine how references to names are associated with variables. In C++, the scope of a variable depends on the block where the variable is declared or called. There are two kinds of scope, static and dynamic scope. Depending on the scope it is in, a variable can be divided into two kinds:

o Local Variable is variable declared in a block of program and can only be called in said block.
o Global Variable is variable declared outside of all block of programs and can be called from anywhere after its declaration. In the case when a global variable and a local variable share a name, the local variable will be prioritized if the variable name is called inside the same block of that local variable.

– Static Scope
Static scope is based on program text and connect a name reference to a variable by finding the declaration. The search process of the declaration starts locally, then in increasingly larger enclosing scopes, until one is found for given name.
– Dynamic Scope
Dynamic scope is based on calling sequences of program units, not their textual layout. References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point.

Tags: , ,

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Leave a Reply