Tuesday, March 12, 2013

Dynamic scope pattern

[Noticed this was unpublished so I'll just publish it now]

Recently I noticed some confusion about dynamic scoping on Reddit. Many of the comments were even bashing dynamic scoping as something one would never need. The fact of the matter is you probably use dynamic scoping already, you just do it by hand.

What are global variables?

To understand what dynamic scoping is and why you are using it now, we need to understand global variables. So what is a global variable? A global variable is an implicit parameter to one or more functions [1]. Rather then pass some variable to 20 different functions, most of which simply pass it on to the functions they call, you make a global variable.

From this definition we can see that OO programing also has "global" variables. They are just restricted to specific classes (class/static variables) or instances of a class (instance variables).

Global variables, while convenient, a double edged sword. The problem is that any of these functions could change the variable without leaving a trace. That is, given the following:

int important_variable = 100;
void main()

If the code crashes somewhere in second() or third() due to important_variable being changed, it is difficult to tell what changed it. If some function called by first() changed the variable then by the time one of the later functions crashes, the offender wont be on the stack anymore.

Dynamic scoping - global variables done right
One thing that would help with the above problem is if we could say that important_variable has a fixed value and can only be rebound within a specific context. This way, if our code crashes because important_variable was changed we can look at the back trace. The offending function will have to be in it. In C++ we might do something like this:

template class DynamicSet {
Value* _var;
Value _oldVal;

DynamicSet(Value* var, Value newValue) : _var(var), _oldVal(*var) {
*_var = newValue;

~DynamicSet() { *_var = _oldVal; }

void first()
DynamicSet(&important_variable, 9);
// ...

Here we take advantage of C++'s destructors to make sure the variable is put back as it was no matter how the current stack frame is exited. This is how dynamic scoping works.

Relationship between exception handling and dynamic variables

The reddit thread also mentioned exception handling. The interesting thing here is that exception handling can be implemented in terms of dynamic scoped variables (some languages do exactly that) and dynamic variables can be implemented with exceptions if you have restartable exceptions (I've seen a dynamic variable implementation in Squeak that worked like this).

[1] Well, it could be zero, but that wouldn't make much sense. :)


Anonymous said...

Heya just wanted to give you a quick heads up and let you know a few of the pictures
aren't loading properly. I'm not sure why but I
think its a linking issue. I've tried it in two different web browsers and both show the same outcome.

My web-site creative group

Warbo said...

You're correct that dynamic scope is better than global scope. However, they're both still unnecessary when we can use lexical scope.

Anonymous said...

I just like the valuable information you supply on your articles.
I will bookmark your blog and take a look at again right here regularly.
I am quite sure I will learn many new stuff
right right here! Good luck for the following!

Feel free to visit my homepage :: gearhost.com

Jason said...

Well, it's better to use lexical scope when you can, the issue is that it's not convenient to always use it. That's why people use global variables (keep in mind that object fields are global variables). What I'm saying is that in the cases where you feel the need for global variables, it would be better to use dynamic scoped variables instead.