CleanCode icon indicating copy to clipboard operation
CleanCode copied to clipboard

method too long should not take local functions into account

Open sbradl opened this issue 5 years ago • 5 comments

Nested functions should be counted as separate functions instead of as part of it's parent. They are a nice way to reduce visibility of code.

sbradl avatar May 27 '20 07:05 sbradl

They are a nice way to reduce visibility of code.

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

MO2k4 avatar May 27 '20 10:05 MO2k4

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 avatar May 27 '20 11:05 sbradl

Your first link talks about performance of nested functions in javascript. It does not apply to C#.

sbradl avatar May 27 '20 11:05 sbradl

But would this not increase the complexity (loc) of the outer function? Could you please make a concrete example?

MO2k4 avatar May 27 '20 13:05 MO2k4

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;
}

sbradl avatar May 27 '20 14:05 sbradl