Variables and the roles they play

September 9th, 2009 § 9 comments

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?

Role Example Informal definition
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

The list is from An introduction to the role of variables, but there is also a more extensive description available.

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!

Tagged , , , ,

§ 9 Responses to Variables and the roles they play"

  • fREW Schmidt says:

    I think you are 100% right about functional stuff with more seasoned programmers. I personally hate to make accumulator variables (Gatherer) which is why I tend to use join (or it’s more generic reduce) along with map a lot. The use of reduce and map I think typically will help me to avoid a number of those variable types.

  • Some Developer says:

    What about these?
    toggled – Alternates between a pair of values. Typically, but not necessarily, boolean.
    enumerated – Takes on specific values from a set of values.
    bit field – In C/C++ like languages… stores multiple pieces of information
    return value – the value to be returned from a function/method
    volatile – In C/C++ like languages… variables that can change unexpectedly via an interrupt or another thread.

  • Eric Normand says:

    Man, what I wouldn’t give to work on research like that!

    Something that comes to mind: if you look at the Common Lisp LOOP construct, it has many of these things built in.

    I think these could be very useful for teaching programming. They might also make programmers more productive if the language could statically ensure the variable was being used in the way it was declared. Perhaps, as in CL LOOP, the variable could be almost entirely removed and its functionality replaced by a more declarative construct.

  • admin says:

    Eric Normand: I’m glad you mention that. I was fiddling around making some modules for automating the variable roles, but realized that declarative style programming already does it in a smart way. If you look at my comment on the “Never mind the language, the programmer is what matters” about having programmer/algorithmic knowledge implemented in the language, this is probably one of the big things implicit in Lisp and family?

    And a far better solution than my idea of making scalars with a history :)

  • [...] human brains play in the process of computer programming.”  His September 9, 2009 post is Variables and the roles they play, which summarizes the work of Jorma Sajaniemi and provides links to various academic papers on the [...]

  • Thank you for a very interesting article. I’ve written a blog entry applying these ideas to my language of choice, SQR. I hope you’ll take a look and find it interesting.

  • admin says:

    Steven Alexander, I already have, actually. I added the link below. I´m glad my post inspired you!

  • [...] for example, is what things like the “variable roles” mentioned in a previous post try to encode. They find how experts understand and use variables, and try to teach that to [...]

  • [...] writing about variable roles a while back, I’ve been thinking a lot about the way I use variables. The post also got quite a lot of [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What's this?

You are currently reading Variables and the roles they play at Mats Stafseng Einarsen.

meta