Lowering in programming refers to the process of transforming code written in a higher-level language, construct, or syntax into a lower-level equivalent. For example, lowering might convert Python into machine code or assembly language, which can be executed more efficiently. Or it might transform code into a different programming paradigm, like object-oriented code into functional code.
Lowering is usually done to improve performance, enable code to run on different platforms and environments, and implement more expressive high-level features. It's part of every program execution and happens automatically in most cases by the compilation step and any underlying language runtime.
Take a foreach
construct for example. This code in C# is the typical simple and expressive syntax for iterating over a collection.
var collection = new[] { 1, 2, 3, 4, 5 };
foreach (var item in collection)
{
Console.WriteLine(item);
}
However foreach
loops can also be expressed as a lower-level for
loop.
var collection = new[] { 1, 2, 3, 4, 5 };
for (var i = 0; i < collection.Length; i++)
{
Console.WriteLine(collection[i]);
}
And foreach
and for
loops can be written as a simple while
loop.
var collection = new[] { 1, 2, 3, 4, 5 };
var i = 0;
while (i < collection.Length)
{
Console.WriteLine(collection[i]);
i++;
}