Slide 1

Slide 1 text

@yot88 The programmer’s brain be better at code

Slide 2

Slide 2 text

Spaced repetition https://miro.com/app/board/o9J_l0DSaRQ=/ • Here are some concepts : Cognitive Load Chunking Long Term Memory Flashcards Elaboration • Who wants to pick one and explain it ?

Slide 3

Slide 3 text

Spaced repetition

Slide 4

Slide 4 text

A word on the book Learn how to optimize your 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

Slide 5

Slide 5 text

plan How to read code better ? Speed reading for 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

Slide 6

Slide 6 text

@yot88 On thinking about code

Slide 7

Slide 7 text

@yot88 On thinking about code Reaching a deeper understanding of code

Slide 8

Slide 8 text

Roles of variables Understanding what types of information variables hold 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)

Slide 9

Slide 9 text

Roles of variables Decision tree for variable role Create a set of icons Use them to mark the roles of variables in unfamiliar code.

Slide 10

Slide 10 text

Reading text is like reading code Evidence from Functional Magnetic 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.

Slide 11

Slide 11 text

Text comprehension strategies Activating Actively thinking of related things to 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

Slide 12

Slide 12 text

Text comprehension strategies applied to code Activating Actively thinking about 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

Slide 13

Slide 13 text

@yot88 Summarize code

Slide 14

Slide 14 text

Text comprehension strategies applied to code • Summarize a piece 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 …

Slide 15

Slide 15 text

@yot88 On writing code better

Slide 16

Slide 16 text

@yot88 writing code better get better at naming things

Slide 17

Slide 17 text

get better at naming things Good names help to activate 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.

Slide 18

Slide 18 text

why identifier names matter Names make up a large part 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

Slide 19

Slide 19 text

Perspective 1 : A good name can be defined syntactically Simon Butler, associate Senior Lecturer at the Open University in the UK, created a list of issues with variable names :

Slide 20

Slide 20 text

Perspective 2 : Names should be consistent in a codebase 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

Slide 21

Slide 21 text

Clear names help your LTM

Slide 22

Slide 22 text

Variable names can contain different types of information to help 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

Slide 23

Slide 23 text

Quick tips To abbreviate or not to abbreviate ? Hofmeister 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.

Slide 24

Slide 24 text

@yot88 writing code better Avoiding bad code and cognitive load

Slide 25

Slide 25 text

Code smells create a lot of cognitive load Code smells are parts of code that suffer from structural anti-patterns: the code is correct, but not structured in a way that is easy to process.

Slide 26

Slide 26 text

Arnaoudova’s six categories of linguistic anti-patterns Methods that do the 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

Slide 27

Slide 27 text

Arnaoudova’s six categories of linguistic anti-patterns She studied their occurrence 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

Slide 28

Slide 28 text

linguistic anti-patterns cause confusion var elements = retrieveElements(); private Product 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

Slide 29

Slide 29 text

@yot88 writing code better getting better at solving complex problems

Slide 30

Slide 30 text

Problem solving Traversing the state space in the optimal way Reaching the goal state in as few steps as possible What is it ? How to ? 1. Understanding the problem 2. Devising a plan 3. Carrying out the plan

Slide 31

Slide 31 text

LTM can store different types of memory How to do 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

Slide 32

Slide 32 text

What types of memories play a role when you solve 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.

Slide 33

Slide 33 text

Technique 1 : Automatization (create implicit memories) Automatization of programming 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

Slide 34

Slide 34 text

Technique 1 : Automatization – improve implicit memories (create implicit 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

Slide 35

Slide 35 text

Technique 2 : Learning from code and its explanation (create 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.

Slide 36

Slide 36 text

Technique 2 : Learning from code and its explanation (create 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

Slide 37

Slide 37 text

@yot88 Automatize what ?

Slide 38

Slide 38 text

Automatization Start a new programming session while thinking about the 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

Slide 39

Slide 39 text

@yot88 On collaborating on Code

Slide 40

Slide 40 text

@yot88 On collaborating on Code getting better at Handling interruptions

Slide 41

Slide 41 text

Interruption… 20 percent of a developer’s time is spent on 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

Slide 42

Slide 42 text

What happens after an interruption? Takes about 15’ to start 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

Slide 43

Slide 43 text

prepare for interruptions better 1) Nakagawa’s results showed a warm-up 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)

Slide 44

Slide 44 text

@yot88 On collaborating on Code How to onboard new developers

Slide 45

Slide 45 text

Issues in the onboarding process A senior developer throws lots 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.

Slide 46

Slide 46 text

Improve it - Limit tasks to one programming activity During an onboarding process, it is best to specifically choose activities in each of the five categories and have newcomers do them one by one.

Slide 47

Slide 47 text

Improve it - Support the LTM of the newcomer Explain 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

Slide 48

Slide 48 text

Improve it – read code together Activating Actively thinking about 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

Slide 49

Slide 49 text

@yot88 Separate domain learning from exploring code

Slide 50

Slide 50 text

Separate domain learning from exploring code • Choose a project 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

Slide 51

Slide 51 text

https://bit.ly/2WMtn02