Slide 1

Slide 1 text

Reading Code Øredev - Nov 10, 2023 https://maritvandijk.com/

Slide 2

Slide 2 text

Reading Code • We spend more time reading code than writing code

Slide 3

Slide 3 text

Reading Code We read code when: • adding features • fi xing bugs • understanding a new system • doing code reviews • learning new technologies • etc.

Slide 4

Slide 4 text

Reading Code "developers on average spend as much as 58% of their time comprehending existing source code" - Felienne Hermans, The Programmer's Brain

Slide 5

Slide 5 text

Reading Code • We spend more time reading code than writing code • But we don't practice reading code explicitly (much)

Slide 6

Slide 6 text

Reading Code "How to teach programming (and other things)?" by Felienne Hermans https://www.youtube.com/watch?v=g1ib43q3uXQ

Slide 7

Slide 7 text

Reading Code • What if we practice reading code explicitly?

Slide 8

Slide 8 text

Code Reading Club • Code Reading Club https://codereading.club/

Slide 9

Slide 9 text

Code Reading Club • Printed code • Structured exercises

Slide 10

Slide 10 text

Examples of exercises • First glance • Identify components & relationships • Most important lines • Identify concepts • Summary

Slide 11

Slide 11 text

Code Reading Club Learned about ... • reading code • other peoples' perspectives • noticing my own assumptions

Slide 12

Slide 12 text

Reading Code

Slide 13

Slide 13 text

Reasons code is confusing 1. Lack of knowledge 2. Lack of information 3. Lack of processing power in the brain

Slide 14

Slide 14 text

Lack of knowledge • Programming domain • Language / syntax • Programming constructs / algorithms • Business domain • Domain concepts • Business logic

Slide 15

Slide 15 text

Dealing with a lack of knowledge • Create a list • Research • Memorize

Slide 16

Slide 16 text

Lack of information • Unknown details • Unfamiliar names • Code connected in unknown ways • Too many things at once

Slide 17

Slide 17 text

Dealing with lack of information • Chunking

Slide 18

Slide 18 text

Lack of processing power in the brain • Too many processing steps • Too many variables

Slide 19

Slide 19 text

Dealing with lack of processing power in the brain • Memory aids • Dependency graph • State table

Slide 20

Slide 20 text

Dependency graph

Slide 21

Slide 21 text

State table public class NestedLoop { public static void main(String[] args) { for (int i = 0; i < 2; i++) { System.out.println("i is: " + i); for (int j = 0; j < 2; j++) { System.out.println("j is: " + j); } } } }

Slide 22

Slide 22 text

Reading Code Understanding a piece of code

Slide 23

Slide 23 text

Reading Code • Structured exercises

Slide 24

Slide 24 text

Dimensions of code reading • Structure • Domain • Concepts • Context

Slide 25

Slide 25 text

Structure Code is not read linearly

Slide 26

Slide 26 text

Domain • Behavior • Data

Slide 27

Slide 27 text

Concepts

Slide 28

Slide 28 text

Reading Code • Context

Slide 29

Slide 29 text

Help in the IDE • Structure / Restructure • Domain • Behavior • Syntax highlighting & hints • Context

Slide 30

Slide 30 text

Strategies • Structure • Identify components & relationships • Identify concepts • Gain context

Slide 31

Slide 31 text

A slice of the application

Slide 32

Slide 32 text

Find a starting point

Slide 33

Slide 33 text

Find a starting point • Generic • Main method • Endpoint • Speci fi c • Error message / Exception • Logic / Functionality

Slide 34

Slide 34 text

Help in the IDE • Search • Navigate

Slide 35

Slide 35 text

Strategies • A slice of the application

Slide 36

Slide 36 text

Reading Code The bigger picture

Slide 37

Slide 37 text

Understanding a new code base • Joining a new team / organisation • Using a new code base

Slide 38

Slide 38 text

Reading Code • What does the code do?

Slide 39

Slide 39 text

Context

Slide 40

Slide 40 text

Reading Code A good place to start: • Check out the code • See if you can build it

Slide 41

Slide 41 text

Help in the IDE • README • Project structure • Dependencies • Diagrams

Slide 42

Slide 42 text

Strategies • Identify components • Identify relationships / dependencies • Identify concepts • Gain context

Slide 43

Slide 43 text

Reading Code What's next?

Slide 44

Slide 44 text

Structured exercises • Code Reading Club: https://codereading.club/ • Code Reading Club Resources: https://github.com/CodeReadingClubs/Resources • The Programmer's Brain - Felienne Hermans • Code Reading in Practice - Felienne Hermans & Gien Verschatse

Slide 45

Slide 45 text

Let your tools help you • Structure/Restructure • Reformat • Refactor • Tests & Debugging • Git history • Search & Navigate • Diagrams • ...

Slide 46

Slide 46 text

... and your brain

Slide 47

Slide 47 text

Reading code Slides & More https://maritvandijk.com/presentations/reading-code/

Slide 48

Slide 48 text

Code reading exercise First glance The goal of this exercise is to practice getting a fi rst impression of code. (1 min) Independently - Glance at the code Note down the fi rst thing that catches your eye. Then note the second thing that catches your eye. Think about why you noticed those things fi rst.

Slide 49

Slide 49 text

Code reading exercise Dependency graph The goal of this exercise is to get a feel for the structure of the code. (5 min) Independently - Examine structure Variables 1. Go through the code and underline all variables 2. Then draw a link between variables and their uses Function / method calls 3. Go through the code and circle all methods / function calls 4. Then draw a link between methods and their invocations Instantiation 5. Go through the code and draw a box around all instances of classes 6. Then draw a link between classes and their instances

Slide 50

Slide 50 text

Code reading exercise Most important lines The goal of this exercise is to start to think about which lines in the code de fi ne its essence, have the biggest impact or need to be paid close attention to. (5 mins) Independently - Identify most important lines Brie fl y discuss what it means to be important as a group (if you want to). Then, identify the 5 lines you consider most important.

Slide 51

Slide 51 text

Code reading exercise Concepts (5 min) Individually - Central thematic concepts Name the 5 most central concepts of the code. These could be names, themes, classes, or information found in comments.

Slide 52

Slide 52 text

Code reading exercise Summary The goal of this exercise is to think about the core purpose or function of this code. (5 mins) Independently - Summarize Try to write down the essence of the code in a few sentences.