Regarding the returns I know that being void functions do not return any value, even so a teacher told me that as a good practice she will always use them - Manuel Aviles
An important distinction.
It does not matter if a function returns a value or does not return it, an instruction return
will behave in the same way in both cases: it will exit the running function. The only difference will be that, together with the instruction return
, a value will be returned (or not).
Early exit of the function.
It is considered early exit of a function to leave without having reached the end of the function, this is done using the return
instruction, which creates new exit points of the function to the end of the function.
Like many other programming topics, the single exit principle 1 is misinterpreted or decontextualized or abused.
There is nothing wrong with leaving a function with a return
instruction in any part of a function (whether the function returns something or not).
The mechanisms of the language when leaving a function are the same whether it comes out early or leaves the function, the only possible difference would be that certain instructions would not be executed (thus avoiding their side effects ) but it would happen exactly the same with a if
instruction.
Multiple function outputs What is the problem?
If there is nothing wrong with it, what is the problem? It is not a performance problem, but a design problem, sometimes having several exit points of a function (whether it returns something or not) makes it more difficult to follow the logic of the function and consequently it will be more difficult to debug and work with it, some programmers defend the principle of single exit 1 for this reason.
Other programmers defend the single exit principle 1 for performance, but there will be no performance difference between following or not following this principle nor c ++ or in java (or in any modern programming language) so defending that position is just a misinterpretation of the principle.
Are you sure there's no problem?
We can find problems with multiple exit points in a function (early function exits) in fortran or cobol or assembler since in these languages this type of practice is prone to errors (they do not follow the recommendations of Edsger W. Dijkstra on structured programming) by making it possible for some variables to be uninitialized or for a certain code crucial does not run; but it is due to particularities of these languages, modern languages do not have these problems.
Structure the code.
When you have a long function that can exit at several points (returning or without returning value) we can structure it as the Acueducto de Amoreira 2 or as the Temple cursed 2 :
Acueducto Amoreira
if (condicion_1)
{
// hacer cosas 1
if (condicion_2)
{
// hacer cosas 2
if (condicion_3)
{
// hacer cosas 3
if (condicion_4)
{
// hacer cosas 4
if (condicion_5)
{
// hacer cosas 5
// ...
}
else
{
// Otras cosas
}
}
else
{
// Otras cosas
}
}
else
{
// Otras cosas
}
}
else
{
// Otras cosas
}
}
else
{
// Otras cosas
}
Templo Maldito
if (!condicion_1)
{
// Otras cosas, como salir de la función
return;
}
// hacer cosas 1
if (condicion_2)
{
// Otras cosas, como salir de la función
return;
}
// hacer cosas 2
if (condicion_3)
{
// Otras cosas, como salir de la función
return;
}
// hacer cosas 3
if (condicion_4)
{
// Otras cosas, como salir de la función
return;
}
// hacer cosas 4
if (condicion_5)
{
// Otras cosas, como salir de la función
return;
}
// hacer cosas 5
In the Amoreira Aqueduct the code ripples through multiple indentations (making it look like an aqueduct) and the function flows at the end without going through instructions return
, there is a single point of entry and exit. In The Cursed Temple, the function passes different tests and if it does not exceed them, it comes out with a return
instruction, there is an entry point and several exits.
Neither of the two structures is better than the other, but, according to the tastes of the programmer: one will be easier to use than the other.
1 I have just coined the name, I do not know that the practice of having only one exit point in a function is officially known.
2 I have just coined the name, I am not aware that this structure receives this name.