Dude, get a debugger!
Tags: debugger, javascript, problemsolve
Published: 2019-03-23 19:05:19

---
title: Dude, get a debugger!
published: true
description: An introduction to basic problem solving by using a debugger (Beginners Level)
tags: Debugger, JavaScript, Problem-Solve
---


<img title = "you, pissed off @ your code" src="https://media.giphy.com/media/SrPwcBxqEsNgY/giphy-downsized.gif"/>
<center><figcaption> IT JUST DOESN'T F***ING WORK !</figcaption></center>

<p> Does the above describe sounds like you at 9pm after a few beers have hit your system; after spending hours on end to no avail with a problem that has you questioning your very existence in the developer universe?</p>

<img title = "How you look in confusion at your code" src="https://media.giphy.com/media/xT8qBgHkfCACqvjJny/giphy.gif"/>
<center><figcaption> MY CONSOLE.LOG()/PRINT() STATEMENTS NOT HELPING !</figcaption></center>

<p> Do you spend hours on end putting random logging statements in your code with messages like "here", "it works", or just gibberish "hfdhfdhfd"?</p>

<h2>Dude, get a debugger!</h2>

<p>If the above sounds like yourself, and you're ready to tackle these mundane issues in a more methodical manner to end your torture, then its time to learn how to use A debugger. I say A debugger, because THERE IS more than one debugger. As a Data Visualization Engineer, I focus more on the web technologies so my debugger lineup falls around the ones included within the popular web browsers (I.e. Chrome Debugger, FireFox Debugger, etc.), but other languages such as Python have their own debugger for their language, and even IDEs and Text-Editors such as Visual Studio and Visual Studio Code have their own embedded or available for install.</p>

<h2>Why a fancy debugger?</h2>

<p>A debugger will allow you to quickly see a problem as it rises in the execution of your code, before waiting for final output of the broken state that's frustrating you. Think of a debugger as a tool that when it runs into a issue, it will pause the program from continuing, show you the location of said problem along with other data available at that time in the program. For example, if you have a variable named "result" that needs to be used elsewhere, but never gets declared; the error raised (undefined) will be displayed in the debugger along with the current value of the "result" which you can quickly see is undefined. If that didn't make sense, no worries, we will have a more in-depth example below which will explain more.</p>

<h2>History Lesson: Who the hell called it a debugger?</h2>

<img title = "Grace Hopper" align = "left" src="https://media.giphy.com/media/l378eVk7KxCOuJfPO/giphy.gif"/>
<figcaption>Programming Goddess Grace Hopper</figcaption>
<p>I'm a firm believer in understanding the past, to grasp on where you're trying to go. I'm not going to bore you to death on the history, but it's some key elements of the story of where the words "bug" and "debugger" you should know (it's programming folklore after all). In the olden days of vacuum tube computing you had machines taking up the size of rooms. As you can imagine, machines of this size could have some little intruders of things other than electronic parts attempt to make their way inside the inner-workings. So legend says that on September 9, 1945 a Harvard Tech noticed something unusual in one of these gigantic machines. Upon removal of the panel, a moth was found and promptly removed. Programming goddess and legend Grace Hoper then recorded in the notes "First actual case of bug being found", coining the first use of the word bug in computers. The subsequent word to follow, de-bug boils down to remove-bug, thus the use of the debugger is to remove bugs from your program. *WOOO* Now hopefully I didn't bore you too much with that history lesson, but now you have a piece of programming folklore to share at your next meetup with other nerds like ourselves.</p>

<h2>Great, now lets do the damn thing!</h2>
<img src = "https://media.giphy.com/media/E1Kd3pQwrsMtQbNkt0/giphy-downsized.gif"/>

<p>So for ease of access to everyone, I'm going to use the debugger available within the Google Chrome browser. Head on over to <a href ="https://github.com/js1599/Debugger-Learning-Material" target = "_blank">This Link</a> and download the training material to follow along. Be sure to NPM install everything before jumping into the next section</p>

<h2>So what's the game plan?</h2>

<p>The first step to understand where we're going is to understand the problem at hand. We're going to be using this dummy API from <a href = "https://jsonplaceholder.typicode.com/">HERE</a> that will allow us to do fake mocked API calls. At this time, go ahead and start the downloaded code (<code>npm run start</code>) from above. Notice on the page there's a button to get some data from our API defined above, along with a spot for the title that's returned from our API call. Go ahead and click on the button, and click it two more times as well. NOTHING HAPPENS, so we have a issue with the expectations and reality of our code. Using the Chrome browser, hit F12,select "sources", use the keyboard shortcut of "CTRL + p" and search for our App.js file. After recognizing our file from earlier now in the browser, you can start to add target lines in here known as "breakpoints" to get a better view of the issue at hand. A better explanation of a "break-point" is a chosen point within the code for the execution to "break" on, hence "break-point". Add the break point on the line involving the return of the data from the API (line 12), and click the button again. What just happened? You should notice the line you put your break-point on is highlighted, indicating the break-point selected earlier was "hit". Now within this mode you can do a few things, and will notice a few things with lots of data within them. 

<hr/>

<img src = "https://thepracticaldev.s3.amazonaws.com/i/93o4dhq9bw6xce7su3mg.JPG" title = "call stack"/>

On the right is the call stack of the application. As a stack (LIFO), the call stack holds the calls (execution) that were made up to the state of the current breakpoint. Translation: the call-stack is a map of the code your program ran up to the point it hit your break-point. Thus, using the call stack you're able to retrace the steps made by your program to investigate if the error could be prior than expected. Clicking on the files listed in the call stack will jump you into them for a closer investigation. You will also notice on the right the breakpoints you have setup, and the ability to toggle them on and off, as well as other attributes such as the watchers and global breakpoints (to name a few) which I can cover in a more advanced tutorial (just ask). For now, just be concerned with the call stack and breakpoints section.

<hr/>

Back to the breakpoint we hit earlier; hover over the "response" variable, notice what jumps out, it's the raw data being returned from the API. Seeing that the data is being returned with a valid <a href = "https://www.restapitutorial.com/httpstatuscodes.html" target = "_blank">200 status code</a>, we now have to use our debugger "step controls" to move closely to the error in order to prevent the error from happening again and us losing our place in the debug state.<img align = "left" src = "https://thepracticaldev.s3.amazonaws.com/i/y3l7cjt7yeo5x0tfu964.JPG" title = "debug on hover, raw response"/>

<hr/> 

<h3>Debugger step controls:</h3> 
<img title = "Debugger step controls" src = "https://thepracticaldev.s3.amazonaws.com/i/w13py1gmfzxucnmnt27g.JPG" /><br/>
<figcaption>You can find these in the right corner, above the call stack in order below.</figcaption>
<ul>
<li>Continue - Run the program until error/next breakpoint/completion</li>
<li>Step Over - You want to skip OVER the upcoming statement to be executed</li>
<li>Step In - You want to go INTO the statement about to be executed</li>
<li>Step Out - You screwed up by stepping into some s**t, and now wana go back up the stack</li>
<li>Step - You want to go to the upcoming call in the stack</li>

</ul>

<hr/>

 Now Step Over line 12, and investigate the data object being written to the State object, data property (line 13). Notice how if you hover over <code>json.tile</code> it comes back as <code>undefined</code>, but hover over the json variable on line 12. Notice that when hovering over it, you see no property labeled as "tile", but "title" instead. We have a typo of "title" being represented in our state object as "tile". 
<img align = "left" src = "https://thepracticaldev.s3.amazonaws.com/i/pj76uj0v8eo5pol5yv9q.JPG" title = "debug on hover, json variable"/>

<hr/>

<img src = "https://media.giphy.com/media/XreQmk7ETCak0/giphy.gif" title = "success!" />
So, hop back over to your code, and correct the typo, and run the code. IT WORKED! You've successfully debugged a program. That's all there is to it. Just remember to think of it in terms of physical stepping. Do you want to continue walking - Step, do you want to investigate what this evaluates more - step into, walked too far into what you're currently in - Step out, do you want to move over what's coming up - Step Over, and do you want to just run the whole way until another breakpoint, error, or full execution - Continue. But the debugger is good for more than fixing issues, you can also figure out how to improve/add features by using the debugger  </p>

<hr/>

<h2>Let's get creative.</h2>
<img src = "https://media.giphy.com/media/l0HlDtKDqfGGQtwic/giphy.gif" title = "lets amp it up"/>

<p>Say we need to add their street name, and geo-coordinates to the row, but are unsure on if the data even exists on our current route, and how the data is structured in the response. We can repeat our same debugging process from earlier by <ol><li>Add A break point once the response completes</li><li>Investigate the response</li><li>Investigate how the specific data we want is structured</li><li>Update your code based on your findings</li></ol> Voila! If your code is structured correctly, along with the correct results from your findings, then you should now have the geo-cords and street names on each button appropriately. If you want to try this, you can swap out the endpoint to <a href = "https://jsonplaceholder.typicode.com/users" target = "_blank">THIS</a></p>

<h2>Other Investigate Tools</h2>
<ul><li><a target = "_blank" href = "https://www.telerik.com/fiddler">Fiddler - To "man in the middle" network requests</a></li><li><a href = "https://www.getpostman.com/">Postman - To hit endpoints and investigate</a></li></ul> 

<h2>Other debuggers</h2>
<ul><li>All of the major broswers have a debugger included in their respective developer tools</li><li><a target = "_blank" href = "https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2017">Visual Studio - Included debugger in their IDE</a></li><li><a target = "_blank" href = "https://code.visualstudio.com/Docs/editor/debugging">VS-Code - Available via a plugin</a></li></ul> 

<hr/>

<h2>Thank You!</h2>

Thank you so much for reading my first, but not my last tutorial. I hope to create more in the future ranging from beginners to more advanced, and am always open to constructive feedback, suggestions, and to answer any questions you may have.


<img src = "https://media.giphy.com/media/J2oFWZdalFZmM/giphy-downsized.gif" title = "keep challenging yourself"/>