Skip to content

AV2406: Add relative position of local functions #111

@bkoelman

Description

@bkoelman

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

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