4 ms·
> Using console.log() for JavaScript debugging is the most common practice among developers. But, there is more… Cut your debugging time, knowledge of console.
by yepthatsreality 6y ago
> Using console.log() for JavaScript debugging is the most common practice among developers. But, there is more…
Cut your debugging time, knowledge of console.log, and mental churn in half and set up your tooling to use a `debugger` statement. The console.log method may be used heavily but it’s actually a bad practice and often leaves code littered with log statements. Even for the purpose of logging itself you should use a logging library for serious development.
You should use a debugger in every language you can for development.
Neat tricks though.
- ehsankia 6y agoIf you want to quickly log stuff without adding a log statement, you can right click the line gutter in sources and add a logpoint. Similar to a breakpoint, but logs every time it hits that line. Not modifying the source code so you don't have to remember about removing it.
- ksenzee 6y agoBrowser debuggers are amazing. I've been using them since Venkman. There's no substitute for setting a breakpoint, via the UI or a `debugger` statement, and watching your code execute a line at a time, in context. But that doesn't make console.log() a bad practice! The console is a great tool I am glad to have, especially when I remember the alternative, which was calling alert() and getting [object Object] and wanting to throw your desktop tower out the nearest window.
- yepthatsreality 6y agoUse console.log for it's intended use, formatting string output, but for debugging purposes, it is a bad practice because the reason it's used usually do not usually justify the sloppy unfocused mess it leaves behind. A poorly written log statement is tech debt, as well. Debugger provides all the benefits one would expect with console.log usage: 1. You can see where you are at that moment of execution in code 2. You can see variables and arguments within scope 3. You can forget about it and a linter will pick it up or it will be ignored unless run in debugging mode All of this allows you to focus on the actual bug and not looking for something like: `MY SPECIAL VAR: [object Object]` I forgot to format, let's run it again.
- marshmellman 6y agoWhat about your workflow causes console.log statements to litter the code but not debugger statements? There are fewer of them?
- marshmellman 6y agoI find use cases for both. Sometimes I want to run code and review the generated debug log, other times I want to step through interactively. I find interactive debugging takes more concentration than a workflow of: form hypothesis, add the console.logs to prove/disprove it, run the code, and analyze the result.