think about three problems A, B, and C.
A uses x,y methods to be solved;
B uses x,z and
C uses x,y,z.
which one would be better and which one could give better performance, and which one could be more readable:
"writing three different methods"
"writing one method and taking one or two parameter and deciding which part will run in if statement"
And please explain why.
You need to make a tradeoff between performance and modularity here.
Performance - When you make three methods probably there is a higher overhead of saving the current function on the stack and allocating a new function frame and making a call etc. But with every day our machines & memories are getting faster and larger, and the very fact that you've choosen to write code on .NET framework itself says something in itself. Performance for this kind of trivial decision is not really a big deal, so I'd prefer to make it modular.
Modularity - A method exactly does one thing, and can be easily understood by other programmers without actually having to understand how you think, is a better code in the long run. Imagine the cost of bugs, fixes, testing, time it takes to understand your code over and over by different developers at different points in time in future. If you have a method/function which is very modular, which can be tested in isolation also has a higher chance of reusability compared to a complex function, as other developers feel confident if they understand your API/code or both.
This is a personal opinion and I'd lean towards lower cost of maintanance in long term, higher reusability, testability over performance.
I would break apart the feature you are implementing with x, y, z and make as many methods. This follows the simple simple rule: Every method does one job nothing more nothing less. You can then compose these methods to achieve whatever features are wanted for A, B, C.
Fastest would be to write a function for A,B,C but it would probably eat more ram. Every method call means a jump in the machine code and so costs processor time. The difference is too small to be worth the higher maintenance. Thats why you make more functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With