Someone else's code. Even worse, thousands of lines, maybe hundreds of files of other peoples code. Is there a way to methodically read and understand other peoples work, build their mental models? In this talk I will go through techniques I have developed throughout 18 years of programming. Hopefully you will walk away with a plan on how to approach a new code base. But even more I hope you walk away with a feeling of curiosity, wanting to get to know your fellow programmers through their code.
3. Patricia Aas - Vivaldi Browser
Programmer - mainly in C++
Currently : Vivaldi Technologies
Previously : Cisco Systems, Knowit, Opera Software
Master in Computer Science - main language Java
Twitter : @pati_gallardo
4. - So… You Got Someone
Else’s Code?
- Before You Start
- 10 Techniques
- Different is Good
@pati_gallardo
10. #preperation
- Get the code
- Put it in source control
- Put it in a “smart” IDE
- Try to build it
- Try to run it
- Preferably in a debugger
@pati_gallardo
13. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
15. Strings you see
as ends to pull on
- in the GUI
- on the commandline
- in the logs
@pati_gallardo
16. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
18. - Grep for the button text
- Find the button
- Set a breakpoint on onClick
- Click on the button
- Look at the stack
- Traverse up the widget
hierarchy
@pati_gallardo
19. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
21. Investigating Your GUI
framework
- Trace platform events
- Look at graphics output
- Find the platform
integration architecture
@pati_gallardo
22. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
24. Integration / System Tests
- How to run it
- Use Cases
- Write tests to drive the code
you’re looking at
- Write tests to examine your
assumptions
@pati_gallardo
25. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
28. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
30. The How
Execution Architecture
- Mainloop & event handling
- Read top to bottom
- Take notes & draw
- Important objects/functions
- Watch for common types
- Recurse@pati_gallardo
31. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
33. - Find the Main Layout
- Find the (implicit) State
Machine
- This is what changes the
window contents
- Maps often to Use Cases
@pati_gallardo
34. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
36. Rough Outline of Architectures
- Event driven : main loop, async, event handlers
- Request handling : one thread per request - mostly
synchronous
- Command line tool : mostly synchronous, takes input,
produces output
@pati_gallardo
37. - Use the debugger to
examine runtime state and
stacks
- Read the logs to see flow
- Run the tests
- Add logging
- Add tests and assertions
- Add a feature@pati_gallardo
38. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
40. - Which interfaces does it
implement?
- Who uses it and how?
- Public functions are the
“mains” of a class
(Getters don’t count)
@pati_gallardo
41. 1. Grepping
2. Where is this button?
3. Following input events
4. What do the tests do?
5. Refactoring
6. Reading “main”
7. The graphical layout
8. Runtime Investigation
9. Reading a class
10. Retelling or Rubber Ducking@pati_gallardo
43. Explain It To Someone
Write a (fictional) blog post
Write some documentation
Make an internal prestation
(Ducks aren't very motivating)@pati_gallardo
45. Great code should be personal
We want people to take pride
in their work
Learn to appreciate other
people's code
Style is individual
@pati_gallardo
51. #goals
- Establishing a vague
outline and fleshing it out
in an iterative process
- Taking notes and drawing
- Make documentation
- Teach others
@pati_gallardo
52. Code is like Balls of Yarn on the FLoor
It’s a mess.
How do you know where to begin?
Find an interesting end:
Pull on it
@pati_gallardo
53. Refactoring is opinionated
Use it to understand,
throw it away and go back to the original code
Do not judge
@pati_gallardo
54. You Need a Full Toolset
For synchronous execution the debugger is useful
For async the log will yield interesting places
@pati_gallardo
55. If you approach other people's
code wanting to learn:
You will learn
If you approach to criticize:
You will criticize
@pati_gallardo
56. “Instead of condemning people,
let’s try to understand them.
Let’s try to figure out why they do what they do.
That’s a lot more profitable and intriguing than criticism;
and it breeds sympathy, tolerance and kindness.”
Dale Carnegie, How to Win Friends & Influence People
@pati_gallardo