4 ms·
> And I think global variables should just be removed altogether, as many problems as they cause. Hi, I'm curious what your thinking is? Is it because global v
by bazookajoes 12y ago
> And I think global variables should just be removed altogether, as many problems as they cause.
Hi, I'm curious what your thinking is?
Is it because global variables make testing hard?
Is it because global variables are can not be optimized well?
Is it because of bugs in julia related to global variables?
I would like to use a language that doesn't have static variables, global variables, or static functions to make writing tests easier, but that makes it really hard to interface with C and the JVM. I often need functions like write() and System.out.print.
- Xcelerate 12y ago> Is it because global variables are can not be optimized well? Is it because of bugs in Julia related to global variables? Mainly this; it's incredibly easy to accidentally use global variables and swamp your whole program. Think about defining some constant "n" in the global space and forgetting to put "const" in front of it. Then any for loops that use "for i = 1:n" will reallocate n on every iteration. If it's a nested loop, you'll get huge accidental memory allocations (this particular problem [not mine] is one on Stack Overflow that took a while to debug). It's the same reason garbage collection is used; it's not strictly necessary but it prevents a lot of accidental bugs and makes programs easier to reason about. I think it would be the same with removing global variables. * Of course, you still want function names to be global, so perhaps define a namespace that you can dump immutable functions or constants into. Or if there's good reasons not to remove global variables, at least make them all const by default and add a "mutable" keyword (and maybe swap that in the REPL). This would at least remove most of the accidental bugs.