You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
method too long should not take local functions into account #24
Should this really be the goal, to reduce visibility? ;-)
I am not sure nested functions are such a good pattern, because if you are using nested functions you could have also written a normal method which could be reused and tested on their own. A nested function increases the complexity of the outer function.
Consider 2 public methods. Each of them calls a (different) private method. Without local functions both private methods are visible to both public ones. Uncle Bob himself describes in his videos about functions that those local functions is exactly what he would like to have in Java.
Testing is not a concern because local functions are private.
Why should those two cases be treated differently? Semantically they are the same. The only difference is the scope. And reducing scopes is quite important. That's the reason why we have access modifiers don't we?
public void DoSomething()
{
if (CheckSomething())
DoSomethingElse();
bool CheckSomething()
{
return true;
}
}
public void DoSomething()
{
if (CheckSomething())
DoSomethingElse();
}
private bool CheckSomething()
{
return true;
}
Activity
MO2k4 commentedon May 27, 2020
Should this really be the goal, to reduce visibility? ;-)
I am not sure nested functions are such a good pattern, because if you are using nested functions you could have also written a normal method which could be reused and tested on their own. A nested function increases the complexity of the outer function.
Stop nesting functions but not all of them
Does nested function promote clean code
sbradl commentedon May 27, 2020
Consider 2 public methods. Each of them calls a (different) private method. Without local functions both private methods are visible to both public ones. Uncle Bob himself describes in his videos about functions that those local functions is exactly what he would like to have in Java.
Testing is not a concern because local functions are private.
sbradl commentedon May 27, 2020
Your first link talks about performance of nested functions in javascript. It does not apply to C#.
MO2k4 commentedon May 27, 2020
But would this not increase the complexity (loc) of the outer function? Could you please make a concrete example?
sbradl commentedon May 27, 2020
Why should those two cases be treated differently? Semantically they are the same. The only difference is the scope. And reducing scopes is quite important. That's the reason why we have access modifiers don't we?