Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The programmer's brain (2/2) - be better at code

Yoan
September 15, 2021

The programmer's brain (2/2) - be better at code

Workshop based on the book "The programmer's brain" from Felienne Hermans

Yoan

September 15, 2021
Tweet

More Decks by Yoan

Other Decks in Programming

Transcript

  1. 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 ?
  2. 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
  3. 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
  4. 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)
  5. Roles of variables Decision tree for variable role Create a

    set of icons Use them to mark the roles of variables in unfamiliar code.
  6. 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.
  7. 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
  8. 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
  9. 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 …
  10. 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.
  11. 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
  12. 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 :
  13. 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
  14. 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
  15. 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.
  16. 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.
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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.
  23. 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
  24. 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
  25. 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.
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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)
  31. 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.
  32. 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.
  33. 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
  34. 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
  35. 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