通过添加“打印"语句进行调试的正确名称是什么 [英] What is the proper name for doing debugging by adding 'print' statements

查看:28
本文介绍了通过添加“打印"语句进行调试的正确名称是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试的方法有很多种,使用调试器就是其中一种,但对于谦虚、懒惰的程序员来说,最简单的方法就是在代码中添加一堆打印语句.

 def foo(x):打印 '嘿哇,我们要去 foo!', x...打印 'foo 正在返回:', bar返回栏

这种调试方式有合适的名称吗?

解决方案

是的 - 它被称为 printf() 调试,以无处不在的 C 函数命名:><块引用>

使用描述由完成的调试工作插入输出更多或的命令不太仔细选择的状态关键点的信息程序流程,观察信息并推断出问题所在基于这些信息.

-- printf() debugging@everything2

其他语言的本地用户无疑会通过可用于他们选择的编码平台的默认打印/日志/或跟踪命令来引用它,但我听说过printf()"名称用于在除了 C 之外的许多语言.也许这是由于它的历史:虽然 BASIC 和 FORTRAN 有基本但有用的 PRINT 命令,但 C 通常需要更多的工作来格式化各种数据类型:printf() 是(而且通常仍然是)到目前为止最方便的方法,它提供了许多内置的格式选项.它的表亲 fprintf() 接受另一个参数,即要写入的流:这允许仔细的调试器"将诊断信息定向到 stderr(可能本身重定向到日志文件),同时保持程序的输出不被破坏.

虽然经常被现代调试软件的用户看不起,但 printf() 调试继续证明自己是必不可少的:广受欢迎的用于 Firefox 网络浏览器的 FireBug 工具(以及现在可用于其他浏览器的类似工具)是围绕控制台构建的网页脚本可以在其中记录包含格式化数据的错误或诊断消息的窗口.

There are many ways of doing debugging, using a debugger is one, but the simple one for the humble, lazy, programmer is to just add a bunch of print statements to your code.

i.e.

 def foo(x):
     print 'Hey wow, we got to foo!', x

     ...

     print 'foo is returning:', bar
     return bar

Is there a proper name for this style of debugging?

解决方案

Yes - it's known as printf() debugging, named after the ubiquitous C function:

Used to describe debugging work done by inserting commands that output more or less carefully chosen status information at key points in the program flow, observing that information and deducing what's wrong based on that information.

-- printf() debugging@everything2

Native users of other languages no doubt refer to it by the default print / log / or trace command available for their coding platform of choice, but i've heard the "printf()" name used to refere to this technique in many languages other than C. Perhaps this is due to its history: while BASIC and FORTRAN had basic but serviceable PRINT commands, C generally required a bit more work to format various data types: printf() was (and often still is) by far the most convenient means to this end, providing many built-in formatting options. Its cousin, fprintf(), takes another parameter, the stream to write to: this allowed a careful "debugger" to direct diagnostic information to stderr (possibly itself redirected to a log file) while leaving the output of the program uncorrupted.

Although often looked down on by users of modern debugging software, printf() debugging continues to prove itself indispensable: the wildly popular FireBug tool for the Firefox web browser (and similar tools now available for other browsers) is built around a console window into which web page scripts can log errors or diagnostic messages containing formatted data.

这篇关于通过添加“打印"语句进行调试的正确名称是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆