Sample code of Local Functions https://github.com/rhammonds/LocalFunctionSamples
I like Local Functions for the most part but every developer has different tastes. We all tend to function differently as well. I regularly use the drop-downs at the top of the screen to select classes and methods and it is cumbersome to see so many private functions. Also, many times we write logic or helper methods that are only helpful in one method.
And there are those who are fine with methods that have 100s of lines of code that I find difficult to read. There are those who see no issue in writing complex, multi-level if/else statements, which for me is so painful.
I prefer reading straightforward code, like a bullet list of steps. I believe local functions can help with this. They help break up complexity in a method without adding a bunch of private methods, which you don’t really want any other code to share. Methods are easy to understand when they are written such that they do one thing even if it is a group of related steps.
The main benefits of Local functions are really that you don’t have to write a private method, you can keep the function non-shareable and related methods are kept together. An example of related code may also be some code that reiterates and calls another method or piece of logic that is only used in the code.
It could also help if you are refactoring a very long method. You could slowly break out logic into smaller functions that you can later move around to something more object-oriented.
Now ideally, code should be OO and not procedural. I find it difficult to write all code in OO style, a lot of developers do not like it when I do that. Much of code is just passing data to other systems or applying business logic and OO-style is not always applicable or easy. Anyways, when you find yourself writhing procedural code in .NET, consider using local functions.
- Coding style feature
- https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions
- Pretty much follows the same rules as methods.
- Useful for long methods. Not a panacea. Just another alternative.
- C# Version
- Introduced in C#7
- Static added in C#8
- Attributes added in C#9
- Benefits
- Although it’s subjective and everyone differs, it can make code cleaner to read.
- Again it’s subject, it can write the language to be more direct without all the distracting details.
- Can reduce the need for commenting and using regions
- Complex code can be written as a method with a descriptive name
- They also make it impossible for another developer to mistakenly call the method directly from elsewhere in the class or struct.
- Reduces the need for private functions
- Some memory improvements versus Lambda expressions
- Improves iterator and Task methods exception handling
- Problems LF solves
- Methods with a lot of comments
- Class with several one-off private methods
- Long Methods
- Methods with a lot of Sanitizing/validation logic
- Error handling in iterators and Task methods
- Disadvantage
- It can lead to code duplication
- Doesn’t resolve long methods
- Might be overkill in small methods