1 of 15

What Does This Code Do?

2 of 15

Agenda

  • Why read code?
  • How to read code
  • What does this code do?

2

3 of 15

1.

Why Read Code?

I just want to write all the code

3

4 of 15

Why Read Code?

  • The most common activity for professional programmers is reading code
  • A typical programmer writes (on average) 25-50 lines of code every day
  • Fixing bugs require reading code
    • With small rewrites
  • Adding features and functionality requires integration with existing codebases
    • Very difficult not to break existing code

  • What about this course?
    • Next slide ➡️

4

5 of 15

Why Read Code?

  • In this course, there are two primary reasons for reading code:
    • There is a bug in your code and you need to figure out how to fix it
    • You are trying to integrate into a library and need to understand how other programmers have used the library

5

6 of 15

2.

How to Read Code

“I’d never write it this way!”

6

7 of 15

Fixing Bugs

  • When code doesn’t do what you expect (intent vs implementation)
  • Use different options:
  • Try to understand what’s going on with different inputs
  • Print variables
  • Run the code “by hand”

7

8 of 15

Different Inputs

  • Least effective method of “reading code”
  • Sometimes the only option
    • If no access to original source code or documentation
  • Run with many variations of inputs
  • Use scientific method
    • Vary one element at a time
    • Record all results

8

9 of 15

Print Variables

  • Most common method
    • There is a trade-off between time and understanding
  • Print lots of variables
  • Run the program in different ways
  • When variables are different from expectation there is a possible bug (intent vs implementation)

9

10 of 15

Run code by hand

  • Time consuming but very effective
  • Start at beginning of program
  • Write down each variable as you come to it in the code
  • Make a column for each variable
  • Any time the variable changes, note the new value in the appropriate column
  • Note anything that is printed to the screen
  • Anytime there is a disconnect between intent and implementation, there may be a bug (in thinking or implementation, or both!)

  • The faster version of this is using the debugger that comes with the development environment
    • Repl doesn’t really have a debugger 😕

10

11 of 15

3.

What Does This Code Do?

Excellent question!

11

12 of 15

What Does This Code Do? Part 1

  • Walk through the “code-by-hand” process for the following code - not through repl!

  • Check your results in repl and with the rest of the class
    • How would you use extra printing to help?

12

13 of 15

What Does This Code Do? Part 1

13

14 of 15

What Does This Code Do? Part 2

  • Walk through the code by hand process for the following code - not through repl!

  • When completed, each student should have:
    • Same number of variables
    • Same values in each column
    • Same printed output
  • Check your results in repl
    • How would you use extra printing to help?

14

15 of 15

Credits

Special thanks to all the people who made and released these awesome resources for free:

  • Presentation template by SlidesCarnival

15