Rocky Mountain Ruby 2011

Cognitive Psychology and the Zen of Code

Cognitive Psychology and the Zen of Code

by Jay Zeschin

In the talk titled "Cognitive Psychology and the Zen of Code," Jay Zeschin discusses how cognitive psychology can enhance code readability and maintainability. He begins by referencing the quote from Abelson and Sussman: 'Programs must be written for people to read and only incidentally for machines to execute.' This principle emphasizes the importance of human readability over purely mechanical execution.

Zeschin highlights several key points throughout the presentation:

  • Memory and Cognitive Function: He explains the functions of memory, including encoding, storing, and retrieving data, and distinguishes between working memory and long-term memory. Working memory is limited but can be enhanced through strategies like chunking, which allows individuals to remember larger amounts of data by breaking it into smaller, manageable pieces.
  • Cognition and Attention: The speaker introduces the concept of attention in cognitive processes, differentiating between high-level conscious thought and automatic responses. He illustrates the 'cocktail party effect' as an example of selective attention and discusses the Stroop task to exemplify how attention manages conflicting information.
  • Heuristics in Decision Making: Zeschin discusses cognitive shortcuts, known as heuristics, which help individuals make quick decisions but can also lead to biases. Heuristics that rely on availability, representativeness, and framing can influence judgments and should be recognized to avoid faulty reasoning.
  • Pattern Recognition: The ability of the brain to recognize patterns plays a crucial role in decision making, allowing developers to draw on past experiences to inform current judgments.

Zeschin connects these cognitive concepts back to programming by arguing that developers should design code to be predictable and readable to minimize cognitive load. Key strategies include:

  • Minimizing Surprises: Ensuring code adheres to principles that reduce unexpected complexities, allowing readers to focus on conceptual understanding rather than syntax.
  • Encouraging Recognition over Recall: Modifying code structures to facilitate easy recognition, thereby aiding retention and comprehension.
  • Effective Communication in Code: Using language constructs thoughtfully to create clear expectations for code behavior, which enhances understanding.

In conclusion, Zeschin asserts that understanding cognitive processes enables developers to write code that engages readers more effectively. By designing code that is comprehensible and logically organized, one can optimize coding practices for better retention and ease of use. The main takeaway is that developers should prioritize writing code for human readers, leveraging cognitive psychology principles to enhance usability and maintainability.

00:00:08.960 Hello, everyone! My name is Jay Zeschin, and I am a developer and partner at a company down in Denver called Bit Theory. We are a small development shop focused on Ruby on Rails and mobile work. When I'm not working with Ruby and JavaScript, I spend a lot of time running. In fact, this is a picture of me from a couple of weeks ago after completing a half marathon. Normally, I’m not this blurry in real life—though I have been drinking, so that’s certainly possible!
00:00:21.920 Let's dive right into the topic at hand. The inspiration for this talk stems from a quote by Abelson and Sussman: 'Programs must be written for people to read and only incidentally for machines to execute.' How many folks here have read or even heard of the book 'Structure and Interpretation of Computer Programs'? That's great! For those who haven’t read it, it's an excellent book and, as of a couple of years ago, it’s totally free and available online. It's been the courseware for MIT's introductory computer science class for many years, offering fascinating insights into programming concepts suitable for beginners.
00:00:54.240 One of the notable quotes from that book emphasizes how programs should prioritize readability for humans. While runtime optimization is vital, there are various paths to optimize a program. Today, I would like to focus on the non-runtime aspects. More generically, I'm discussing metacognition, which involves learning about how cognition works. I'm particularly interested in how we can leverage this understanding to 'hack' our cognitive processes. How can we better control our cognition and grasp our own cognitive processes?
00:01:20.000 I want to start this discussion with memory, specifically examining how memory operates. Essentially, memory has three functions: encoding sensory data, storing that data, and retrieving it later. Interestingly, we are generally better at recognition than at arbitrary retrieval. For instance, think back to middle school and those dreaded free-response tests, which could be a nightmare if you hadn't studied. In contrast, multiple-choice tests are much easier to navigate because our brains are wired for recognition, not recall.
00:02:11.040 One particular type of memory that is significantly useful is working memory, also called short-term memory. Working memory varies in theory but generally includes an executive component, a buffer for sensory information, and a visual-spatial sketchpad where you can manipulate images in your mind. Importantly, the capacity of working memory is limited to about seven plus or minus two items, and this capacity is consistent across individuals.
00:02:50.000 You can stretch this limit a little with strategies such as chunking. For example, when trying to remember a phone number, you don't remember each digit individually; instead, you chunk it into parts like area code and the prefix. This way, you remember fewer items than the actual number of digits. Moreover, working memory is closely tied to our auditory system. Our brain encodes memories based on sounds, and the pronunciation of words can significantly affect recall, especially with longer names of countries.
00:03:18.559 A couple of interesting observations about working memory include the inefficiency of scanning through information. If you’re trying to recall if something is in your working memory, the brain processes this serially, going through each item one by one, and it costs time. Additionally, it takes about 38 milliseconds to scan each item, which may seem trivial, but it can accumulate significantly, especially relative to how quickly computers operate.
00:04:07.040 Now, let’s touch on long-term memory. Long-term memory is hierarchical and consists of different types, such as declarative and procedural memories. Declarative memory can be further divided into episodic memory, which is about personal experiences, and semantic memory, which consists of general knowledge. Factors like decay and interference can hinder memory retention over time, leading to forgetting information.
00:04:42.680 Moreover, visual storytelling is a powerful tool for memory retention. Both pictures and stories engage our memory on multiple levels, helping encode experiences and emotions along with the content, making it easier to retrieve.
00:05:34.800 After discussing memory, let’s shift our focus to cognition. How many of you have read 'Blink'? It's a compelling book regarding our subconscious decision-making processes. It discusses concepts like thin slicing, the ability to make judgments based on minimal information, which can often feel like drinking from a fire hose due to the overwhelming stimuli those judgments involve.
00:06:19.360 In cognitive processes, attention plays a crucial role, dividing tasks into two processes: high-level conscious thought (attention) and automated responses. High-level processing is when you consciously consider decisions, while automated processing happens seamlessly, without requiring focused thought.
00:06:41.760 This division of attention allows us to efficiently multitask. The phenomenon known as the 'cocktail party effect' illustrates this perfectly. Have you ever noticed how you can focus on one conversation at a party while still being aware of your name being called from across the room? This is an example of how selective attention enables us to filter and prioritize information.
00:07:31.200 We can also examine the Stroop task, where the color of words conflicts with their meaning, highlighting how attention manages conflicting information. These cognitive processes facilitate faster processing of information through categorization and schemata, allowing us to organize and retrieve knowledge more effortlessly.
00:08:09.520 Heuristics also play an essential role in how we make decisions, often relying on cognitive shortcuts that can lead to biases or errors. Examples of heuristics include relying on availability, representativeness, and the framing effect. Each of these shortcuts helps us navigate complex decisions quickly but can lead to faulty reasoning if not recognized.
00:09:09.120 Pattern recognition is another critical function of cognition. Our brains excel at recognizing patterns and can quickly assemble templates from data. Understanding how our brains recognize patterns allows us to make swift judgments based on experiences we’ve previously encountered.
00:10:09.760 Now that we've explored various cognitive psychology concepts, it’s important to connect these insights back to programming and code. Going back to the Abelson and Sussman quote, effective use of cognitive resources by optimizing code for readability is crucial. You want readers to maximize their cognitive processing on the critical elements of your code rather than be bogged down by trivialities.
00:10:46.080 To achieve this, it's essential to minimize surprises in your code, adhere to the principle of least surprise, and avoid complexities that require more cognitive load. By reducing the burden on working memory, you can ensure readers spend their cognitive resources on high-level conceptual issues instead of mundane syntax or stylistic choices.
00:11:30.880 You should also encourage recognition over recall by implementing methods that let programmers recognize what they’re working with easily, rather than forcing them to remember it. Telling a story with your code can strengthen understanding and aid retention. This is where using helpful visual elements can resonate with readers.
00:12:10.960 Being aware of nuances in language constructs, like the difference between 'if' and 'unless,' ensures you’re framing conditions effectively and leading readers toward clear expectations. Ultimately, knowing your audience is paramount. Code is not just written for machines; it's created for people who maintain it and build upon it.
00:12:51.680 To conclude, your brain is adept at taking shortcuts, so don’t resist this ability. Design your code to make it predictable, readable, and understandable, allowing others to engage with it effectively. Thank you for your time, and I hope these insights will help you optimize your coding practices!