Imagine I have this code:
public void Foo()
{
// Do bar work
// Do baz work
// Do foobar work
}
And I realize I can (and should because it was doing more than one thing) refactor it into:
public void Foo()
{
bar();
baz();
foobar();
}
private void bar() { /* do bar work */ }
private void baz() { /* do baz work */ }
private void foobar() { /* do foobar work */ }
But then I realize I will never use these functions outside of Foo()
, so those functions are just cluttering the main page and the auto-complete. I could get away with this:
public void Foo()
{
bar();
baz();
foobar();
void bar() { /* do bar work */ }
void baz() { /* do baz work */ }
void foobar() { /* do foobar work */ }
}
Which would make things neater and less clutter, but all I've really done now is made the method even longer instead of shorter.
Which would make things neater and less clutter, but all I've really done now is made the method even longer instead of shorter.
No, you haven’t. You are basically saying something similar to not being any difference between a class with one method that does a whole lot of stuff and a class that does the same job but with multiple shorter and easier to mantain methods.
Your local functions are just like methods, the fact that they are contained within another method doesn’t preclude that the whole is much easier to maintain; functionality is encapsulated in clearly defined scopes.