Nesting (computing)


In computing science and informatics, nesting is where information is organized in layers, or where objects contain other similar objects. It almost always refers to self-similar or recursive structures in some sense.
Nesting can mean:
In a spreadsheet functions can be nested one into another, making complex formulas. The function wizard of the OpenOffice.org Calc application allows to navigate through multiple levels of nesting, letting the user to edit each one of them separately.
For example:
=IF
In this Microsoft Excel formula, the SUM function is nested inside the IF function. First, the formula calculates the sum of the numbers in the cells from C8 to G8. It then decides whether the sum is 0, and it displays the letter Y if the sum is 0, and the letter N if it is not.
Naturally, to allow the mathematical resolution of these chained formulas, the inner expressions must be previously evaluated, and this outward direction is essential because the results that the internal functions return are temporarily used as entry data for the external ones.
Due to the potential accumulation of parentheses in only one code line, editing and error detecting can became somehow awkward. That is why modern programming environments -as well as spreadsheet programs- highlight in bold type the pair corresponding to the current editing position. The balancing control of the opening and closing parenthesis known as brace match checking.

In programming

Control Structures

In structured programming languages, nesting is related to the enclosing of control structures one into another, usually indicated through different indentation levels within the source code, as it is shown in this simple BASIC function:

function LookupCode as integer
dim sLine, path as string
dim ReturnValue as integer
path="C:\Test.dsv"
if FileExists then
open path for input as #1
do while not EOF
line input #1, sLine
if codeleft then
'Action to be carried out
End if
loop
close #1
End if
LookupCode=ReturnValue
end function

In this small and simple example, the conditional block “if... then... end if” is nested inside the “do while... loop” one.
Some languages such as Pascal and Ada have no restrictions on declarations depending on the nesting level, allowing precisely nested subprograms or even nested packages. Here is an example of both :

-- Getting rid of the global variables issue
-- from a set of old sources, without the need to change that code's
-- logic or structure.
--
procedure Nesting_example_1 is
type Buffer_type is array of Integer;
procedure Decompress
is
-- Here are the legacy sources, translated:
package X_Globals is
index_in, index_out: Integer;
-- *** ^ These variables are local to Decompress.
-- *** Now Decompress is task-safe.
end X_Globals;
-- Methods 1,2,3,...
package X_Method_1 is
procedure Decompress_1;
end X_Method_1;
-- Methods 1,2,3,...
package body X_Method_1 is
use X_Globals;
procedure Decompress_1 is
begin
index_in:= compressed'First;
-- Here, the decompression code, method 1
end Decompress_1;
end X_Method_1;
-- End of the legacy sources
begin
X_Method_1.Decompress_1;
end Decompress;
test_in, test_out: Buffer_type;
begin
Decompress;
end Nesting_example_1;

Data Structures

Nested data structures are also commonly encountered in programming.

Lisp

In the functional programming languages, such as Lisp, a list data structure exists as does a simpler atom data structure.


The atoms in the list are A, T, O, M, and S.

U N N E C E S S A R I L Y