COMP 10066
Week 3 Lecture
Code Reviews and Debugging
Lecture Overview
• Code Reviews
Inspection vs Walkthrough
What to look for?
• Debugging
Print statements
Debugger in IntelliJ
Static Analysis Tools
Code Reviews
Participants in Code Review
• Original Programmer
• Mangers
• Other programmers
• Testers
• Stakeholders
Code Review – Peer Reviews
• General informal review process, where the
code is viewed by another programmer
• Usually, a more senior programmer will do this
review
• Mentoring opportunity
• Ensures company policies and standards are
being met
Code Review –
Walkthroughs
• More systematic / detailed review of work
• Typically done by a group (3 – 4 programmers)
• One programmer is the original author and
walks the rest through the code
• Ensures company policies and standards are
being met
• Allows for transfer of knowledge between
programmers
Code Review – Inspections
• Formal review of the code
• Can include mathematical proof on concepts
• Can include mapping of requirements directly
to areas of code to prove implementation
• Typically done on systems that impact human
safety
Nuclear reactor, space shuttle
Code Review – Inspections
• Involves a series of events and meetings
Planning – each participant is provided with
information about what is being reviewed
Overview meeting – original programmer
provides overview of code
Preparation – each participant individually
inspects the code
Formal Inspection Meeting – formal
walkthrough, recording issues found
Rework – original programmer refactors the
code based on what was found in the
inspection meeting
Follow up – changes made are confirmed by
participants
Code Review – What you are
looking for?
• Data Reference Errors
Is the correct variable being used?
• Data Declaration Errors
Is the correct type being used?
In many cases the compiler (strongly typed)
will assist in detecting problems with data
type
This can be an issue in loose type
programming languages (e.g. PHP /
Javascript)
Code Review – What you are
looking for?
• Computation Errors – Is the calculation correct
Need to verify with another method
Hand, Calculator, A separate application.
• Comparison
Are all conditions correct
Need to be careful when creating multiple
condition expression and when using
negative logic.
• Control of Flow
Loop entry and exit
Are entry and exit conditions for flow control
set correctly
Code Review – What you are
looking for?
• Parameter Errors
Are the parameters used within a function
call correctly specified
Is there a verification of the values – range
checking
• Input / Output
Are the data input / outputs matched to the
appropriate variables.
Advantages of Code
Reviews
• Find and identify issues early on
• Ensure compliance with standards and
specification
• Transfer of knowledge
• Discussion around workflow and potential user
experience issues
Disadvantages of Code
Reviews
• Time consuming
• Programmer’s “ego” gets hurt
• Miscommunication
• Personal preferences pressed upon someone
Code Reviews are useful for many reasons, but
are not a replacement for Formalized Testing
Best Practices for Code
Reviews
• Use constructive feedback
• Take the time to think if suggested change is a
functional change or a preference change
• Support feedback with reasons
• Try not to let your ego get in the way
Handout – Code
Peer Review
Come up with a list of suggestions on how to
improve the algorithm given on today’s handout.
Debugging
Rubber Ducking – Talk it out
• Similar to code reviews – talking about the
problem often can help brainstorm solutions
• Term comes from programmers that used to
talk to inanimate objects (Rubber Ducks,
Teddy Bears)
• Of course, the duck is optional!
• Idea is reasoning the problem out loud often
helps us to realize or see things we missed
• You may have already done this with your
assignments, especially when discussing them
with a fellow student
Print Statements
• Often used to find out the state of the program
during execution
State of a program – a snapshot of what the
variable values are if the program was
paused for a moment
• In live code, this could also be printing to a log
Be careful of security issues with logging –
sensitive data should not be kept in logs
• Can help the programmer:
Confirm a certain block of code was entered
See the value of a variable
Using the IDE
(Integrated Development Environment)
• Inserting a breakpoint will pause execution of
the code on that line when in debug mode
• Java – IntelliJ Example
• Debug Mode is different than Run – In IntelliJ
the green bug is the debug mode
• Breakpoints are created and removed by
clicking the blank space between the line
number and the code
Using the Debugger in
IntelliJ
Many of these symbols are similar in different
IDEs
• Button 1 – bent arrow – Step Over
Executes the current line of code and steps
to the next one in this function (does not
enter another method)
• Button 2 – downwards arrow – Step Into
Enters the method if the current line is a
method call
Using the Debugger in
IntelliJ
Many of these symbols are similar in different
IDEs
• Button 3 – upwards arrow – Step Out
Runs until the end of the current method and
returns to where the method was called
• Button 4 – diagonal arrow – Run to Cursor
Runs until where the cursor is
Using the Debugger in
IntelliJ
• Each moment the execution is paused we can
see the state of the program in the debug
console
• Screenshot above is from debugging
countAllVowels method
Putting this into practice
• The lab activity next week will have us practicing
our debugging skills