This is an extremely difficult problem, both in practice and in theory. We think a lot about how to prevent or isolate side effects for your scenarios - memoization, automatic parallelization , etc. - but it is difficult, and we are still far from a workable solution for C #. So no promises. (Consider switching to Haskell if you really want to eliminate side effects.)
Unfortunately, even if a miracle happened and you found a way to prevent the recall of methods with side effects, you still have big problems. Consider the following:
1) What if you memoize a function that calls the memoized function itself? This is a good situation, right? You want to be able to create memoized functions. But memoization has a side effect: it adds data to the cache! So, right away you have a meta problem: you want to tame side effects, but only the βbadβ side effects. The "good" that you want to promote, the bad that you want to prevent, and it's hard to tell them apart.
2) What are you going to do with exceptions? Can you memoize the method that throws the exception? If so, does it always raise the same exception, or does it throw a new exception each time? If first, how are you going to do this? If the latter, now you have a memoized function that has two different results in two different calls, because two different exceptions are thrown. Exceptions can be seen as a side effect; itβs hard to tame exceptions.
3) What are you going to do with methods that do not have a side effect, but nevertheless unclean methods? Suppose you have a GetCurrentTime () method. This has no side effect; The call has not been changed. But this is not a candidate for a memorandum, because any two calls are required to get different results. You do not need a side effect detector, you need a purity detector.
I think your best bet is to solve a human problem through education and code reviews, rather than trying to solve a complex technical problem.
source share