Debugging Demystified — Conditional Breakpoints
Intro
Debugging is one of the major skills that every engineers should master. Investing time on learning debugging techniques will help us to build better products by increasing our productivity. In the upcoming blog posts I will be posting some of the powerful debugging techniques found in the Xcode eco system.
Conditional Breakpoints
Conditional breakpoints become handy when you want to play with dynamic data. It gives us more control over how the breakpoint should work.
Lets have a look at how it works.
func staircaseBuilder(count:Int) {
for outerNumber in 1...count {
var str = ""
for innerNumber in 1...(count) {
str += innerNumber <= (count-outerNumber) ? " ":"#"
}
print(str)
}
}
If we pass 5 for the above function it will print the following staircase structure in the console
#
##
###
####
#####
Lets add a breakpoint in the Xcode. And add condition for breakpoint by Control-click the breakpoint & choose “Edit Breakpoint”
We will modify the output of the above algorithm in run time.
[caption id=”attachment_418" align=”alignnone” width=”1722"]
Guess the output :)[/caption]
Ok! lets break it down.
- [ innerNumber % 2 == 0 ] : We added a condition to check that the innerNumber can be divided by 2
- [ expr str += “ “ ] : We added an action that will modify the variable str if the above condition is met
- By setting the option we tell the debugger to continue the execution after evaluating the action
And we get the following output in the console
#
##
# ##
## ##
# ## ##
(Don’t use this staircase ;) )
It allow us to modify the values without re compiling & running the project. It will be very helpful when finding bugs in Algorithms & data manipulations tasks.