What Is Debugging?
- Wikipedia: "debugging is the process of finding and resolving bugs (defects or problems that prevent correct operation) within computer programs, software, or systems.
Why is it broken, and how do we fix it?
Why Is Debugging Important?
- Programming isn't just writing code
- Planning / design
- Communication
- Review
- And figuring out why something isn't working
Developers spend significant time debugging and fixing code...
and yet many devs are uncomfortable or not confident doing this!
- Why don't developers know how to debug?
- Doesn't seem to be taught in schools - 学校没教
- Mostly comes from experience - 主要来自于经验
- Typically learned "the hard way" as you go - 通常是边走边学的 "硬道理"
Debugging is a critical skill for devs...and you can learn how to get better at it!
Core Principles of Debugging - 调试的核心原则
Every problem has a cause, and it should be possible to figure out why things are broken - 每一个问题都有一个原因,应该可以找出事情坏掉的原因
- Not necessarily easy to do this :) - 要做到这一点不一定容易
- Potential problems: non-deterministic behavior, hard to reproduce, difficult environment - 潜在的问题:非决定性的行为,难以复制,困难的环境
It's critical to understand the system's intended behavior and what should be happening - 了解系统的预期行为和应该发生的事情是至关重要的
- A bug is "the system not working as expected" - bug就是"系统没有按照预期工作"
- Have to know how it should behave to identify what's different and incorrect! - 必须知道它应该如何表现,以确定什么是不同的和不正确的!
Reproducing the issue is key! - 重现这个问题是关键!
- Helps confirm where the problem is happening - 有助于确认问题发生的位置
- Allows further investigation of the actual behavior - 允许对实际行为进行进一步调查
- Lets you repeat the process to verify a fix actually does resolve the problem - 让你重复这个过程,以验证一个修复方案确实解决了问题
Debug with a plan - 有计划地进行调试
- Scientific method: form a hypothesis, experiment, check results - 科学方法:形成假设、实验、检查结果
- Be intentional about what changes you make - 对你所做的改变要有意向性
- Only change one piece of behavior at a time! - 每次只改变一项行为!这是最重要的!
- Critical to help determine if a change helped or hurt - 确定某个变化到底是好还是坏 是很关键的
- Narrow down possible causes - 缩小可能的原因
Errors provide useful information - 错误提供了有用的信息
- Error messages and stack traces are almost always helpful - 错误信息和堆栈跟踪几乎总是有帮助的
- Where did the error occur? - 错误发生在哪里?
- What was the first cause? - 第一个原因是什么?
- When did this happen? - 这是什么时候发生的?
- Identify the most relevant piece of the error and search for that - 确定最相关的错误部分并进行搜索
- "How to Google Your Errors" has good tips - "如何谷歌你的错误 "有很好的提示