functions and processes such as: perception, attention, thought, imagination, intelligence, the formation of knowledge, memory and working memory, judgment and evaluation, reasoning and computation, problem-solving and decision- making, comprehension and production of language.” - Wikipedia
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
following program for 3 minutes Try to reproduce it as best as you can public class InsertionSort { public static void main(String[] args) { int[] array = {45, 12, 85, 32, 89, 39, 69, 44, 42, 1, 6, 8}; int temp; for (int i = 1; i < array.length; i++) { for (int j = i; j > 0; j--) { if (array[j] < array[j - 1]) { temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; } } } for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } }
your brain. 2) Information that proceeds into your STM 3) Information traveling from the STM into the working memory 4) Where it’s combined with information from the LTM Long Term Memory (LTM) • Can store your memories for a very long time • Hard drive of your brain Short Term Memory (STM) • Used to briefly hold incoming information • RAM or a cache that can be used to temporarily store values • Just a few items fit in STM (<12) Information Filter STM LTM WM Working Memory (WM) • The actual “thinking”, happens in working memory • Processor of the brain 1 2 3 4
LTM to retrieve knowledge (Keywords for example) Use STM to store some of the info we encounter Use working memory : trying to mentally execute the code / understand what is happening TRACING Information Filter STM LTM WM 1 2 4 3
STM Filter STM LTM WM 1 2 4 3 public class InsertionSort { public static void main(String[] args) { int[] array = {45, 12, 85, 32, 89, 39, 69, 44, 42, 1, 6, 8}; int temp; for (int i = 1; i < array.length; i++) { for (int j = i; j > 0; j--) { if (array[j] < array[j - 1]) { temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; } } } for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } } int[] array int temp 2 boucles public static void main(String[] args) for (int i = 1; i < array.length; i++)… public class InsertionSort { public static void main(String[] args) { int[] array = {45, 12, 85, 32, 89, 39, 69, 44, 42, 1, 6, 8}; int temp; for (int i = 1; i < array.length; i++) { for (int j = i; j > 0; j--) { if (array[j] < array[j - 1]) { temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; } } } for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } } Reproduced code The information extracted from our LTM depends on what we have stored in it. A person with less experience in Java is likely to extract much less information from their LTM.
that almost 60% of programmers’ time is spent understanding code, rather than writing code Reading code Writing code Improving how quickly we can read code (without losing accuracy) Can help us improve our programming skills substantially Reading code is done for a variety of reasons: • add a feature • find a bug • build an understanding of a larger system https://ieeexplore.ieee.org/abstract/document/7997917
limitation Cannot hold information for more than 30 seconds Short Term Memory Size limitation Just a few slots available for information 7 +/- 2 Some research indicates that its capacity could be smaller : just 2 to 6 things A memory issue
Groot first chess experiment Experts grouped info in logical ways : CHUNKS http://snitkof.com/cg156/chesschunkingtheory.php Fit into only 1 slot in STM
Katherine McKeithen, repeat de Groot’s experiments on programmers : Main takeaway : beginners will be able to process a lot less code than experts Nombre de lignes de code https://www.researchgate.net/publication/222462455_Knowledge_Organization_and_Skill_Differences_in_Computer_Programmers
first one! Because consists of letters that you recognize 2 sentences were the same length: 2 words, 14 characters, 10 different characters Look at this sentence for five seconds and try to remember it as best as you can: Reproduce it on paper
chunk the characters into words. You can then remember just two chunks: “swissquote”, “2024”. Like the chess experts Look at this sentence for five seconds and try to remember it as best as you can: Reproduce it on paper
problem When reading code, the working memory is only capable of processing 2 to 6 "things" at a time... In the context of working memory, this capacity is known as the cognitive load!!!
load Temporary refactoring (most of the time) to understand the code Example – replace unfamiliar linguistic constructions If you do not know the famous “functors”, “monads”, … What is easy to read depends on your prior knowledge No shame in helping yourself understand code by translating it to a more familiar form Automated refactorings
focuses on the values of variables rather than the structure of the code. It has columns for each variable and lines for each step in the code. How to ? Make a list of all the variables Create a table : 1 column / variable 1 line for each mutation Execute each part of the code
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
pipeline for the Pipeline Object Calisthenics : 1 level of indentation https://github.com/advent-of-craft/advent-of-craft/blob/main/solution/day07/docs/step-by-step.md
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
our code = NAMES (72%) Names play a role in code reviews 1 in four code reviews contained remarks related to naming… (Miltiadis Allamanis) Names are the more accessible form of documentation
more than they do Identifiers whose name says that the opposite than the entity contains Methods that do more than they say Methods that do the opposite than they say var elements = retrieveElements(); private Product retrieveElements() { return products.last(); } public Either<Error, PartieDeChasse> PrendreLapéro(Func<DateTime> timeProvider) { _chasseurs.ForEach(c => c.SortirDeLaPartie(this)); Status = Apéro; EmitEvent("Petit apéro", timeProvider); return this; } private bool Exists(string chasseur) { var found = false; foreach (var c in _chasseurs) { if (c.Nom == chasseur) { return found; } } return false; } var isValid = 42; Impossible to chunk correctly...
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
code (Patterns, naming, high level comments, …) Improve in code reading techniques Automate LAPs detection A scientific endorsement of the feeling we had with Clean Code