3 ms·
another way to accomplish this goal is to write your function using only const variables. every time you'd mutate a variable, use a new const variable instead.
by blt 11y ago
another way to accomplish this goal is to write your function using only const variables. every time you'd mutate a variable, use a new const variable instead. you end up transforming the code into static-single-assignment form, which is what compilers do anyway, so you can be sure it won't degrade performance.
this style really makes you wish C/C++ had if expressions. you end up using a lot of ternary operators to compensate.
it makes the code a bit more verbose, but its worlds better than poring through a function to make sure nothing is mutated in an unexpected way.
- eru 11y agoI reach for Haskell whenever possible to get similar effects.
- mattnewport 11y agoI actually prefer ternary expressions in many situations, once you're familiar with the syntax they're more concise and more clearly an expression rather than flow control.
- JadeNB 11y ago> you end up transforming the code into static-single-assignment form, which is what compilers do anyway, so you can be sure it won't degrade performance. I don't know my way around compiler internals, but this sounds a little suspect to me. It seems to me—just a tyro's opinion—that (1) work spent doing what the compiler will do anyway is probably work wasted, and (2) it is possible to get in the compiler's way even if you are trying just to do what it would do anyway, thus causing unexpected degradation.