brain’s natural cognitive processes to • Read code more easily • Write code faster • Pick up new languages in much less time https://www.manning.com/books/the-programmers-brain Released on September 7, 2021 Dr Felienne Hermans ü Associate professor at the Leiden Institute of Advanced Computer Science ü @felienne
code Learn programming syntax more quickly How to not forget things Read complex code On thinking about code Reaching a deeper understanding of code On writing code better Get better at naming things Avoiding bad code and cognitive load Getting better at solving complex problems On collaborating on Code Getting better at Handling interruptions How to onboard new developers
is key to being able to reason about and make changes to code. Often variables are hard to understand because most programmers do not have a good schema in their long-term memory to relate variables. Sajaniemi’s framework : with just 11 roles, you can describe almost all variables : (distinguished by Sajaniemi)
Resonance imaging (fMRI) about what code does in the brain Areas related to natural languages Many similarities between reading code and reading natural language Our ability to learn a natural language can be a predictor of your ability to learn to program.
activate prior knowledge Determining importance Deciding what parts of a text are most relevant Visualizing Drawing diagrams of the read text to deepen understanding Inferring Filling in facts that are not explicitly given in the text Summarizing Creating a short summary of a text Questioning Asking questions about the text at hand Monitoring Keeping track of your understanding of a text
code elements will help your WM to find relevant information stored in the LTM Determining importance What matters is that you think about which parts of the code are likely to have the most influence on the program’s execution. Visualizing List all operations in which variables are involved (dependency graph, state table,…) Inferring Inferring the meaning of variable names Summarizing Writing a summary of code in natural language will help you gain a deeper understanding of what’s happening in that code Questioning Asking yourself questions while reading code will help you understand the code’s goals and functionality ex : What are the five most central concepts of the code? Monitoring Keep track of what you are reading and your understanding
of code by filling in the following table • You can add more information Questions Answer Goal of the code: what is the code trying to achieve? Most important lines of code Most relevant domain concepts Most relevant programming constructs …
your LTM to find relevant information that you already know about the domain of the code Bad names can lead you to make assumptions about the code leading to misconceptions.
of codebases Eclipse source code : tokens = 33%, characters = 72% Names play a role in code reviews Miltiadis Allamanis analyzed over 170 reviews with over 1000 remarks in them • 1 in four code reviews contained remarks related to naming • Remarks about identifier names occurred in 9% of all code reviews. Names are the more accessible form of documentation Serve as an important type of documentation : right inside the codebase Names can serve as beacons Help readers make sense of code Many different researchers have tried to define what makes a variable name good or bad
If the same word is used for similar objects across a codebase • It will be easier for the brain to find relevant related information stored in the LTM • Demonstrated by Allamanis work on code reviews
you understand them Concept like a tree will unlock info from LTM A tree has a root, can be traversed… Word like “customer” will have all sorts of associations (name, addresses, …) Variable named j will remind you of a nested loop
research – 72 professional C# devs Participants found on average 19% more defects per minute when reading programs in which the identifiers were words -> compared to letters and abbreviations Snake case or camel Case ? • Use of camelCase leads to higher accuracy among both programmers and non-programmers • 51.5% higher chance of selecting the right option for identifiers written in the camelCase style.
opposite than they say Complete list here https://veneraarnaoudova.com/linguistic-anti-pattern-detector-lapd/LAs/ Methods that do more than they say Methods that say more than they do Identifiers whose name says that they contain more than what the entity contains Identifiers whose name says that they contain less than what the entity contains Identifiers whose name says that the opposite than the entity contains
in 7 open-source projects : • 11% of setters also return a value in addition to setting a field • 2.5% of methods -> method name and the corresponding comment gave opposite descriptions of the working of the method • 64% of identifiers starting with ‘is’ turned out not to be Boolean Complete list here https://veneraarnaoudova.com/linguistic-anti-pattern-detector-lapd/LAs/ We can use tools like Archunit to prevent those
retrieveElements() { return products.last(); } What do you think when you read this ? • Think of information on functions returning a list of things • Gives you the idea you could sort, filter, or slice the returning element isValid What do you think when you read this ? Lead to mischunking • Assume the variable is a Boolean • No need for your brain to dig deeper • By trying to save energy, your brain has made a wrong assumption
something ex : How to run a bike Memories we are explicitly aware of (facts we can remember) Things we have experienced (Memories of experience) ex : meeting our wife / husband Facts we know (Memory for meanings, concepts, facts) ex : 10 x 10 = 100
problems ? Experts heavily rely on episodic memory when solving problems Instead of finding a new solution, they rely on solutions that have previously worked for similar problems.
skills is key to being able to solve larger and more complex problems. Automatization • Once you have practiced a skill so many times that you can do it without thinking about it • like walking, reading, or tying your shoelaces, -> You have automatized this skill When you learn something new / a new piece of information : • Needs to be split explicitly in smaller parts • You must explicitly think about the task at hand You need to actively repeat the new information until patterns of response emerge. Skill is perfected -> skill automatized
memories) Example : when you are struggling with creating for loops without errors • Deliberately typing 100 for loops • Building these small skills will help you to solve larger problems with greater ease • Just like with flashcards, spaced repetition is key to learning Use deliberate practice to improve skills • Use very small tasks and execute them repeatedly, until you have reached perfection • It frees up cognitive load for larger problems Deliberate practice : While regular practice might include mindless repetitions, deliberate practice requires focused attention and is conducted with the specific goal of improving performance. Set some time aside every day to practice and continue until you can consistently perform the tasks without any effort
episodic memories) Deliberately study how others have solved problems • Study worked examples • Worked examples explain how to solve the problems in detail Split a group of children Group 2 : received the algebra equations + worked examples of the equations • Group 2 solved it 5 times faster • Also performed better on different problems For which calculations rules could be used which were present in the recipe Australian professor John Sweller : The case for case studies of programming problems Worked examples: are something like a recipe which describes in detail the steps that are needed to solve the problem.
episodic memories) Collaborate with a colleague Start a “code reading club” Can exchange code and its explanation, and learn from each other Explore github Choose repositories : • domain is at least a bit familiar to you • not too many unfamiliar words and domain concepts -> can focus on the programming itself Read books / blog post about source code
tasks or skills that you are using while programming. For each skill, examine at what level you have automatized the skill or task and write the results in the table below. These questions can help you to decide the level of your skills: • Do you need to spend explicit attention to the task at hand in isolation? That means you are still in the cognitive phase. • Can you do the task, but are you relying on tricks to help you perform the task? You are likely in the associative phase. • Can you perform the task with ease, while also thinking or other problems at the same time? You have reached the autonomous phase. Task or skill Cognitive Associative Autonomous
interrupts “Interrupts: Just a Minute Never Is” – Van Solingen Average programmer : just one uninterrupted 2-hours session / day “Resumption Strategies for Interrupted Programming Tasks”– Parnin
editing code after an interruption When interrupted during an edit of a method : only 10% of times programmers could resume their work in less than a minute
period in comprehension activities : • Spent on building a mental model of the code at hand • If parts of the model are stored apart from the code : can help quickly regain your mental model -> Comments can be an excellent location to leave notes about your mental model 3) Label subgoals Explicitly write down into what small steps a problem can be divided 2) Help your "Prospective memory” : • Put TODO comments in the part of the code • Remind you to complete or improve part of the code Parnin’s VS code plugin allowing to add TODO expiry date Prospective memory : memory of remembering to do something in the future. (related to planning / problem solving)
of new information at a newcomer • The amount of information is too much to process • Causing high cognitive load After the introduction, the senior developer asks the newcomer a question or gives the newcomer a task A typical story The newcomer often fails because of the high cognitive load Caused by a combination of a lack of relevant chunks for the domain and/or the programming language, and a lack of automatized skills relevant Curse of expertise : Once you have mastered a certain skill sufficiently, you will inevitably forget how hard it was to learn that skill or knowledge.
relevant information : • Prepare the onboarding process for newcomers by deeply understanding the relevant information that plays a role when working with the codebase • Separate domain learning from exploring code
code elements will help your WM to find relevant information stored in the LTM Determining importance What matters is that you think about which parts of the code are likely to have the most influence on the program’s execution. Visualizing List all operations in which variables are involved (dependency graph, state table,…) Inferring Inferring the meaning of variable names Summarizing Writing a summary of code in natural language will help you gain a deeper understanding of what’s happening in that code Questioning Asking yourself questions while reading code will help you understand the code’s goals and functionality ex : What are the five most central concepts of the code? Monitoring Keep track of what you are reading and your understanding
that you work on often. • Create two lists that could help newcomers: One list containing important domain concepts and their description One list with all important libraries, frameworks and programming concepts that the codebase uses. Domain concepts Programming concepts Concept Definition Concept/module/library Use / Definition