Skip to content

method too long should not take local functions into account #24

Open

Description

@sbradl

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.

Activity

  1. MO2k4 commented on May 27, 2020

    @MO2k4
    Owner

    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

  2. sbradl commented on May 27, 2020

    @sbradl
    Author

    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.

  3. sbradl commented on May 27, 2020

    @sbradl
    Author

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

  4. MO2k4 commented on May 27, 2020

    @MO2k4
    Owner

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

  5. sbradl commented on May 27, 2020

    @sbradl
    Author

    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;
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions