DEV Community


Discussion on: Should function arguments be reassignable or mutable?

michelemauro profile image

In languages that manage memory for you (i.e. Java, or in general garbage collected runtimes), even if imperative, it's better to leave parameters as they are: the compiler may want to do some optimizations on their passing, and won't be able to if you modify them.
If you have a functional language, those optimizations are probably the norm, and you won't be able to do it at all (thus, you have one less problem to worry about).
If you have a runtime where you manage the memory directly (C, C++, or in embedded/IoT situations) there may be some circumstances where you're better off mutating your parameters; but you should be able to recognize them and use them correctly.

Otherwise, is much better to leave your parameters alone: they make the code much easier to reason about.

mortoray profile image
edA‑qa mort‑ora‑y Author

Part of my motivation for the question was a defect in the Leaf compiler (my language). The fix is relatively simple, but it does imply an efficiency lost for calling functions.

As you say, if I limit what can be done with arguments by default, I gain a lot of flexibility in the compiler.