Preston Parry
Data Scientist, Machine Learning Engineer, addicted cyclist and rock climber
The super badass Admiral Grace Hopper found this moth stuck in a switchboard in an early supercomputer.
You should check her out. She's incredible.
Whenever your code isn't doing what you expect it to, and you spend time figuring it out, that's debugging.
You will do this a lot.
That's totally normal.
"I like debugging: it's the one time that hacking is as straightforward as people think it is. You have a totally constrained problem, and all you have to do is solve it. Your program is supposed to do x. Instead it does y. Where does it go wrong? You know you're going to win in the end. It's as relaxing as painting a wall."
- Paul Graham, http://www.paulgraham.com/hp.html"If something that seems like work to other people doesn't seem like work to you, that's something you're well suited for. For example, a lot of programmers I know, including me, actually like debugging... you may have to like debugging to like programming, considering the degree to which programming consists of it."
- Paul Graham, http://www.paulgraham.com/work.htmlWhat it's not:
"Everything's broken. I need to fix it all."
What it is:
"What specific line in my code isn't doing what I want it to?"
Your goal:
Reduce the problem from "ALL OF MY CODE IS BORKEN!!!!" to a single, solvable thing you can work on.
Reducing the problem to a single, solvable thing is much easier to do! All you need to do is find where your issue is.
Then from there, you'll gain the confidence that you can totally go off and figure out that single, solvable thing.
To the console!!!
Error messages are your best friend.
You will grow to love them for the useful information they give you.
Whenever I get a new error message, I get a bit excited because the computer just gave me more information to solve this bug!
Write your code in the .js files that are saved on your computer.
Write console.log('hi there'); statements inside your code that is saved in those .js files.
Open up those files in the browser (hint: we're doing this for you automatically when you open up that basic index.html file).
Open up the console in that browser window.
Repl.it, jsbin, and other environments. They oftentimes require extra keystrokes, oftentimes don't throw the same error messages, and are less useful than the tools available in the console as you work on more and more complex things.
Writing code directly in the console- write it in your .js files.
These things will work, and were good ways to start getting into programming, but you're advanced enough now that it's worth doing it in your .js files and the console.
The console will tell you exactly which line is throwing the error.
See, our computer's being nice to us!
What type of error is this? //SyntaxError
What exactly is broken? //Unexpected identifier
Where is it broken? //collectingRelationships.js file, line 42
THIS IS ALL YOU NEED. IT’S LITERALLY TELLING YOU EVERYTHING.
TRUST THE CONSOLE.
Sometimes this screen gives you more info.
Take this info back to your .js file and make updates there.
Here's how to set up your debugging process the first time:
1. Write code in a file on your computer
2. Save code
3. Switch over to your browser
4. Make sure console is open
5. Refresh page
Here's what you'll actually be doing on each iteration:
Command + S
Command + Tab
Command + R
Not sure what's going on? console.log it!
(with a comment)
Many new developers think they can just look at code and fully understand it, and know exactly what each variable will be at all points in time.
That sounds like a lot of work.
Make it easy for yourself- just investigate and console.log things to the console as you go to figure out what you're working with!
You guys should be pretty familiar with this from our loops exercises last week- keep up that same pattern of logging things to the console constantly to see what they are.
You're going to be console.logging many things. Make it clear what each one is by adding in a comment/label describing what it is:
console.log('name inside nameGetterFunc:',name);
You don't have to be creative, just tell yourself two things:
1. The variable name that you're logging
2. Where you are in your code
Note: the '+' operator in JavaScript will force the thing after the string to be a string as well.
The ',' operator will allow it to still be an object that you can investigate.
console.dir(pirateTreasureChestObject);
console.dir() just gives you more ability to explore objects and functions and arrays.
It takes the training wheels off and shows you everything- even the things that console.log hides for you by default.
Rule of thumb: ignore everything in that weird light pinkish color. This is mostly just stuff JS provides for you by default. The stuff in that darker purple color is likely more useful.
Remember what our success metric was from week 1?
"How many times you logged something to the console and got a new result."
Test your code rapidly. We expect it to be wrong more often than correct. Testing your code rapidly and seeing what the latest results are will get you to being correct most quickly.
The exercises are a review from last week and are not directly linked to the lecture material we covered today.
https://github.com/TelegraphPrep/week2
By Preston Parry
An intro to debugging for Telegraph Prep
Data Scientist, Machine Learning Engineer, addicted cyclist and rock climber