Share E-Book

The Programmers Brain What every programmer needs to know about cognition (Felienne Hermans) (z-library.sk, 1lib.sk, z-lib.sk)

Author

Rating 4.0 AI initial rating

Log in to rate

Programming
Language English

Your brain responds in a predictable way when it encounters new or difficult tasks. This unique book teaches you concrete techniques rooted in cognitive science that will improve the way you learn and think about code. In The Programmer’s Brain: What every programmer needs to know about cognition you will learn: • Fast and effective ways to master new programming languages • Speed reading skills to quickly comprehend new code • Techniques to unravel the meaning of complex code • Ways to learn new syntax and keep it memorized • Writing code that is easy for others to read • Picking the right names for your variables • Making your codebase more understandable to newcomers • Onboarding new developers to your team Learn how to optimize your brain’s natural cognitive processes to read code more easily, write code faster, and pick up new languages in much less time. This book will help you through the confusion you feel when faced with strange and complex code, and explain a codebase in ways that can make a new team member productive in days! About the technology Take advantage of your brain’s natural processes to be a better programmer. Techniques based in cognitive science make it possible to learn new languages faster, improve productivity, reduce the need for code rewrites, and more. This unique book will help you achieve these gains. About the book The Programmer’s Brain unlocks the way we think about code. It offers scientifically sound techniques that can radically improve the way you master new technology, comprehend code, and memorize syntax. You’ll learn how to benefit from productive struggle and turn confusion into a learning tool. Along the way, you’ll discover how to create study resources as you become an expert at teaching yourself and bringing new colleagues up to speed. What's inside • Understand how your brain sees code • Speed reading skills to learn code quickly • Techniques to unravel complex code • Tips for making codebases understandable About the

Format PDF
Size 9.6 MB
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
M A N N I N G Felienne Hermans Foreword by Jon Skeet What every programmer needs to know about cognition
Page 2
Information Filter Short-term memory Long-term memory Working memory 1 2 3 4 An overview of the three cognitive processes that this book covers: STM, LTM, and working memory. The arrows labeled 1 represent information coming into your brain. The arrows labeled 2 indicate the information that proceeds into your STM. Arrow 3 represents information traveling from the STM into the working memory, where it’s combined with information from the LTM (arrow 4). Working memory is where the information is processed while you think about it.
Page 3
The Programmer’s Brain
Page 4
(This page has no text content)
Page 5
The Programmer’s Brain WHAT EVERY PROGRAMMER NEEDS TO KNOW ABOUT COGNITION FELIENNE HERMANS FOREWORD BY JON SKEET M A N N I N G SHELTER ISLAND
Page 6
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2021 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from an usage of the information herein. Manning Publications Co. Development editor: Tricia Louvar 20 Baldwin Road Technical development editor: Jerry Kuch PO Box 761 Review editor: Mihaela Batinić Shelter Island, NY 11964 Production editor: Keri Hales Copy editor: Michele Mitchell Proofreader: Melody Dolab Technical proofreader: Sébastien Portebois Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617298677 Printed in the United States of America
Page 7
v brief contents PART 1 ON READING CODE BETTER.................................................1 1 ■ Decoding your confusion while coding 3 2 ■ Speed reading for code 13 3 ■ How to learn programming syntax quickly 33 4 ■ How to read complex code 46 PART 2 ON THINKING ABOUT CODE..............................................65 5 ■ Reaching a deeper understanding of code 67 6 ■ Getting better at solving programming problems 91 7 ■ Misconceptions: Bugs in thinking 110 PART 3 ON WRITING BETTER CODE.............................................125 8 ■ How to get better at naming things 127 9 ■ Avoiding bad code and cognitive load: Two frameworks 147 10 ■ Getting better at solving complex problems 160
Page 8
BRIEF CONTENTSvi PART 4 ON COLLABORATING ON CODE ........................................177 11 ■ The act of writing code 179 12 ■ Designing and improving larger systems 191 13 ■ How to onboard new developers 205
Page 9
vii contents foreword xiii preface xv acknowledgments xvii about this book xix about the author xxii about the cover illustration xxiii PART 1 ON READING CODE BETTER.......................................1 1 Decoding your confusion while coding 3 1.1 Different kinds of confusion in code 4 Confusion type 1: Lack of knowledge 5 ■ Confusion type 2: Lack of information 5 ■ Confusion type 3: Lack of processing power 6 1.2 Different cognitive processes that affect coding 6 LTM and programming 7 ■ STM and programming 7 Working memory and programming 8 1.3 Cognitive processes in collaboration 9 A brief dissection of how the cognitive processes interacted 9 Cognitive processes regarding programming tasks 10
Page 10
CONTENTSviii 2 Speed reading for code 13 2.1 Quickly reading code 14 What just happened in your brain? 15 ■ Reexamine your reproduction 16 ■ Reexamining your second attempt at reproducing code 17 ■ Why is reading unfamiliar code hard? 18 2.2 Overcoming size limits in your memory 18 The power of chunking 19 ■ Expert programmers can remember code better than beginners 22 2.3 You see more code than you can read 23 Iconic memory 23 ■ It’s not what you remember; it’s the way you remember it 25 ■ Practice chunking 30 3 How to learn programming syntax quickly 33 3.1 Tips for remembering syntax 34 Disruptions play havoc with your workflow 34 3.2 How to learn syntax quickly with flashcards 35 When to use the flashcards 36 ■ Expanding the set of flashcards 36 ■ Thinning the set of flashcards 36 3.3 How to not forget things 37 Why do we forget memories? 38 ■ Spaced repetition 39 3.4 How to remember syntax longer 40 Two forms of remembering information 40 ■ Just seeing information is not enough 41 ■ Remembering information strengthens memories 42 ■ Strengthen memories by actively thinking 42 4 How to read complex code 46 4.1 Why it’s hard to understand complex code 47 What’s the difference between working memory and STM? 48 Types of cognitive load as they relate to programming 49 4.2 Techniques to reduce cognitive load 51 Refactoring 51 ■ Replacing unfamiliar language constructs 52 Code synonyms are great additions to a flashcard deck 55 4.3 Memory aids to use when your working memory is overloaded 56 Creating a dependency graph 56 ■ Using a state table 59 Combining dependency graphs and state tables 61
Page 11
CONTENTS ix PART 2 ON THINKING ABOUT CODE ....................................65 5 Reaching a deeper understanding of code 67 5.1 Roles of variables framework 68 Different variables do different things 68 ■ Eleven roles to cover almost all variables 69 5.2 Roles and paradigms 71 Benefits of roles 72 ■ Hungarian notation 73 5.3 Gaining a deeper knowledge of programs 75 Text knowledge vs. plan knowledge 75 ■ Different stages of program understanding 76 5.4 Reading text is similar to reading code 79 What happens in the brain when we read code? 79 ■ If you can learn French, you can learn Python 81 5.5 Text comprehension strategies applied to code 84 Activating prior knowledge 84 ■ Monitoring 85 ■ Determining the importance of different lines of code 86 ■ Inferring the meaning of variable names 87 ■ Visualizing 88 ■ Questioning 89 Summarizing code 90 6 Getting better at solving programming problems 91 6.1 Using models to think about code 92 The benefits of using models 92 6.2 Mental models 94 Examining mental models in detail 96 ■ Learning new mental models 97 ■ How to use mental models efficiently when thinking about code 97 6.3 Notional machines 102 What is a notional machine? 103 ■ Examples of notional machines 103 ■ Different levels of notional machines 105 6.4 Notional machines and language 106 Expanding sets of notional machines 106 ■ Different notional machines can create conflicting mental models 107 6.5 Notional machines and schemata 108 Why schemata matters 108 ■ Are notional machines semantics? 109
Page 12
CONTENTSx 7 Misconceptions: Bugs in thinking 110 7.1 Why learning a second programming language is easier than learning the first one 111 How to increase the chances of benefiting from existing programming knowledge 113 ■ Different forms of transfer 114 ■ Already knowing something: Curse or blessing? 115 ■ The difficulties of transfer 116 7.2 Misconceptions: Bugs in thinking 117 Debugging misconceptions with conceptual change 118 Suppressing misconceptions 119 ■ Misconceptions about programming languages 120 ■ Preventing misconceptions while learning a new programming language 122 ■ Diagnosing misconceptions in a new codebase 122 PART 3 ON WRITING BETTER CODE ...................................125 8 How to get better at naming things 127 8.1 Why naming matters 128 Why naming matters 129 ■ Different perspectives on naming 129 ■ Initial naming practices have a lasting impact 131 8.2 Cognitive aspects of naming 133 Formatting names supports your STM 133 ■ Clear names help your LTM 134 ■ Variable names can contain different types of information to help you understand them 135 ■ When to evaluate the quality of names 136 8.3 What types of names are easier to understand? 137 To abbreviate or not to abbreviate? 137 ■ Snake case or camel case? 140 8.4 The influence of names on bugs 141 Code with bad names has more bugs 141 8.5 How to choose better names 142 Name molds 142 ■ Feitelson’s three-step model for better variable names 145 9 Avoiding bad code and cognitive load: Two frameworks 147 9.1 Why code with code smells creates a lot of cognitive load 148 A brief intro to code smells 148 ■ How code smells harm cognition 151
Page 13
CONTENTS xi 9.2 The influence of bad names on cognitive load 153 Linguistic antipatterns 154 ■ Measuring cognitive load 155 Linguistic antipatterns and cognitive load 158 ■ Why linguistic antipatterns cause confusion 159 10 Getting better at solving complex problems 160 10.1 What is problem solving? 161 Elements of problem solving 161 ■ State space 161 10.2 What is the role of the LTM when you solve programming problems? 162 Is problem solving a cognitive process on its own? 162 ■ How to teach your LTM to solve problems 164 ■ Two types of memories that play a role in problem solving 164 10.3 Automatization: Creating implicit memories 167 Implicit memories over time 168 ■ Why automatization will make you program quicker 170 ■ Improving implicit memories 171 10.4 Learning from code and its explanation 172 A new type of cognitive load: Germane load 173 ■ Using worked examples in your working life 175 PART 4 ON COLLABORATING ON CODE ..............................177 11 The act of writing code 179 11.1 Different activities while programming 180 Searching 180 ■ Comprehension 181 ■ Transcription 181 Incrementation 181 ■ Exploration 182 ■ What about debugging? 182 11.2 Programmer interrupted 183 Programming tasks require a warm-up 183 ■ What happens after an interruption? 184 ■ How to better prepare for interruptions 185 When to interrupt a programmer 187 ■ Some thoughts on multitasking 189 12 Designing and improving larger systems 191 12.1 Examining the properties of codebases 192 Cognitive dimensions 192 ■ Using CDCB to improve your codebase 200 ■ Design maneuvers and their trade-offs 201 12.2 Dimensions and activities 202 Impact of dimensions on different activities 202 ■ Optimizing your codebase for expected activities 204
Page 14
CONTENTSxii 13 How to onboard new developers 205 13.1 Issues in the onboarding process 206 13.2 Differences between experts and novices 207 Beginners’ behavior in more depth 207 ■ Difference between seeing concepts concretely and abstractly 211 13.3 Activities for a better onboarding process 213 Limit tasks to one programming activity 213 ■ Support the memory of the onboardee 214 ■ Read code together 216 13.4 Some words to close this book 221 epilogue 221 index 223
Page 15
xiii foreword I’ve spent a lot of my life thinking about programming, and if you’re reading this book you probably have too. I haven’t spent nearly as much time thinking about thinking, though. The concept of our thought processes and how we interact with code as humans has been important to me, but there has been no scientific study behind it. Let me give you three examples. I’m the main contributor to a .NET project called Noda Time, providing an alter- native set of date and time types to the ones built into .NET. It’s been a great environ- ment for me to put time into API design, particularly with respect to naming. Having seen the problems caused by names that make it sound like they change an existing value, but actually return a new value, I’ve tried to use names that make buggy code sound wrong when you read it. For example, the LocalDate type has a PlusDays method rather than AddDays. I’m hoping that this code looks wrong to most C# developers date.PlusDays(1); whereas this looks more reasonable: tomorrow = today.PlusDays(1); Compare that with the AddDays method in the .NET DateTime type: date.AddDays(1);
Page 16
FOREWORDxiv That looks like it’s just modifying date, and isn’t a bug, even though it’s just as incor- rect as the first example. The second example is also from Noda Time, but it’s not quite as specific. Whereas many libraries try (for good reason) to do all the hard work without developers having to think much, we explicitly want the users of Noda Time to put a lot of thought into their date and time code up front. We try to force users to put thought into what they’re really trying to achieve, with no ambiguity—and then we try to make it easy to express that clearly in the code. Finally, there’s the one conceptual example of what values variables hold in Java and C# and what happens when you pass an argument to a method. It feels like I’ve been trying to counter the notion that objects are passed by reference in Java for most of my life, and when I do the math, that’s probably the case. I suspect I’ve been trying to help other developers fine-tune their mental models for about 25 years now. It turns out that how programmers think has been important to me for a long time, but with no science behind it, just guesswork and hard-won experience. This book helps to change that, although it isn’t quite the start of this process for me. I first came across Felienne Hermans at the NDC conference in Oslo in 2017 when she gave her presentation “Programming Is Writing Is Programming.” My reaction on Twitter says it all: “I need a long time to let it all sink in. But wow. Wow.” I’ve seen Feli- enne give this presentation (evolving over time, of course) at least three times now and have taken something new out of it each time. Finally there were some cognitive explanations for the things I had been trying to do—and also some surprises that chal- lenged me to tweak my approach. Alternating reactions of “Ah, that makes sense now!” and “Oh, I hadn’t thought of that!” have been the background rhythm when reading this book. Aside from some immediate practical suggestions such as using flashcards, I suspect the impact of the book will be more subtle. Maybe it’s a little more deliberation about when to put a blank line in code. Maybe it’s a change in the tasks we give to new members of the team, or even just a change in the timing of those tasks. Maybe it’s how we explain concepts on Stack Overflow. Whatever the impact, Felienne has provided a treasure chest of ideas to think about and to process in working memory and move to long-term memory—thinking about thinking is addictive! —JON SKEET STAFF DEVELOPER RELATIONS ENGINEER, GOOGLE
Page 17
xv preface When I started to teach children to program about 10 years ago, I quickly realized I didn’t have the faintest idea how people use their brains for anything, especially for programming. While I learned a lot about programming in university, no course in my computer science education had prepared me to think about thinking about programming. If you followed a computer science program like I did, or if you learned program- ming by yourself, you most likely did not learn about the cognitive functions of the brain. Therefore, you might also not know how to improve your brain to read and write code in a better way. I certainly did not, but while teaching kids to program, I realized I needed a deeper understanding of cognition. I then set out to learn more about how we think and how we learn. This book is the result of the last few years of me reading books, talking to people, and attending talks and conferences about learning and thinking. Understanding how your brain works is interesting in its own right, of course, but it also matters for programming. Programming is seen as one of the most demanding cognitive activities there is: you are both solving a problem in an abstract way and manipulating a program, which requires a level of attention that does not come natu- rally to most people. Missed a space? Error. Miscalculated where to start the array index- ing? Error. Misunderstood the precise workings of an already existing code? Error. There are so many ways in which you can shoot yourself in the foot while program- ming. As you will see in this book, many of the errors you make are rooted in cognitive issues. For example, missing a space might mean you have not mastered the program- ming language’s syntax in enough detail. Miscalculating where to index an array might
Page 18
PREFACExvi indicate you have wrong assumptions about the code. Misunderstanding existing code could be caused by a lack of skills in how to read code. The aim of this book is to first help you understand how your brain processes code. Understanding what your brain does when presented with new information will help you be a better programmer because professional programmers are faced with new information often. Once we know how code affects the brain, we will talk about meth- ods to improve your code processing skills.
Page 19
xvii acknowledgments I fully realize how tremendously lucky I am to be able to complete a book on a topic I love. There are so many things in my life that happened in the exact right way at the exact right moment, without which my life would have been a lot different, and this book certainly would have not been written. Dozens of small and large encounters with amazing people have all contributed to this book and to my career. I want to name a few very important ones. Marlies Aldewereld first put me on the path of programming and language learn- ing. Marileen Smit taught me enough psychology to write this book. Greg Wilson made the topic of programming education mainstream again. Peter Nabbe and Rob Hoogerwoord set world-class examples of how to be a great teacher. Stefan Hanen- berg gave me advice that shaped the trajectory of my research. Katja Mordaunt kick- started the first ever code reading club. Llewellyn Falco’s thinking on koans shaped my thinking on learning extensively. And Rico Huijbers is my beacon in any storm. In addition to these people, of course, I need to thank the people at Manning— Marjan Bace, Mike Stephens, Tricia Louvar, Bert Bates, Mihaela Batinić, Becky Rein- hart, Melissa Ice, Jennifer Houle, Paul Wells, Jerry Kuch, Rachel Head, Sébastien Porte- bois, Candace Gillhoolley, Chris Kaufmann, Matko Hrvatin, Ivan Martinović, Branko Latincic, and Andrej Hofšuster for taking on this vague book idea and making it into something readable and sensible. To all the reviewers: Adam Kaczmarek, Adriaan Beiertz, Alex Rios, Ariel Gamiño, Ben McNamara, Bill Mitchell, Billy O’Callaghan, Bruno Sonnino, Charles Lam, Claudia Maderthaner, Clifford Thurber, Daniela Zapata Riesco, Emanuele Origgi, George
Page 20
ACKNOWLEDGMENTSxviii Onofrei , George Thomas, Gilberto Taccari, Haim Raman, Jaume Lopez, Joseph Pere- nia, Kent Spillner, Kimberly Winston-Jackson, Manuel Gonzalez, Marcin Sęk, Mark Har- ris, Martin Knudsen, Mike Hewitson, Mike Taylor, Orlando Méndez Morales, Pedro Seromenho, Peter Morgan, Samantha Berk, Sebastian Felling, Sébastien Portebois, Simon Tschöke, Stefano Ongarello, Thomas Overby Hansen, Tim van Deurzen, Tuomo Kalliokoski, Unnikrishnan Kumar, Vasile Boris, Viktor Bek, Zachery Beyel, and Zhijun Liu, your suggestions helped make this a better book.
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay

Every 10 yuan is 1 year of VIP; the remainder counts as months (5 yuan is 5 months, 100 yuan is 10 years). Signed-in users are enabled immediately and both you and the admin get an email. Guests receive an ID and are sent to register.

← Back to List