Visibility (programming)

from Wikipedia, the free encyclopedia

In structured and object-oriented programming , visibility indicates in which program parts the identifiers of data types , variables , functions or constants as well as procedures or methods that have been defined in a specific program unit such as a class or a module can be used.

The visibility rules are set in the associated interfaces with the help of access modifiers . The usual division is:

  • public (modifier "public"), the element can be accessed from any other class.
  • protected (Modifier "protected"), the element may (usually) only be accessed from the same or an extended class.
  • private (modifier "private"), the element may only be accessed within the defining area.

In order to limit the possibility of accidental mix-ups and programming errors , it is advisable to restrict the visibility of identifiers as much as possible (shading) . In structured programming languages , the visibility of the parameters and local variables of procedures, functions and methods is limited to these themselves. Data types or procedures can be referenced recursively within the visibility area. Global variables are visible in the entire corresponding program unit (for example in a program package , in a module or in a class).

To make it easier to distinguish between local and global variables in the source code, many programmers use different naming conventions , for example, identifiers that start with a lowercase letter for local variables and names that start with an uppercase letter for global variables .

blocks

In many, but not all, block-structured programming languages , the scope can be limited to a block called the block area. Most of the time, this block is contained in a function, which limits the scope to part of a function .

A representative example of the use of the block area is the code shown here in the C programming language , in which two variables are applied to the loop : the loop variable n , which is initialized once and incremented with each iteration of the loop, and the auxiliary variable n_squared , which is initialized with each iteration.

unsigned int sum_of_squares(const unsigned int N)
{
    unsigned int ret = 0;
    for (unsigned int n = 1; n <= N; n++)
    {
        const unsigned int n_squared = n * n;
        ret += n_squared;
    }
    return ret;
}

This is to avoid adding variables to the range of functions that are only relevant for a specific block. This prevents, for example, errors in which the generic loop variable i has inadvertently already been set to a different value. In this example, the expression n * n would generally not be assigned to any auxiliary variable, and the body of the loop would simply be written ret + = n * n , but auxiliary variables are useful in more complicated examples.

Blocks are mainly used for control flow, e.g. B. with if statements , while loops and for loops . In these cases, block size means that the size of the variable depends on the structure of the execution flow of a function . Block-range programming languages , however, typically also allow the use of "bare" blocks, the sole purpose of which is to provide fine-grained control over the variable range. For example, an auxiliary variable can be defined in a block, then used (for example, added to a functional variable) and discarded when the block ends, or a while loop can be enclosed in a block that initializes the variables used within the loop should only be initialized once.

One nuance of several programming languages like C is that block range variables can be declared not only within the block body but also within the control statement, if any. This is analogous to function parameters that are declared in the function declaration before the start of the block of the function body, and in the area of validity for the entire function body. This is mainly used for loops whose initialization statement is separate from the loop condition, unlike while loops , and is a common phrase.

The block area can be used to hide. In this example, the auxiliary variable within the block could also have been called n , which hides the parameter name. However, this is considered bad style due to the possibility of mistakes. In addition , despite the support of block scope , some descendants of C , such as Java and C # , do not allow one local variable to hide another, as a local variable can go out of scope before the end of a function . In such languages, attempting to declare the second n would result in a syntax error and one of the variables would have to be renamed.

example

The use of visibilities can look like this (PHP):

<?php
class SampleClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';

    public function printHello()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}

$obj = new SampleClass();
echo $obj->public; // Funktioniert
echo $obj->protected; // Fataler Fehler
echo $obj->private; // Fataler Fehler
$obj->printHello(); // Zeigt Public, Protected und Private

See also

literature

Individual evidence

  1. http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_05_002.htm#mjf5b3fff9748ae6323d1923016a906a8f
  2. a b Interfaces , in: Structured Programming , Wikibook, accessed on September 6, 2018
  3. Björn and Britta Petri: Modifiers , Java Tutorial, 2010–2018, accessed on September 6, 2018
  4. http://php.net/manual/de/language.oop5.visibility.php
  5. a b Hanspeter Mössenböck , Niklaus Wirth : Declarations and scope rules , in: The Programming Language Oberon-2 , Institute for Computer Systems, ETH Zurich , October 1993, accessed on September 6, 2018
  6. Variables and Methods , in: Structured Programming , Wikibook, accessed on September 6, 2018
  7. PHP: Visibility - Manual. Retrieved August 28, 2018 .