Chapter A

Learning Programming

The Science of Learning Programming

Get schooled

Subsections of Learning Programming

Introduction

You may not have given much thought to how you are learning to program. Very likely, you’ve just been engrossed in doing so. But understanding the learning process can help you improve how quickly, and how well you learn. As we tackle the subject of object-oriented programming, it will greatly benefit you to have a better grasp of exactly what’s going on upstairs. Especially as we embark on our first semester-long programming project. In the immortal words of Marty McFly, things are about to get heavy! 1

Doc Brown asking about heavy problems Doc Brown asking about heavy problems


  1. This is a reference to the classic 1985 film “Back to the Future,” in which Marty McFly (played by Michael J. Fox) travels back in time to save his scientist friend Doc Brown (played by Christopher Loyd). An advance warning: Many of my pop-culture references are going to be grounded in the 80’s and 90’s. You may be more familiar with the animated show “Rick and Morty,” which was inspired by Doc and Marty’s relationship in the film. ↩︎

Natural Born Programmers

There is a prevalent myth that some people are “natural born programmers” to whom programming comes easily. This is a dangerous idea, because of its corollary - the idea that “some people simply aren’t able to learn programming.” If you embrace these related ideas, you will find yourself wondering to which set you actually belong.

The truth is that in learning to program, we are learning to solve problems in a way that can be performed by a computing machine. Think back to your introductory computer science course - you may have been asked to give instructions to the professor or a peer to perform a task. Shuffling cards, for example, or making a peanut butter sandwich, as in the following video from the Darnit family:

The point of this exercise is to understand the exactitude or precision with which programs must be written. But it also reveals just how different people are from computers. The simple truth is that to become good programmers, we must learn to write programs by developing an understanding of how the computers work, as well as how to express instructions in a form they can use.

The rest of this chapter is devoted to understanding that learning process.

Mindsets

Carol Dweck is a researcher who has been developing a theory on “Mindsets.” She describes her research in the following Ted Talk:

The New Science of Learning

Terry Doyle and Todd Zakrajsek explore the implications of mindsets and other understandings of the learning process emerging from cognitive science in their book The New Science of Learning: How to Learn in Harmony with your Brain, which they specifically wrote for college-age learners. I would encourage you to read this book. But I’ll offer a brief summary here:

Sleep

Perhaps the most impactful thing you can do to improve your learning is to get high-quality sleep. It is during sleep that your brain consolidates the memories of the day (including what you were learning) into long-term storage. Insufficient sleep has a large negative impact on this process, resulting in lost or inaccessible memories.

The three stages of memory processing are encoding, storage, and retrieval. All three are affected in different ways by the amount of sleep you get. It is difficult to encode new learning when you are tired and unable to pay attention to the information. In fact, when you are sleep deprived, it becomes more difficult to learn new information the longer you are awake. Similarly, without the proper amount of sleep, storage of new memories will be disrupted. The third stage of memory processing is the recall phase (retrieval). During retrieval, the memory is accessed and re-edited. This is often the most important stage, as learned information is of limited value if it can't be recalled when needed, for example, for an exam. [...] Converging scientific evidence, from the molecular to the phenomenological, leaves little doubt that memory reprocessing "offline," that is, during sleep is an important component of how our memories are formed, shaped, and remembered.

Dolye and Zakrajsek, 2013, pp. 21-22

The NIH recommends 7.5 to 9 hours of sleep nightly. Individuals can vary greatly in their sleep needs, and may have different sleep patterns known as “chronotypes” which are the basis for stereotypes like ‘morning people’ or ’night owls’. It is important to understand your own sleep patterns to be able to adapt your study and sleep habits to maximize your learning effectiveness. Doyle and Zakrajsek offer advice to tailor your sleep and study patterns, as well as how to deal with ‘sleep debt’ - the learning penalty incurred when you were unable to get enough rest.

Info

Caffeine and alcohol, especially when consumed in large quantities, disrupt your ability to sleep effectively, and therefore your ability to learn.

Exercise

Exercise plays a surprisingly important role in learning. When you exercise - especially aerobic exercise (the kind that gets your heart rate up), your body releases extra neurochemicals and proteins that are used by the brain cells to communicate. This rush of extra resources improves your ability to learn new concepts and skills.

How Much Physical Exercise do you need? How Much Physical Exercise do you need?

In addition to getting this regular exercise, you should periodically get up from your computer and walk around, or even better, adopt a standing desk. The human body is not meant to sit for extended periods of time. Doing so can lead to blood pooling in the buttocks and thighs and increases dramatically your risk of heart attack and stroke.

The Senses

As memories are encoded, they draw upon all the senses involved in the experience. Research is starting to show that involving multiple senses in a learning activity can improve the learning. This is one reason I sprinkle these readings with images and videos. It is also one of the reasons I like using Cognitive Apprenticeship in my teaching. By showing you the code I am writing (seeing), and talking about what and why I am doing it (hearing), and having you write the same code (touching), we are engaging multiple senses for a stronger learning experience.

You can also leverage this in your own learning efforts. Creating concept maps connecting the ideas you are learning in this and other CS courses engages you visually with the learning, and also encourages your brain to recognize and utilize those connections to strengthen your reasoning about them. Annotating your textbooks engages you in both reading (seeing) and touching (writing), and helps you reflect more strongly on what you are studying. Try printing out the pages of this textbook as you read them, and make your own annotations in the margins and put it into a three-ring binder. It will improve your learning and give you a physical copy of the text when you’re done!

Patterns

The human brain is built to recognize and process patterns. Explicitly seeking patterns in what you’re doing can therefore help your learning. And programming is chock full of patterns! Consider if I asked you to write some code to add one to each element in an array. What would you do? Possibly a for loop like this:

for(int i = 0; i < array.Length; i++) {
    array[i] += 1;
}

But what if I asked you to halve the value instead? perhaps:

for(int i = 0; i < array.Length; i++) {
    array[i] /= 2;
}

Notice the parts that stay the same? We call this pattern iteration, and use it constantly. Moreover, the syntax of the for loop itself is a pattern. In fact, for loops were developed because of another pattern programmers found themselves using regularly:

int i = 0;
while(i < array.Length) {
    // DO SOMETHING WITH ARRAY
    i++;
}

Notice the three statements int i = 0;, i < array.Length, and i++;? These are the three parts of a for loop! When programmers found themselves using this pattern over and over again, they added new syntax to programming languages to simplify writing it1. In fact, the reason we use the semicolon to separate the three parts of a for loop is because they were originally separate statements in this while loop construction.

We also can apply this pattern recognition on similar, but unfamiliar formulations. Consider this code:

for(int i = array.Length-1; i >= 1; i--) {
    array[i] += array[i-1];
}

What does it do?

You likely noticed that it iterates through an array, but not forward through the array but rather backwards. And at each step we increase the value of an item by the value of the item before it. Could we rewrite this code as a forward iteration?

Memory

We already talked about the role of sleep in memory formation, but Doyle and Zakrajsek go deeper into how memories form and how that can be enhanced in the context of college life. For example, cognitive science has shown that parts of the brain responsible for memory formation remain active up to an hour after the learning experience (i.e. a lecture) has ended. So the common practice of scheduling your classes back-to-back may be impacting your ability to learn in the earlier classes!

Research has also shown that distributed practice is crucial to reinforcing and making available memories involved in skills as well as conceptual reasoning. The process of using the skill, or recalling the knowledge at regular intervals helps ‘settle’ the learning into our minds. In contrast, the practice of cramming may help us recall answers for an exam, but shortly after those answers will be lost to us, as the memories we formed were short-term. This is also why this class, as well as your math and physics courses emphasize solving problem after problem. It isn’t that we want to torture you - it’s that this continual practice is what helps you to learn the skills you’re developing.

Elaboration is another key to strengthening memory and recall. This involves adding more nuanced understandings to what you already know. For example, in this class we’re revisiting programming syntax that you’ve already learned, and I’ll be introducing new ideas and concepts that tie into that. Just like the example of how a for loop is nothing more than syntactic sugar for a specific while loop formulation. Do you need to know that? Probably not. But learning about it helps reinforce your understandings of how both kinds of loops operate, and that can make you a more proficient programmer.

Do you study while watching YouTube or conversing with friends? You might be interested to know that multitasking has a significant impact on memory formation:

When you shift task while working on something that requires thinking, such as texting your friend and listening to a lecture in class, your brain goes through a four-step process that allows you to switch your attention: (a) shift alert, (b) rule activation for task 1, (c) disengagement, (d) rule activation for task 2. This process is repeated every time you switch tasks that involve thinking, and you never get better or faster at it. You may have noticed that when you try to do two thinking tasks at the same time, you cannot complete both simultaneously, as the brain must shut down one task before working on the other. [...R]esearch demonstrates that individuals who shift tasks make 50% more errors and spend at least 50% more time on both tasks.

Dolye and Zakrajsek, 2013, p. 79

Stress also has an impact on memory formation - even minor stressful events can cause the release of hormones that disrupt the brain’s learning process. Exercise is the best counter to this issue, both helping repair the damage from stress and prevent it from occurring.


  1. We call this common practice of adding new syntax to a language to simplify writing commonly used patterns syntactic sugar, as it’s not strictly necessary, but makes the process of writing programs so much sweeter. ↩︎

Jean Piaget

Jean Piaget was a biologist and psychologist who performed some of the earliest studies on knowledge acquisition in children. His work is the foundation of Constructivism, one of the more influential philosophies of education.

Jean Piaget Jean Piaget

Genetic Epistemology

Of especial interest to us is his theory of genetic epistemology. Epistemology is the study of human knowledge, and genetic in this sense refers to origins i.e. the genesis, so his theory concerns how knowledge is created by humans.

Piaget’s genetic epistemology was inspired by studies he conducted of snails of the genus Lymnea native to the lakes of his home, Switzerland. He was able to establish that what had previously been considered different species of snails based on the shape of their shells were actually one species. He showed that when the snails of one lake were placed in a different lake, the way their shells grew changed to match those of the snails living in the second lake. In other words, the traits the snails displayed altered in response to their environment.

Examples of Lymnea Examples of Lymnea

Piaget suspected a similar mechanism was at work in human learning. He proposed that the human brain, much like the bodies of the snails, sought to exist in equilibrium with its environment. For the brain, this environment meant the ideas it was exposed to. He proposed two different mechanisms for learning, accommodation and assimilation, which related to how structures of knowledge were formed in the brain.

Assimilation referred to the process of adding new knowledge into existing mental structures. For example, once you know of and understand colors, you might encounter a new one - say periwinkle, which falls between blue and violet. Since you already know blue and violet, adding the knowledge of periwinkle is a straightforward task for the brain of assigning it to a slot between the two in the mental structure of ‘colors’.

In contrast, accommodation refers to the process by which knowledge for which you have no mental structures to represent are learned. This process involves building these mental structures, and is far more work. Modern cognitive science equates this process to the formation and reinforcement of new connections between neurons - a significant biological investment. Thus, triggering accommodation requires significant stimulus, in the form of wrestling with concepts that are currently beyond your grasp - a struggle that creates disequilibrium for your brain, eventually leading to it creating the new structure to accommodate the new knowledge.

This is the basis of the ’eureka’ moment, when a difficult concept has finally become clear. No doubt you have experienced this in a subject such as mathematics or programming, where a skill or idea you’ve been struggling with suddenly snaps into place, and becomes far easier to work with. This is also why your math and programming courses put so much emphasis on homework - this work helps create the disequilibrium you need to trigger accommodation. This is also why mindsets are so powerful - a fixed mindset provides a different mechanism for managing cognitive disequilibrium - by denying your ability to learn a subject, you provide justification for not engaging in the learning process.

Stage Theory

In addition to the mechanisms of accommodation and assimilation he outlined in his Genetic Epistemology theories, Piaget identified four stages children progress through as they learn to reason more abstractly. Those stages are:

  • Sensorimotor - where the learner uses their senses to interact with their surroundings. This is the hallmark of babies and toddlers who gaze wide-eyed at, touch, and taste the objects in their surroundings.
  • Preoperational - in this stage the learner begins to think symbolically, using words and pictures to represent objects and actions.
  • Concrete Operational - In this stage, the learner begins to think logically, but only about concrete events. Inductive logic - the ability to reason from specific information to a general principle also appears.
  • Formal Operational - This final stage marks the ability to grasp and work with abstractions, and is marked by hypothetico-deductive reasoning (i.e. formulating and testing hypotheses)

Neo-Piagetian Theory

While Piaget focused his study on children, many of the researchers who followed him also looked at how adults learn. Their findings suggest that all learners progress through the four stages with any new learning. That is, when you begin to learn a novel concept or skill, you are building the cognitive structures necessary to support it, and that your progress through this process corresponds to these four stages. Moreover, they have found that the divisions between stages are not rigid and clearly delineated; learners can exist in multiple stages at once (which they call the overlapping waves model).

The Overlapping Waves Model The Overlapping Waves Model

Developmental Epistemology of Computer Programming

Among these neo-Piagetian researchers is a group including Raymond Lister and Donna M. Teague whom applied these theories to the learning of computer science, formulating a theory Lister calls The Developmental Epistemology of Computer Programming. This theory describes the traits of programmers at each of the stages of development. In particular, they use a student’s ability to trace code (explain line-by-line what it does) as a demarcation between stages.

StageTraits
Sensorimotor
  • Cannot trace code with >= 50% accuracy
  • Dominant problem-solving strategy is trial and error
Preoperational
  • Can trace code with >= 50% accuracy
  • Traces without abstracting any meaning from the code
  • Cannot see relationships between lines of code
  • Struggles to make effective use of diagrammatic abstractions of code
  • Dominant problem-solving strategy is quasi-random code changes and copious trial runs
Concrete Operational
  • Dominant problem-solving strategy is hasty design, futile patching
  • Can establish purpose of code by working backwards from execution results
  • Tends to reduce levels of abstraction to make concepts more understandable
Formal Operational
  • Uses hypothetico-deductive reasoning
  • Reads code rather than traces to deduce purpose

These stages reflect the progress the learner is making through accommodation, creating the mental structures needed to reason about programming. An expert has developed these structures, which reflect patterns in how code is written - that is why an expert no longer traces code - they can see the patterns in the code and immediately grasp its action and purpose. In contrast, the novice must deduce the result of each line of code, put those understandings together, and then deduce what it is doing overall.

Writing a program is similar, the expert begins with a clear picture of the patterns she must employ, and focuses on fleshing those out, while a novice must create the program ‘from whole cloth’, reasoning out each step of the process. They are not yet capable of reasoning about the program they are writing in the abstract.

This also helps explain why learning to program can be so hard. Abstraction is considered a central tool in programming; we use abstractions constantly to simplify and make programs more understandable to other programmers. Consider a higher-level programing language, like C#. Each syntax element is an abstraction for a more complex machine-level process. The statement:

x += 2;

Stands in for machine instructions along the lines of:

PUSH REG5 TO REG1
PUSH 2 TO REG2
ADD REG1 AND REG2
PUSH REG3 TO REG5

Which are in turn, simplifications and abstractions of the actual process of adding the two binary values in register 1 and register 2 (remember studying binary math in CIS 115)?

Also, many of the productivity tools created to support expert programmers (i.e. automatic code completion) may actually hamper your learning, as they alleviate the need to carry out part of the process you are learning. Consider turning these features off in your development environment until you have developed fluency as a programmer.

Info

To turn off autocomplete in Visual Studio:

  • From Visual Studio, select “Tools” > “Options”.
  • Select “Text Editor” in the left pane.
  • Select the language you are using (C#, C++, Basic, etc.).
  • For C# and Basic, choose “IntelliSense”. For C or C++, choose “Advanced”, then scroll to the “IntelliSense” section.
  • For C# and Basic, check the “Show completion list after a character is typed” to disable it. For C/C++, you will have a few options, such as “Disable Auto Updating”, “Disable Squiggles”, and “Disable #include Auto Complete”. Set any of these to “True” to turn them off.

Summary

So what does all of this mean in the context of your learning?

  • Developing into an expert programmer is going to take hard work
  • It will require a lot of writing and reading code
  • There is no shortcut in this learning process, because you must create disequilibrium in your brain in order for the necessary cognitive structures to form through the process of accommodation
  • This process can be very frustrating
  • You can do this, just as many students before you have

What can you do to improve your learning process?

  • Get enough sleep, both in quantity and regularity
  • Exercise regularly
  • Engage with the readings and activities.
  • Take notes, annotate your text, draw concept diagrams
  • Don’t try to multitask while working on your classwork
  • Turn off autocompletion in your development environment
  • Don’t copy/paste code directly into your program. Instead, type it - it will take longer, but it will also give you the time to study each line and develop and understanding of what it is doing and how it interacts with the rest of the program.
  • Read programs written by other people, and try to understand what they are doing - GitHub is a great source of examples, and you can filter it by programming language