3 ms·
Your global variables were / are shared with yourself, even in no-concurrency case. With typical control flow being far from linear, it's easy to surprise yours
by nine_k 4y ago
Your global variables were / are shared with yourself, even in no-concurrency case. With typical control flow being far from linear, it's easy to surprise yourself with unexpected state mutation.
- Scarbutt 4y agoHence the value of immutability even for single-threaded code.
- Jtsummers 4y agoGiving me flashbacks to inherited codebases from old-school FORTRAN coders... // 500 variables shared by everything main loop "task" 1 "task" 2 "task" 3 ... "task" n So t121 is now supposed to only be used by task 3, but "t" as a prefix means it is temp. However, we don't want to change all the references. So just don't use it everyone else, please?
- MaxBarraclough 4y agoRight. Pure functions make it considerably easier to reason about code, even in the absence of concurrency. It's a shame D's pure modifier (to statically guarantee purity) isn't available in many other languages. [0] SPARK Ada has something similar in that functions must always be pure, forcing procedures to be used for impure functionality. [1] edit I think that's mistaken, SPARK appears to permit functions to read globals but not to modify them. edit John Carmack has written about this kind of thing. From [2]: > if a function only references a piece or two of global state, it is probably wise to consider passing it in as a variable. It would be kind of nice if C had a "functional" keyword to enforce no global references. [0] https://dlang.org/spec/function.html#pure-functions https://dlang.org/spec/function.html#pure-functions [1] https://learn.adacore.com/courses/intro-to-spark/chapters/01_Overview.html https://learn.adacore.com/courses/intro-to-spark/chapters/01... [2] https://cbarrete.com/carmack.html https://cbarrete.com/carmack.html