Scope of variables in OOP service

Scope of variables in OOP service

> LeekScript Tutorial

This page is only valid for LS1.0, in LS1.1 the classes are directly usable: Classes and Objects

In this article, we will create pseudo OOP (Object Oriented Programming), since LeekScript is not an object oriented language. First, I'll introduce the notion of OOP, then I'll explain how variable scope works. In order not to be too theoretical, we are going to program a structure called "Binary Search Tree", it is a structure optimized for ordered insertion and searching.

Notion of OOP

This notion is very important if you start to take an interest in other languages such as C (++/#), PHP, Java, etc. It's a new way to build your programs. She introduces objects. You surely know what it is in everyday life, it's something transformable/manipulable, but in programming we call "object" a structure made up of variables and functions called '''attributes and methods''. ''. Let's take the example of a car, it is characterized by a speed, an acceleration (let's not go into the details either) as well as actions such as turning, accelerating, braking... Here is how we could represent our car:

This allows for a structured and clean code. But also to simplify the use of actions composed of complex instructions that will be hidden from the user. Another very important thing in OOP is the principle of encapsulation... All attributes and methods do not have to be manipulated by the user who could "break" the object if it does not check such and such things. Let's imagine that the user modifies the speed of our car during the game and does not take into account inertia or something else, the car would behave strangely, and even this is only a minor modification. Encapsulation is therefore the principle of making parts of the object invisible to the user, this is called private attributes/methods, on the contrary if the user has the possibility of using attributes/methods , these are public.

The definitions of these objects are called "classes", they are used by creating a new instance of the class as with a normal type (numbers, arrays, strings...). It's just that in most compilers (if not all) this instantiation is implicit: var a = 5; creates a variable of the number object with the value '5'. I speak of numbered objects by abuse of language. A difference with the OOP are the functions applied to a variable:

let a = 1;

//Without OOP

add(a, 1); // defined by function (@obj, nbr) { obj += nbr; }

// OOP

a.add(1); // defined by method add (nbr) { this += nbr; }

The 'add' function takes a number to add as a parameter. The difference between the two versions is the context, in the first case 'add' does not know which object to apply these instructions to, it needs a reference to this number (the 'obj' parameter). Whereas in the second function, it is a method. It therefore knows the object that must be modified since 'add' belongs to 'a'. Note: The syntax is totally arbitrary, you just have to understand the principle.

FYI, 'add' is public if you followed since the user can call the function.

Variable scope

You have probably already used global variables, they allow you to keep information between rounds and to be able to access it anywhere in your code. What differentiates them from "local" variables is their "scope". Take the following code:

global _global;

function scopeTest () { var localFunction; }

var localMain;

Each of the variables above have a different scope: _global is accessible everywhere on all towers, localMain is accessible only in the main block (not in functions), localFunction is only accessible in the "testScope" function. The scope also applies to functions, indeed scopeTest is accessible to all towers from everywhere, whereas an anonymous function is only accessible in the space where it was created (like a variable).

The scope of a variable applies to all blocks ( block symbol: '{' and '}' ). Thus a variable created in an if, a while, a do/while, a for is not accessible outside of it:

if (true) { var localIf = 0;// Creation of localIf } // end of life of localIf

debug(localIf); // Error: (localIf) unknown variable or function

for (var i = 0; i < 5; ++i) { // Creation of i