This workshop will be retired on May 31, 2020.
Introduction to Debugging5:26 with Gabe Nadel
Let's get a high-level overview of how and why we developers spend so much time debugging. Along the way, we'll cover some of the most common terminology you'll need to know.
Hi there, I'm Gabe and IOS teacher here at Treehouse, 0:04 today we're going to talk to you about one of the most important skills you'll need 0:08 to develop as you learn to code, debugging. 0:11 Now, depending on your past programming experience, 0:14 you may have some notions about when why and how we debug. 0:16 But just to be sure, let's start at the very beginning. 0:20 So, first off what is debugging? 0:23 Well, in its narrowest definition, debugging is the process of locating and 0:25 fixing defects, aka bugs, in software or systems. 0:29 Now that's great, but 0:34 I'd say that definition is actually a bit misleading to an aspiring programmer. 0:35 It be a little like saying that c is a guitar player who's best known for 0:39 playing really loud music. 0:43 Sure, that's true but there's a lot more to it than that. 0:45 You see, when developing software, we often do find defects or bugs, but 0:49 non-programmers or 0:53 novice programmers probably think of those things in terms of the symptoms users see. 0:54 For instance, let's say you open up your favorite news app and for 1:00 some reason, certain words are characters in the article 1:03 are being replaced by weird symbols. 1:06 Or maybe the app refuses to update yesterday's news to today, or 1:08 worst of all, the app just crashes when you try to open it. 1:11 Now, these are most definitely bugs and they will definitely need to be debugged. 1:14 But to me, 1:19 they're just one of the uses of what we might loosely call debugging skills. 1:19 To solve the problems I mentioned above, 1:25 a developer would probably place a bunch of breakpoints in the code and run it. 1:27 They'd stop in spots where they might find clues as to what's going to awry, 1:31 leaks in the plumbing, loose ends, square pegs in round holes, stuff like that. 1:35 But let's take a step back. 1:40 The news app, which we've now deemed defective, 1:42 is a real live app we downloaded from the App Store. 1:44 It probably took hundreds, maybe thousands of hours to build. 1:46 Chances are most of the debugging took place before it was ever even on sale. 1:50 I just happened to know that when this hypothetical app was first being built, 1:55 the developer was trying out several new tools. 1:59 In fact, they'd been a developer for nearly a decade, but 2:01 only had limited experience coding in Swift. 2:04 They weren't yet fluent enough to just write syntax off the top of their head. 2:06 So, they had to stop frequently and look things up. 2:10 Then, even once their code compiled successfully, 2:12 they realized the news stories weren't showing up in the app. 2:16 They had to go poking around in the code to find out why. 2:18 I suppose you could call that fixing a defect. 2:21 But you could just as reasonably say they're simply building the core 2:24 feature of the app. 2:27 Then a few months later, just before the planned launch of the app, 2:28 our trusted developer was poached by a famous Silicon Valley company. 2:32 Luckily, we found another developer quickly, but 2:36 those two developers, they never got to meet. 2:38 And to make matters worse, our first developer didn't put in many comments. 2:40 So, now our second developer is in the common position of deciphering 2:45 someone else's code. 2:48 Guess what they do is their first task? 2:49 Yep, they employ debugging skills to step through the code 2:51 to figure out the original authors logic, architecture and so on. 2:54 Sure, some of this they could figure out just by reading the code. 2:58 But you never really know if you're reading it correctly 3:01 until you pull back the curtain and follow the flow. 3:04 Thankfully, they did such a thorough job of internalizing 3:07 the original author's code that when she did find a bug, 3:09 namely that the dates on all the news articles were wrong, 3:12 she all ready had a good idea of where in the code base she'd be fixing the issue. 3:15 Before we get into the nuts and bolts of debugging using Xcode, 3:20 let's define a few terms for debugging in general. 3:23 A couple minutes ago I snuck in the word breakpoint. 3:27 Well a breakpoint is pretty much what it sounds like. 3:30 It's a place in the program where you intentionally stop or 3:32 break to see what's going on. 3:35 Just to be clear, a breakpoint is not the point where your code breaks down. 3:38 It's where you decide to take a break to get a closer look. 3:42 When you do positive breakpoint, you can inspect variables, values, and 3:46 code at that breakpoint or 3:49 continue to step through the code in a few ways we'll demonstrate inside Xcode. 3:51 The debugger is a program we used to actually 3:56 debug the software we're creating, which is often called the target program. 3:59 In Xcode we use LLDB though you may at times see posts or 4:03 documents referencing the older GDB. 4:07 In case you're curious about Apple's shift to LLDB, I provided a link below. 4:10 But before we roll up our sleeves, we have one more term we sort of glossed over. 4:16 What is a bug exactly anyway? 4:20 Well a bug is a flaw, defect or failure in a program or 4:22 system usually causing unexpected or unwanted consequences. 4:26 You probably figured out that definition on your own, but 4:30 do you know how the name came about? 4:33 Well, if you dig back to the century before last, you actually see 4:35 Thomas Edison and his peers using the term bug to describe faults and 4:38 difficulties in mechanical engineering. 4:41 Though what many programmers think of as the root of the term 4:44 actually took place just after World War II in 1947. 4:48 In a delightful collision of etymology and entomology, a group of Harvard scientists, 4:52 including computing pioneer Grace Hopper, 4:56 found that there was hardware that was malfunctioning. 4:59 After literally poking around, 5:02 Hoppers's colleagues located an actual moth stuck in the machinery. 5:03 The moth was removed, taped to the lab log and 5:08 given an honorary doctorate in engineering. 5:10 Okay, I made up that last bit about the honorary degree. 5:13 But if you'd like to see the famous moth, I've linked a picture below. 5:16 Okay, while these concepts are still fresh in your mind, 5:19 join me in the next video where we'll hunt down some bugs of our own. 5:22
You need to sign up for Treehouse in order to download course files.Sign up