You’re staring at code and can’t figure out why! WHY is it doing this unexpected, potentially interesting — if you didn’t have a deadline — thing?
Before you abandon your approach and resort to cobbling together somewhat-related found code that might get the job done, I have three questions for you:
1. What do you expect that first expression to do?
2. Why do you expect it to do that?
3. Does it actually do it?
If you can’t answer the first two questions, I wish you luck with your copy-paste hack, BUT if you know what you expect and why, there are tools to help you figure out IF it is doing the thing.
After you confirm that your expectation is spot on—or fix a bug—move to the next logical expression or line to check. Here are a few helpful tools to aide you in bug sleuthing.
When you need to verify a value
Console.log may be your go-to to check a var, const, let, argument or this value, which is adequate at times, but be aware that sometimes Chrome’s console displays a value updated after its runtime evaluation. Be leery of the accuracy if you see a little light-blue box with an ‘i’ next to the logged value.
A more dependable alternative to console.log-ing is Chrome’s debugger tool. To use it, add a debugger statement in your code at the line where you would like to inspect values. Save the file, then open the Dev Tools panel in your Chrome browser with:
iOS: Cmd + Opt + I
Windows: Ctrl + Shift + I
Navigate to the page where you are testing your code, likely localhost:PORT_NUMBER or the deployed dev site, and reload the page. Execution will pause at the debugger allowing you to inspect values.
If your application is built with React and you need to check props or state values, the React Developer Tools Chrome extension becomes your best friend, momentarily.
Once you add the extension, if you visit a webpage build with React, a React tab will be added to the dev console that displays props and state values for any element you click on.
When you need to check if a request was sent
If you are not sure if your request made it to the server, head to the Chrome Dev Tools network tab. Look in the call list for the request you are not sure about. You can check the status code and click on the request to view headers and other info.
When you need to debug server-side code
You are confident that you are sending data to the server, but you are not sure how Node or Express is routing and processing the request. If you are using a application you can’t peer into, like an API, read its documentation. If you built or maintain the server in question, meet Node Inspect.
4. Node Inspect
Node Inspect is like Chrome Dev Tools for server code. Before using inspect, make sure that you have node 6.6+ installed and are surfing on Chrome 55+. If you can check off those two prereqs, open a bash and run:
node --inspect PATH_TO_YOUR_SERVER_FILE
You should see something like the image below with a connection URL.
Open that link in your browser for a pleasantly familiar interface to explore the inner workings of your server.
When you need to check a server response
If you know that your request made it to the server, but you aren’t sure what the data coming back looks like—or if there is data coming back at all, Postman is your man. Though he doesn’t quite have Marvel status, he has saved several devs.
Postman is an app that you download and install. The app gives you the option to pick a request method (GET, POST, PUT, PATCH, DELETE…), paste an endpoint, add any auth credentials required by the endpoint, and send the request. The response is displayed in the app’s body tab.
This tool is a great to make sure you are getting back the info you expect so you know the function in your client that handles the server response can process the data. More helpful Postman tips can be found in the docs, https://www.getpostman.com/docs.
When you have a syntax error
A text editor linter like ESLint is quite helpful. The often bright-red logged errors in browser and bash consoles also provide great clues to pay attention to and use as search queries. Another helpful but less referenced syntax debugger is Webpack. Yes, the same Webpack that you use to compile modules, https://webpack.js.org/guides/getting-started/
If Webpack can’t build, it will throw an error that shows a lot of helpful details. So if something doesn’t update in the browser when you refresh the page, check the Webpack bash to make sure it is compiling.
If the bug persists
If you still have a bug, formulate a concise, well factored question to ask a mentor or coworker, or to post in a dev forum—OR travel to a city showcasing the work of Dutch artist, Florentijn Hofman (pictured below in Ahmad Nassri’s tweet).