Thursday, October 04, 2018

If you are using global variables you are doing it wrong

One of the hard things for students to learn is simplicity. Beginners tend to over complicate things. In part this is because they lack the full toolbox early in their learning. Another part though is that they tend to see new tools as the answer to problems they are not intended to handle. They really want to show that they can use the tool.

My students are learning about methods and I assigned a project that requires two specific methods be written. I recently had the following conversation.

Student: I may have 5 too many variables for this project.

Teacher: Yes, if you have 5 variables you have too many variables.

Student: I have many more than 5 variables. I have these up here for example [Student shows global variables]

Teacher: If you're using global variables you are doing it wrong.

[time passes]

Student: you are right. It is a lot simpler without global variables.

As part of our discussion of methods we talked about scope of variables. This introduced global variables for the first time. I suspect I did not discuss them enough so that is something I have to fix before I do this again next semester. But that aside, the worst thing is that the student missed one of the reasons we use methods – data hiding.

Students see things differently than experienced programmers see things. They also get excited about using new ideas which can get in the way of them using them appropriately.  “But it works” they say ignoring the fact that other solutions are better and that there are flaws in the way they are using the new tool.

This is one of the reasons I am skeptical of automatic grading programs that only compare inputs and outputs.  It’s not always whether or not your program generates the right answer but that you got there the right way. Helping students find the right path, or at least different paths, means reading their code.


Dai said...

This is such a lovely post that appreciates the unfolding of an individual learning. I do not think you need to review your teaching. I do think you're identifying the most important and yet challenging thing that CS teachers face; to read their learners code. It is time consuming and a skill that evades those like me that are untrained and self-taught and infinitely catching up. A comparison to sight reading music might be fair.

Garth said...

This is one of the drawbacks with Small Basic. All variable in SB are global. When the kids switch to Python or VB they have to rethink their approach. I really wish SB had a few more of the big kid features available.

Mike Zamansky said...

Great post.

Problems include:
1. Some "best practices" won't make sense to the kids until much later and not so "best practices" often make sense given what students know now.

2. Teachers and professors frequently push some "best practices" without
justification while ignoring or violating others.

As usual, this has gotten me thinking and I'll probably write up a follow up post in a day or two.

√ėyvind Jakobsson said...

I overtrain this by deliberately uaing methods in another class or object than the startup class. This way, there are no global variables and the students are forced/encouraged to use parameters.

I introduce cross object global variables like the heap later in the course.