Debugging

Like testing, debugging isn't a way to improve the quality of your software; it's a way to diagnose defects.

Effective approach for finding a defect:
1. Stabilize the error.
2. Locate the source of the error.
 a. Gather the data that produces the defect.
 b. Analyze the data that has been gathered and form a hypothesis about the defect.
 c. Determine how to prove or disprove the hypothesis, either by testing the program or by  examining the code.
 d. Prove or disprove the hypothesis using the procedure identified in 2(c).
3. Fix the defect.
4. Test the fix.
5. Look for similar errors.

Tips for finding defects:
1. Use all the data available to make your hypothesis. When creating a hypothesis about the source of a defect, account for as much of the data as you can in your hypothesis
2. Refine the test cases that produce the error. If you can't find the source of an error, try to refine the test cases further than you already have.
3. Exercise the code in your unit test suite. Defects tend to be easier to find in small fragments of code than in large integrated programs.
4. Reproduce the error several different ways. Sometimes trying cases that are similar to the error-producing case, but not exactly the same, is instructive.
5. Generate more data to generate more hypotheses. Choose test cases that are different from the test cases you already know to be erroneous or correct.
6. Use the results of negative tests. Suppose you create a hypothesis and run a test case to prove it. Suppose the test case disprove the hypothesis, so that you still don't know the source of the error. You still know something you didn't before, that the defect is not in the area in which you thought it was.
7. Brainstorm for possible hypotheses. Rather than limiting yourself to  the first hypothesis you think of, try to come up with serveral.
8. Narrow the suspicious region of the code. If you've been testing the whole program, or a whole class or routine, test a smaller part instead.
9. Be suspicious of classes and routines that have had defects before. Classes that have had defects before are likely to continue to have defects.
10. Check code that's changed recently. If you have a new error that's hard to diagnose, it's ususlly related to code that's changed recently.
11. Expand the suspicious region of the code.
12. Integrate incrementally. Debugging is easy if you add pieces to a system one at a time.
13. Check for common defects. Use code-quality checklists to stimulate your thinking about possible defects.
14. Talk to someone else about the problem. Some people call this "confessional debugging." You often discover your own defect in the act of explaining it to another person.
15. Take a break from the problem. Sometimes you concentrate so hard you can't think. 

Fixning a defect
1. Understand the problem before you fix it. The best way to make your life difficult and corrode the quality of your program is to fix problems without really understanding them.
2. Understand the program, not just the problem. If you understand the context in which a problem occurs, you're more likely to solve the problem completely rather than only one aspect of it.
3. Confirm the defect diagnosis. Before you rush to fix a defect, make sure that you've diagnosed the problem correctly.
4. Relax.
5. Save the origianl source code. You should fix the symptom too, but the focus should be on fixing the underlying problem rather than wrapping it in programming duct tape.
6. Change the code only for good reason.
7. Make one change at a time.
8. check your fix. Check the program yourself, have someone else check it for you, or walk through it with someone else.
9. Look for similar defects. When you find one defect, look for others that are similar.

### 三级标题:Print Debugging 的定义与使用方法 在软件开发中,**print debugging** 是一种基础但广泛应用的调试技术,其核心思想是通过在代码中插入打印语句(如 `print()` 函数),输出程序运行时的变量值、执行流程或状态信息,以帮助开发者理解程序行为并定位问题[^1]。 这种调试方式虽然简单,但在缺乏高级调试工具或调试环境受限的情况下,依然是一种有效的排查手段。例如,在 Python 开发中,开发者可以在代码中插入 `print()` 语句来输出变量值或执行路径,从而观察程序的运行状态: ```python def add(num1=0, num2=0): print(f"Adding {num1} and {num2}") # 输出调试信息 return int(num1) + int(num2) def sub(num1=0, num2=0): print(f"Subtracting {num2} from {num1}") # 输出调试信息 return int(num1) - int(num2) def main(): num1 = 28 num2 = 8 addition = add(num1, num2) print(f"Addition result: {addition}") subtraction = sub(num1, num2) print(f"Subtraction result: {subtraction}") if __name__ == '__main__': main() ``` 上述代码中,通过 `print()` 输出关键变量和操作信息,可以辅助理解程序执行流程,并验证函数逻辑是否符合预期。这种方式在早期的调试实践中被广泛使用,尤其适用于没有集成调试器的环境或快速查看某些变量状态的情况。 尽管 print debugging 无法提供如断点控制、变量监视等高级功能,但在一些轻量级场景中,它仍然是开发者首选的调试方式之一。此外,print debugging 的优势在于其简单性和通用性,几乎适用于所有编程语言和平台。 ### 三级标题:Print Debugging 的局限性 尽管 print debugging 是一种简单易用的调试方式,但它也存在一定的局限性。例如,频繁的打印输出可能导致日志信息过多,难以快速定位关键问题。此外,每次修改调试信息都需要重新运行程序,这在调试复杂流程或难以重现的问题时可能带来不便[^3]。 为了解决这些问题,开发者通常会结合使用日志记录工具(如 Python 的 `logging` 模块)来替代简单的 `print()` 调用,从而实现更灵活的调试信息控制和输出管理。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值