8

After working Mathematica for a couple weeks to write a larger program, beyond doing integrals, I am starting to gain an appreciation for the Wolfram Language. There are a couple issues that drive me up the wall, and I am looking for solutions.

It comes down to how mathematica scopes, causes lots of bugs. Most programming languages will not let you access a global variable from inside a function, without explicitly asking. This causes two bugs for me:

1)Sometimes I write a block of code, and realize it would be better as a procedure. Often this chunk of code will have global variables in it, because it started out global. When I stick it in a procedure, I need to ensure all the global variables are replaced with the name of an argument to a function. If I miss one, the code usually still runs, but incorrectly, wasting hours of time on long calculations. Most programming langauges would throw an error

2)I define a function, and later add or remove a parameter, the kernel will still remember the old version. If I forget to update every single instance of that function call, it will still work, but on an old version, wreaking havoc on my calcuation.

It there a Mathematica way to solve this problem? Block and Module would not do it, because you have to define all the symbols that are local. Perhaps a block, where you clear all global variables, except functions?

While would be cool is if I could toggle my scoping. For example, at that start of a program I could say EnableDraconianScoping[]. This would raise errors. Once my code it written, I could DisableDraconianScoping[] to reduce overhead that would probably show up doing this for all my functions.

This problem is one of the few reasons I have a hard considering Mathematica for serious projects.

Thanks!

Josh Kelly
  • 81
  • 1
  • 2
    "Most programming languages will not let you access a global variable from inside a function, without explicitly asking." - what? – ciao Jul 21 '15 at 07:52
  • 2
    You raise a good point. I guess we all develop our own workflows to adapt to this feature. Some combination of ClearAll, contexts, packages, fresh kernels or function definition strategy might prove effective in your development style. – image_doctor Jul 21 '15 at 08:07
  • 4
    Syntax coloring helps a bit here. You can set globals to have a bright color, and scoped variables a dull color. That way, the globals become prominent within a scoping construct. – J. M.'s missing motivation Jul 21 '15 at 08:16
  • 1
    Welcome to the world of dynamic interactive languages programming. One replaces the flexibility of coding by more emphasis on the programmer to use their eyes much more to catch errors. In strongly static compiled languages, the compiler can catch many errors or issue warnings, but then one loses the flexibility of quick interactions and the dynamic nature of programming. The problems you describe are not new to Mathematica. Any interactive language will have such problems (Maple, Matlab, Julia, Python, Ruby, Perl, etc...) – Nasser Jul 21 '15 at 08:23
  • What should trigger the error you are talking about? I don't know what exactly do you need to catch. p.s. closures are commonly used. Could you prepare a minimal (couple of lines) piece of code you want to modularize in next step and add this feature to? – Kuba Jul 21 '15 at 08:43
  • is a good point, but I don't think it's possible to restrict "variables" to only local ones ... because they are not variables really. They're symbols with all that implies (including no real distinction between functions, variables, or constants like Pi). But I agree, it's a pain point. I use the help of syntax highlighting to try to catch errors. For 2), just ClearAll the function before you redefine it.
  • – Szabolcs Jul 21 '15 at 10:22
  • @J. M. There does not seem to be an option for global variables with value assigned in Edit | Preferences | Appearance | Syntax Coloring. – Edmund Jul 22 '15 at 02:27
  • @Edmund, you're right. That's where you do a ClearAll["Global`*"] to make them show up (and also has the benefit of removing previous, possibly buggy, definitions). A less drastic approach is Clear[], if you've kept track of the symbols you need to clear. – J. M.'s missing motivation Jul 22 '15 at 02:39
  • 2
    Now, studying R I see your point, in R the variables are local in the function scope, so you don't have this problem. Unfortunately I don't think this is something solvable in MMA. – Murta Jul 26 '15 at 03:25