Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local functions and SOLID principles C#

I know that starting from C# 7.0 we are able to create local functions, but how is this related with the SOLID principles to achieve a good design model?

I mean, doesn't this break the Single Responsibility Principle, adding a function inside another function?

We can delegate that simple task to be computed either in another method or in another new class? and for the Open-Closed principle which allows me to inherit from SomeClass to modify it is now more complex, I now need to rewrite the entire base function instead of just one part of my code?

Probably we just need to re write some part of the method instead of changing the whole functionality on it.

like image 741
Diego Osornio Avatar asked Jun 01 '18 04:06

Diego Osornio


3 Answers

I agree with you to an extent. Whether the answers here support so called god (little g) methods or not I personally feel, just as you do, that methods should be precise in what they do.

That said, local functions can in fact support SOLID. One example is the ability to easily wire up call backs from events giving the method single responsibility, close the scope, and minimize error. This can be done with local delegates but it isn't as clean or straight forward.

Recursive local functions are also in line with SOLID. There are many situations where you have to write two methods to complete one operation because it's a recursive method and now you can put that logic all in one place. There are even times, depending on the data and types being passed around, that local functions will actually perform better due to the fact they are borrowing scoped variables.

This is only two quick examples that probably don't require a debate or lots of code to illustrate how local functions can be better and inline with SOLID but there are more.

Local functions can also easily polute your code and make methods hard to manage / read and tear away from SOLID principles. Just like any other C# component we have a responsibility to pick and choose when and why we use local functions and SOLID as well as other development practices / guidelines should be considered when we do. I don't agree with making methods longer, more complex, or having more than one job to do just for the sake of scoping methods to it.

like image 109
Michael Puckett II Avatar answered Oct 19 '22 22:10

Michael Puckett II


we are able to create local functions, but how is this related with the SOLID principles to achieve a good design model?

Local functions help you create good code by making it more readable and maintainable. That's not really related to SOLID, though.

does not the Single Responsibility is broken adding a function inside another function?

How so? The Single Responsibility Principle states that "every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class". How you organize the code inside the class doesn't change the level of responsibility or encapsulation implemented.

like image 41
Konamiman Avatar answered Oct 19 '22 20:10

Konamiman


Sometimes you do not need to pollute your base context with another method which it will use just in one method, But you need that as an anonymous method to do something in your code, I recommend you to take a look on this

like image 38
Mohammad Nikravesh Avatar answered Oct 19 '22 21:10

Mohammad Nikravesh