Skip to main content
Post Made Community Wiki by Aniket Inge
Source Link
Earlz
  • 23k
  • 7
  • 48
  • 62

I think the fact that the method is very long is something to check on, but definitely not an instant anti-pattern. The big thing to look for in huge methods is lots of nesting. If you have

foreach(var x in y)
{
    //do ALL the things
    //....
}

and the body of the loop isn't extremely localized(ie, you could send it less than 4 parameters), then it's probably better to convert it to:

foreach(var x in y)
{
    DoAllTheThings(x);
}
...
void DoAllTheThings(object x)
{
    //do ALL the things
    //....
}

In turn, this can vastly reduce the length of a function. Also, make sure to look for duplicate code in the function and move that into a separate function

Finally, some methods are just long and complicated and there is nothing you can do. Some problems need solutions that do not lend themselves to easy to code. For instance, parsing against a grammar of much complexity can make really long methods that you really can't do much about without making it worse.