The December 2022 issue of IEEE Spectrum is here!

Close bar

What Exactly Is Your Brain Doing When Reading Code?

Mulling over a computer program is not like thinking in everyday language—but it's not pure logic either

3 min read
brain and computer code
Illustration: iStockphoto

Coding is becoming an increasingly vital skill. As more people learn how to code, neuroscientists are beginning to unlock the mystery behind what happens in people’s brains when they “think in code.”

“Computer programming is not an old skill, so we don’t have an innate module in the brain that does the processing for us,” says Anna Ivanova, a graduate student at MIT’s Department of Brain and Cognitive Sciences. “That means we have to use some of our existing neural systems to process code.”

Ivanova and her colleagues studied two brain systems that might be good candidates for processing code: The multiple demand system—which tends to be engaged in cognitively challenging tasks such as solving math problems or logical reasoning—and the language system. Despite the structural similarities between programming languages and natural languages, the researchers found that the brain does not engage the language system—it activates the multiple demand system.

The result was consistent regardless of programming language (the study focused on Python and ScratchJr, a visual programming language for children), problem type (math versus string manipulation), and code elements (sequential statements, if statements, and for loops). The study also found that the multiple demand system likely stores representations of code-relevant information, including common coding concepts (such as loops) and knowledge specific to a programming language (such as the syntax of a for loop in Java versus Python).

Yet coding and math and logic don’t rely on entirely the same brain mechanisms. “The multiple demand system includes regions in the left and right hemispheres,” Ivanova says. “For math and logic, we usually see more activity in the left hemisphere. For code, it activated the multiple demand system in both hemispheres, so the activation pattern is different from what we see for math and logic.”

[shortcode ieee-pullquote quote=""Maybe if you're extremely good, you will have some dedicated chunk of brain tissue for code. But you don't need it to be a good programmer."" float="left" expand=1]

Results from MIT’s research tie in with a similar study from Johns Hopkins University (JHU), where researchers found that the neural mechanisms for reading and understanding code overlap with logic, and to a lesser degree with math. But the JHU study indicates a stronger reliance on the left side of the brain’s multiple demand system. (Both the MIT and JHU studies were published in a recent issue of the journal eLife.) 

“When reading code, we may prefer to use the left version of this network,” says Yun-Fei Liu, a graduate student at JHU’s Neuroplasticity and Development Lab and lead author of the study. However, there might be an individual difference on which side to use, which Liu likens to handedness.

“Most people use their right hand to write, while some people use their left, and fewer people can use both hands. That might also be true for reading code,” Liu says. “Most people use the left logical reasoning system, and some use a bit of the right, but maybe some people use both. How much each individual relies on the left brain to read code is associated with how much they rely on the left brain to do linguistic tasks.” This suggests that language may still play a role in understanding code even if the brain doesn’t engage its language network.

While the MIT study didn’t identify any specialized region in the multiple demand system dedicated to interpreting code, Ivanova notes it’s possible to develop such a region as proficiency in programming grows. “Maybe if you’re extremely good, you will have some dedicated chunk of brain tissue for code. But you don’t need it to be a good programmer,” she says.

Both studies come with the limitation of having a small number of participants and using short code snippets. “These aren’t huge programming tasks where a lot of domain knowledge is involved—and you would implement complex business logic,” says Barbara Weber, a professor at the University of St. Gallen in Switzerland who also conducted a comprehensive review of studies on brain activity measurement in software engineering that was published in a recent issue of The Journal of Systems and Software. “We don’t know if the brain behaves exactly the same with a large code base having hundreds of lines of code.” Further studies may also be needed to determine if the brain behaves differently based on the type of coding task (front-end versus back-end or writing versus debugging code) and the programmer’s skill level.

According to Ivanova, the benefits of studying the cognitive and neural foundations of coding are twofold. “We’ll have a more scientific basis to inform our understanding of the most effective way to teach programming,” she says. “And we can leverage the broader domain of cognitive science to figure out how we can design programming languages that are adapted to the specifics of the human brain.”

The Conversation (0)

Why Functional Programming Should Be the Future of Software Development

It’s hard to learn, but your code will produce fewer nasty surprises

11 min read
Vertical
A plate of spaghetti made from code
Shira Inbar
DarkBlue1

You’d expectthe longest and most costly phase in the lifecycle of a software product to be the initial development of the system, when all those great features are first imagined and then created. In fact, the hardest part comes later, during the maintenance phase. That’s when programmers pay the price for the shortcuts they took during development.

So why did they take shortcuts? Maybe they didn’t realize that they were cutting any corners. Only when their code was deployed and exercised by a lot of users did its hidden flaws come to light. And maybe the developers were rushed. Time-to-market pressures would almost guarantee that their software will contain more bugs than it would otherwise.

Keep Reading ↓Show less
{"imageShortcodeIds":["31996907"]}