UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Sharpshooter: debugging for lazy people

If you think print() is a great way to debug your code, this is the perfect Xcode extension

Paul Hudson       @twostraws

There are lots of ways of debugging programs, but probably the first one we all learn is writing print() statements everywhere in our code. This simple approach makes it easy to follow program flow: functionA() got called first, then functionB(), then evilCrashFunction(), and boom – you've found what causes the problem.

Despite this approach being primitive at best, it tends to stick simply because it was the first thing we learned, so even experienced developers have been known to scatter some print() calls around to figure out what's going on.

Well, if this is your preferred approach – or if you know a "friend" who works like this – I wrote a new Xcode extension that will help. It's called Sharpshooter, and it lets you add or remove print() statements to your function entry points in just one click.

Here’s an example of code before adding comments:

class BrokenStuff {
    func broken() {
        print("<Silent screaming>")
    }

    private func reallyBroken() {
        print("<Tears>")
    }

    static private func superBroken() {
        print("<Silent screaming *and* tears>")
    }
}

And after:

class BrokenStuff {
    func broken() {
        print("# Sharpshooter: Entering broken()")
        print("<Silent screaming>")
    }

    private func reallyBroken() {
        print("# Sharpshooter: Entering reallyBroken()")
        print("<Tears>")
    }

    static private func superBroken() {
        print("# Sharpshooter: Entering superBroken()")
        print("<Silent screaming and tears>")
    }
}

In case you were wondering, the name comes from a joke about a Texan sharpshooter who fires gunshots at the door of a barn, then draws a target around the biggest cluster of hits and claims to be a sharpshooter – a pretty accurate analogy for finding bugs using print(), I think.

Link: Sharpshooter.

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.