I promised to explain better the idea of variable roles I mentioned in the previous post about natural programming.
This is based on a finding by Finnish researcher Jorma Sajaniemi (published work), who discovered that 99% of variables in novice’s code can be categorized in to 11 different roles. These roles are variable uses every programmer will recognize: iterators, constants, flags and so on – although in role-terminology the names somewhat differ (steppers, fixed-values and one-way flags, for example). Mr. Sajaniemi has also found that these roles matches tacit knowledge in expert programmers – i.e. the 11 roles are also typically intuitively recognized by expert programmers even if it is not explicit or active knowledge.
Whether or not the same 11 variable roles are enough to categorize expert variable use seems harder to pin down, but there is a master’s thesis from their lab finding that they are sufficient. And, not surprisingly, in expert-written programs, the roles have a significantly different distribution. Judge for yourself, is this enough to describe your own variables?
|Fixed value||maxStringLength||A data item that does not get a new proper value after its initialization|
|Stepper||count||A data item stepping through a systematic, predictable succession of values|
|Most-recent holder||inputData||A data item holding the latest value encountered in going through a succession of unpredictable values, or simply the latest value obtained as input|
|Most-wanted holder||maximum||A data item holding the best or otherwise most appropriate value encountered so far|
|Gatherer||sum||A data item accumulating the effect of individual values|
|Follower||prev||A data item that gets its new value always from the old value of some other data item|
|One-way flag||errorsOccurred||A two-valued data item that cannot get its initial value once the value has been changed|
|Temporary||temp||A data item holding some value for a very short time only|
|Organizer||sortArray||A data structure storing elements that can be rearranged|
|Container||processQueue||A data structure storing elements that can be added and removed|
|Walker||currNode||A data item traversing in a data structure|
Sajaniemi’s aim with the research and role concept appears to be teaching programming. For Perl-programmers, it can be interesting to see an article about Teaching Python using Roles, which might tell on how interesting it is for teaching Perl. Otherwise, his research in variable roles seems to revolve around a Java world.
What I find most exciting with this is the approach to studying and extending programming. Instead of going the computer science route, it looks at how people program, identifies interesting patterns and puts forward numbers and testable hypotheses.
Now can this be used to actually extend programming and help expert programmers?
My first observation is that at least five of the variables seem to all play parts in very typical loop patterns, namely stepper, most-recent-holder, most-wanted-holder, gatherer and follower. If this is such a typical way of organizing code, perhaps language design can help this – or perhaps it already does in the immutable states and for-comprehensions of functional languages such as Scala, or the map, grep and higher-order functions in Perl. Or if nothing else it may explain why expert programmers often tend more towards those constructs (or ?).
But if we know these are the typical ways variables are used, how about implementing variable roles (instead of types) with special functionality that simplifies and enhances what they are used for: most-wanted-holders that triggers events, gatherers and followers with history, walkers with an implicit track, organizers and containers optimized for moving elements or not and so on.
But if that is a good idea is hard to tell. At least in Perl, some things can be patched on with a little magical module, so it would be simple to test. I’m playing around with it, and I’ll keep you updated if something meaningful comes out of it. If you know of any other language that implements something similar, please leave a comment!