Please do not redistribute these slides without prior written permission
1
CS 5500
Foundations of Software Engineering
Dr. Mike Shah
2
Pre-Class Warmup (1/4)
3
Pre-Class Warmup (2/4)
4
Person
Pre-Class Warmup (3/4)
5
Pre-Class Warmup (4/4)
6
Note to self: Start audio recording of lecture :)�(Someone remind me if I forget!)
7
“The Daily Scrum”
8
Last Class (“What did I do yesterday”) (1/2)
9
Last Class (“What did I do yesterday”) (2/2)
10
Course Logistics (“What are we going to do today”)
11
Blocking (“What is stopping forward progress”)
12
Goal(s) for today
Today is all about giving you tools to fix bad code...for the 1% of us who create bugs ;)
13
What Causes Bad Code to be Written? (1/3)
14
What Causes Bad Code to be Written? (2/3)
15
What Causes Bad Code to be Written? (3/3)
16
I want to start off by touching on this one -- and then seeing what we can do to mitigate this circumstance.
Code Smells
17
Question to Audience: What is a Code Smell? (1/3)
18
Question to Audience: What is a Code Smell? (2/3)
19
Question to Audience: What is a Code Smell? (3/3)
20
How to write good code in DLang
21
D Style Guide [https://dlang.org/dstyle.html]
22
(Aside) Inspiration from other languages
23
DLang Avoiding Code Smells
Walter Bright on Avoiding Code Smells (https://www.youtube.com/watch?v=lbp6vwdnE0k)
24
Nice Formatting
25
dfmt Demo (https://github.com/dlang-community/dfmt)
26
(short) Global Variables
27
(short) Global Variables
28
Style
29
Don’t reuse variables
30
Bad!
Good
Best
What compilers do
Don’t reuse variables
31
Bad!
Good
Side Channel Globals
32
Bad!
Good
Aggregate Globals
33
Bad!
Better
Aggregate Globals
34
Leaky Abstractions
35
Bad!
Better
Usage
Leaky Abstractions bad (left) -- fixed on the right
36
Supporting multiple platforms
37
Pull Requests
38
We can go on...
39
(Aside) More on Code Smells https://www.youtube.com/watch?v=D4auWwMsEnY
40
(Aside) More on Best Practices https://www.youtube.com/watch?v=nqfgOCU_Do4
41
Potential Code Smell - Construction Separate (1/2)
42
Potential Code Smell - Construction Separate (2/2)
43
Code Smell - Out Variables (1/3)
44
Code Smell - Out Variables (2/3)
45
Note: In DLang we can explicitly specify ‘out’ which fixes this problem somewhat
Code Smell - Out Variables (3/3)
46
Code Smell - Raw Loops (1/3)
47
Code Smell - Raw Loops (2/3)
48
Code Smell - Raw Loops (3/3)
49
This is a templated function that takes in a predicate ( a function that evaluates to true or false)
We’ll talk more about this as we proceed further.
Code Smell - Multi-Step Functions (1/3)
50
Code Smell - Multi-Step Functions (2/3)
51
Code Smell - Multi-Step Functions (3/3)
52
How to learn ALL the rules? (1/2)
53
How to learn ALL the rules? (2/2)
54
Let’s take a look at some tools now!
Introduction to Program Analysis
(Static Analysis & Dynamic Analysis)
55
The field of Program Analysis (1/2)
56
The field of Program Analysis (2/2)
57
So what are our tools for figuring out these properties?
Tools for Program Analysis
The two most common tools for program analysis are:
58
*There are also hybrid analysis of static and dynamic analysis
Static Analysis
Analyzing program source and/or data before they run
59
Static Analysis (1/3)
60
Static Analysis (2/3)
61
Fact Check: Could this program ever divide by 0?
Static Analysis (3/3)
62
Fact Check: Could this program ever divide by 0?
How Does Static Analysis Work?
63
Example #1 Static Analysis in the Compiler
64
Example #2 Static Analysis in the Compiler
65
Example #2 Static Analysis in the Compiler (GDC)
66
How do these analysis work? CFG Abstraction (1/2)
67
How do these analysis work? CFG Abstraction (2/2)
68
Program Correctness and Program Optimization
69
Another Static Analysis -- Indent tool (1/3)
70
Another Static Analysis -- Indent tool (2/3)
71
Question to the audience: Is this a static analysis?
Another Static Analysis (sort of) -- Indent tool (3/3)
72
Question to the audience: Is this a static analysis?
DLang Static Analysis Tools -- dscanner
73
DLang Static Analysis Tools -- dscanner
74
2. Dynamic Analysis
75
Dynamic Analysis (1/2)
76
Dynamic Analysis (2/2)
77
Fact Check: What are all of the values of ‘x’ a user input in this instance of the program
Example #1 of Dynamic Analysis - Profilers (man perf)
78
Call Tree
Entries Sorted By Time Spent
Profiling
79
-profile [switches see -profile]
80
Example #2 of Dynamic Analysis - Valgrind (pronounced val--grinn)
81
(Repeat) Program Correctness and Program Optimization
82
Static and Dynamic Analysis
(Digging a little deeper)
83
Static Analysis and Dynamic Analysis (1/3)
84
Static Analysis and Dynamic Analysis (2/3)
85
Static Analysis and Dynamic Analysis (3/3)
86
Notice the key difference here between static and dynamic analysis?
Static Analysis - A little more rigorous (1/2)
87
Static Analysis - A little more rigorous (2/2)
88
Static program analysis allows us to ask and answer questions about program behavior
Static Analysis
89
Execution Space
All possibilities of what your program could do
What your program actually does
Dynamic Analysis
90
Execution Space
All possibilities of what your program could do
An actual execution
An actual execution
An actual execution
An actual execution
Which type of analysis is better?
91
Static Analysis at Google Scale
92
Short 5 minute break
93
Onwards to Debugging
94
What you’ll learn today -- the metaphor (1/3)
95
What you’ll learn today -- the metaphor (2/3)
96
What you’ll learn today -- the metaphor
97
What is a bug?
A good place to start
98
Some images today from the wonderful movie ‘A Bug’s Life’ by Disney Pixar.
Apologies for any spoilers! It is a great movie! :)
What is a Software Bug?
99
Debugging
The task of removing faults (i.e., defects or bugs) from code using tools and techniques
100
A Bug's Life was a Disney Pixar film in 1998 -- It is not important that you watch it to understand the topic of today :)
Infamous Software Bugs
101
This image is from the American game show “Jeopardy in which contestants answer questions in the form of a question to earn money
Famous Bugs
The First Software Bug - September, 1947
102
Mars Climate Orbiter - 1998
103
Win 98 Blue Screen of Death (~1998)
104
Win 98 Blue Screen of Death (~1998)
105
And I don’t mean to embarrassed this gentleman on the right -- we know developing software can be tricky!
Y2K Bug - 1999
106
More bugs (Costly bugs!) [source]
107
More bugs (Costly bugs!) [source]
108
BUGS!
Why are we creating bugs?
What’s the difficulty?
109
Why is it hard to get software correct? (1/2)
110
Why is it hard to get software correct? (2/2)
111
Reality of Software Development (1/2)
112
Today’s topic unfortunately however...is too much of a mystery
113
Today’s topic unfortunately however...is too much of a mystery
114
Learn some debugging techniques
Today’s Goal
“Although computer science education devotes a lot of time to teaching algorithms and fundamentals, it appears that not much of this time is spent applying them to general problems. Debugging is not taught as a specific course in universities. Despite decades of literature suggesting such courses be taught, no strong models exist for teaching debugging.” [ACM Queue The Debugging Mindset 2017]
Daily Wisdom
(Everyday wisdom)
115
From Chuck Norris
116
https://memegenerator.net/img/instances/45924170.jpg
From Chuck Norris
117
https://memegenerator.net/img/instances/45924170.jpg
Sorry...that advice is not going to fly!
Do use your debugger!
Some wisdom from Dr. Greg Law
118
Debugging versus testing
119
Debugging Techniques
This is interactive--see if you can spot the bug!
120
#1 Scan and Look Debugging
121
...kind of
Common Strategy - Scan and look (1/5)
122
Common Strategy - Scan and look (2/5)
123
Hmm, do you spot the logic bug? (Either in the code or output)
Common Strategy - Scan and look (3/5)
124
The bug has been spotted!
Logical error/typo by the programmer.
Did not provide the correct type.
The lesson here--even if code compiles, it does not imply correctness!
Common Strategy - Scan and look (4/5)
125
Here’s the corrected code using the correct type
Common Strategy - Scan and look (5/5)
126
Note: The D compiler can be quite handy and prevent some of these ‘implicit conversions’ from happening.
It’s generally wrong for the compiler to assume and truncate 3.1415 should be an integer, so the appropriate error is thrown.
Tradeoffs - Scan and Look (1/2)
127
Where’s Waldo is a children's book where you try to find the main character
https://images-na.ssl-images-amazon.com/images/I/A1auIg-I7WL.jpg
Tradeoffs - Scan and Look (2/2)
128
Where’s Waldo is a children's book where you try to find the main character
https://images-na.ssl-images-amazon.com/images/I/A1auIg-I7WL.jpg
The good news is, we have a tool that can help us having to do this strategy automatically for us.
The compiler can help!
Scan and look (with the compilers help)
129
Tradeoffs - Scan and look (with the compilers help)
130
#2 printf debugging
A technique for helping us debug and retrieve values at run-time
131
Common Strategy - printf debugging (1/5)
132
Try to find the bug!
Common Strategy - printf debugging (2/5)
133
Try to find the bug!
No warnings this time
But see if you can spot the bug (Don’t say anything yet!)
Common Strategy - printf debugging (3/5)
134
Try to find the bug!
Depending on how much code I put on the screen--this bug can be harder to find!
Let’s try to help ourselves out with some output (i.e. writeln) statements
(Bug shown on the next slide)
Common Strategy - printf debugging (4/5)
135
Try to find the bug!
Some well placed output statements anywhere state can change (i.e. a value can be generated or a variable mutated) reveal the value of square(5).
We observe the incorrect value, and confirm we never enter the branch and see ‘output 2’
Common Strategy - printf debugging (5/5)
136
Try to find the bug!
oops, an error in our functions return value--should be (a*a)
Tradeoffs - printf Debugging
137
#3 Delta Debugging
(A technique to help us narrow our search space for where a bug occurs)
138
Strategy for debugging - Delta Debugging (1/3)
139
Strategy for debugging - Delta Debugging (2/3)
140
Potential bug could be anywhere
Strategy for debugging - Delta Debugging (3/3)
141
Our search space for the bug is somewhere in this range -- that’s where we can gather information.
(Note square function is included in our delta because it is in our search space of where we put the writeln statements.)
Reminder: Turn on warnings (-w)
142
#4 printf debugging revisited
Improving our printf debugging using our programming language
143
(Aside) printf Debugging in C and C++ with preprocessor
144
Check out this video for C and C++ and notes on the preprocessor
https://www.youtube.com/watch?v=ksJ9bdSX5Yo&list=PLvv0ScY6vfd8YRjgGvXKJRAMZQAxNypcH&index=4
Conditional Compilation in DLang (-debug flag)
145
1
2
Conditional Compilation in DLang (version flag)
146
1
2
Tradeoffs - Conditional Compilation to Debug
147
Breaking Old Habits
148
Short 5 minute break
149
Interactive Debuggers
Tools that allow introspection into code at run-time e.g., GDB
150
Yes....you will have a part of this -- debuggers save you time!
Interactive Debugger
151
How Debuggers Work
152
Compiling with Debugging Symbols to help GDB
153
Running your program with GDB
154
GDB Live Code
Sample code available in course repository at: https://github.com/MikeShah/C-SoftwareEngineering/tree/master/3 (gdb.d)
155
GNU Debugger GDB (1/3)
156
GNU Debugger GDB (2/3)
157
You can use whatever debugger you like, but I will show examples in GDB for you to follow along with.
Most IDEs have the same functionality and methodology that I will show, perhaps a different workflow
Mac on M1/M2 (Apple Silicon) should use LDC2 with lldb which is similar
GNU Debugger GDB (3/3)
158
One other note is that you’ll occasionally see me press ‘Ctrl+L’ to quickly clear the screen of my terminal window.
Typing ‘refresh’ should also work.
Let’s dive in
159
Round 2 -GDB TUI (Text User Interface)
160
Breakpoints and stepping through code
161
Breakpoints and stepping through code
162
163
Watchpoints
164
Conditional Breakpoints
165
Backtrace (retrieving the call stack)
166
GDB - Attach to a running process
167
Slightly More Advanced Example (time travel)
168
Slightly More Advanced Example (polymorphism)
169
More debugging resources
170
More debugging resources
171
More debugging resources - DDD (1/2)
You are welcome to explore more tools and use them in this course
172
More debugging resources - DDD (2/2)
173
Text Editors and IDEs
174
(Aside) Debugging on Mac
175
Debugging Summary
176
Debug and Release Builds
Other considerations to be careful of when distributing software to the masses
177
Debug and release builds (1/2)
178
Debug and release builds (2/2)
179
Some General Tips on Code Writing and Debugging
180
List of Tips to write better software and ease debugging
181
Closing Thoughts (1/2)
182
Closing Thoughts (2/2)
183
Retrospective
184
In-Class Activity
185
In-Class Activity
186
Extra
187
A Code Smell
188
Refactoring Code
189
(Reference from Stranger Things--a great Netflix series!)
Code Smell - Non-Canonical Operators (1/2)
190
Code Smell - Non-Canonical Operators (2/2)
191
192
Linux Debugging Example
193
Compile in debug code
-debug=level
Compile in debug level <= level
-debug=ident
Compile in debug identifier ident
-debuglib=name
Link in libname as the default library when compiling for symbolic debugging instead of libphobos2.a. If libname is not supplied, then no default library is linked in.
194
One ‘teaser’ C++ Design Idiom
(With a fun name--we’re going to observe this later on in the semester)
195
PIMPL Idiom (“Pointer to Implementation”)
196
Liskov Substitution Principle (LSP)
197
INVEST Acronym
198
Open Closed Principle
199
List of debugging papers from Prof. Guyer: https://www.cs.tufts.edu/comp/150BUGS/#schedule
200