Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AV2406: Add relative position of local functions #111

Closed
bkoelman opened this issue Oct 17, 2017 · 1 comment
Closed

AV2406: Add relative position of local functions #111

bkoelman opened this issue Oct 17, 2017 · 1 comment

Comments

@bkoelman
Copy link
Contributor

Existing rule:

Place members in a well-defined order (AV2406)
Maintaining a common order allows other team members to find their way in your code more easily. In general, a source file should be readable from top to bottom, as if reading a book, to prevent readers from having to browse up and down through the code file.

New language features to consider:

  • local functions
    They can be declared anywhere in method bodies (this includes property/event accessors, anonymous methods, lambda expressions and other local functions)

Based on the guidance "a source file should be readable from top to bottom" I think its best to require their declaration at least below its first usage. Example:

public void M()
{
    string userInput = ReadText();

    string ReadText()
    {
        return Console.ReadLine();
    }

    if (userInput == "X")
    {
        return;
    }
    else
    {
        Console.WriteLine("You typed: " + userInput);
    }
}

But I think the code becomes more readable if they are declared at the end, after all executable code. Example:

public void M()
{
    string userInput = ReadText();

    if (userInput == "X")
    {
        return;
    }
    else
    {
        Console.WriteLine("You typed: " + userInput);
    }

    string ReadText()
    {
        return Console.ReadLine();
    }
}
@dennisdoomen
Copy link
Owner

I agree. If a local function is warranted, then it should be at the end of the containing member.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants