Whenever I see someone really effectively debug JavaScript in the browser, they use the DevTools tooling to do it. Setting breakpoints and hopping over them and such. That, as opposed to sprinkling console.log()
(and friends) statements all around your code.
Parag Zaveri wrote about the transition and it has clearly resonated with lots of folks! (7.5k claps on Medium as I write).
I know I have hangups about it...
- Part of debugging is not just inspecting code once as-is; it's inspecting stuff, making changes and then continuing to debug. If I spend a bunch of time setting up breakpoints, will they still be there after I've changed my code and refreshed? Answer: DevTools appears to do a pretty good job with that.
- Looking at the console to see some output is one thing, but mucking about in the Sources panel is another. My code there might be transpiled, combined, and not quite look like my authored code, making things harder to find. Plus it's a bit cramped in there, visually.
But yet! It's so powerful. Setting a breakpoint (just by clicking a line number) means that I don't have to litter my own code with extra junk, nor do I have to choose what to log. Every variable in local and global scope is available for me to look at that breakpoint. I learned in Parag's article that you might not even need to manually set breakpoints. You can, for example, have it break whenever a click (or other) event fires. Plus, you can type in variable names you specifically want to watch for, so you don't have to dig around looking for them. I'll be trying to use the proper DevTools for debugging more often and seeing how it goes.
While we're talking about debugging though... I've had this in my head for a few months: Why doesn't JavaScript have log levels? Apparently, this is a very common concept in many other languages. You can write logging statements, but they will only log if the configuration says it should. That way, in development, you can get detailed logging, but log only more serious errors in production. I mention it because it could be nice to leave useful logging statements in the code, but not have them actually log if you set like console.level = "production";
or whatever. Or perhaps they could be compiled out during a build step.
Direct Link to Article — Permalink
The post How to stop using console.log() and start using your browser’s debugger appeared first on CSS-Tricks.
from CSS-Tricks https://ift.tt/2NrC4SV
via IFTTT
No comments:
Post a Comment