4 How to read complex code

 

This chapter covers

  • Analyzing what happens when your working memory is overloaded by complex code
  • Comparing two different types of working memory overload when programming
  • Refactoring code for readability to compensate for an overloaded working memory
  • Creating a state table and a dependency graph to support your working memory when reading complex code

Chapter 1 introduced the different ways in which code can be confusing. We’ve seen that confusion can be caused by a lack of information, which must be acquired and stored in your STM, or by a lack of knowledge, which requires storing information in your LTM. This chapter covers the third source of confusion: a lack of processing power in the brain.

Sometimes the code you are reading is just too complex for you to fully understand. Because reading code is not an activity that most programmers practice often, you might find that you lack strategies to deal with reading code you do not understand. Common techniques such as “read it again” and “give up” are not helpful.

In the previous chapters, we covered techniques to help you read code better. In chapter 2 you learned about techniques for more effective chunking of code, and chapter 3 provided tips for storing more syntax knowledge in your LTM, which also aids in reading code. However, sometimes code is so complex that even with a lot of syntax knowledge and efficient chunking strategies it’s still too hard to process.

4.1 Why it’s hard to understand complex code

4.1.1 What’s the difference between working memory and STM?

4.1.2 Types of cognitive load as they relate to programming

4.2 Techniques to reduce cognitive load

4.2.1 Refactoring

4.2.2 Replacing unfamiliar language constructs

4.2.3 Code synonyms are great additions to a flashcard deck

4.3 Memory aids to use when your working memory is overloaded

4.3.1 Creating a dependency graph

4.3.2 Using a state table

4.3.3 Combining dependency graphs and state tables

Summary