文档章节

Great Programmers Write Debuggable Code

卢中强
 卢中强
发布于 2014/03/25 17:24
字数 795
阅读 7
收藏 0

All programs need some form of logging built in to them, so we can observe what it is doing. This is especially important when things go wrong. One of the differences between a great programmer and a bad programmer is that a great programmer adds logging and tools that make it easy to debug the program when things fail.

When the program works as expected, there is often no difference in the quality of the logging. However, as soon as the program fails, or you get the wrong result, you can almost immediately tell the good programmers from the bad.

EXAMPLE 1: “LET’S MAKE A DEBUG VERSION”

For example, a tester came to me with a call case that didn’t work. We looked at the logs, and saw that the problem seemed to be in a neighboring module. A call to the other module to get a list of values simply returned null. When we enabled logging in the neighboring module and re-ran the test case, there was no more information available. No clues to why null was returned – did we supply the wrong arguments, did some external system fail, was there an error in the neighboring module, or what?

When asking the developer responsible for the code, the answer we got was: “Oh, then we have to make a debug version to see what happens”. Fail! It must be possible to get some sense of where the problem is just from the logs. If this had been in a production system, adding a debug version would have been a lot of work. The code needs to include enough information in the log statements so you at least have some idea of why it fails.

EXAMPLE 2: SHOW ME HOW WE GOT HERE

One of our products at work finds the cheapest route for the delivery of SMS:s (text messages) to mobile phones. Depending on the current location of the phone, and the operator the destination subscriber belongs to, there are many possible routes to choose from, each with a given cost and other characteristics.  In addition, there can be exceptions that forbid some routes, and promote other routes. There are typically many thousands of routes defined. The system finds the cheapest route in each case, given the constraints, and delivers the SMS.

Now, suppose a certain SMS gets delivered using route A, but we believe it should have used route B. Why was route A chosen? If there is no logging information (other than “route A was chosen”), we are left with hundreds of possible routes, their costs, the exceptions, and a complex algorithm. Good luck figuring out why A was chosen.

In our implementation, the log includes all potential routes, in cost order. As routes are eliminated due to different restrictions, the eliminated routes are listed in the log, together with the reason for elimination. With all the information on the input to the algorithm, as well as the steps taken, listed in the log, it is fairly easy to see why a certain route was picked.

WHY NOT?

So, why don’t all programmers write debuggable code? I can think of three reasons:

  1. You have to be humble enough to realize that there will be cases when your code doesn’t work as expected. I believe many programmers have a hard time with this.
  2. If you test your own code thoroughly, you will make sure it works (or fails gracefully) in a lot of different scenarios. For each scenario, it is natural to add logging. If you don’t test those cases, you are less likely to add logging there.
  3. Many programmers don’t trouble-shoot their own code in production systems often enough. If you have a problem in a live system, and the logs tell you nothing about why there is a problem, you have a strong incentive to add logging that will actually help you the next time you are in a similar situation.

IS YOUR CODE DEBUGGABLE?

There are of course cases where even good logging messages don’t give you the full story on why something fails. You may still have to make that debug version. But your regular logging should at the very least give you some hints on what the problem might be.

So, how well have you prepared? When your code fails, will the logs tell you what’s going on?

本文转载自:http://henrikwarne.com/2013/05/05/great-programmers-write-debuggable-code/

共有 人打赏支持
上一篇: 杂记
下一篇: 小技巧
卢中强

卢中强

粉丝 1
博文 10
码字总数 1048
作品 0
昌吉
程序员
私信 提问
https://st5.findjobs.me/en/img/candidate-1588726-2

The Seven Mistakes Focusing on years of experience. Trust peoples own assessment of their skill. Don’t ask the candidate to write code. Recruiting for “the other team”. Be fo......

perfectspr
2014/11/26
0
0
Why Do Programmers Fail to Write Good Unit Tests?

We programmers are full of opinions when it comes to unit testing. We don't always agree about the importance of unit testing or what role it should play. There is also a lot of......

Leah Grantz
2017/12/27
0
0
All Programmers Are Self-Taught -----by Jonathan

All Programmers Are Self-Taught by Jonathan When I was a teenager I played high caliber baseball. I’m competitive to a fault and when I decide I want to be good at something, ......

TaciturnKnightYQ
2015/12/09
20
0
8 Python Frameworks For Web Developers

Python has become immensely popular inthe modern IT world. The language is most popular for its efficiency. Itis also known as the best beginner’s learning language. The primer......

linjine
2015/05/03
187
1
Why Can't Programmers.. Program?

I was incredulous when I read this observation from Reginald Braithwaite: Like me, the author is having trouble with the fact that 199 out of 200 applicants for every programmin......

Josh.Zhao
2012/06/27
16
0

没有更多内容

加载失败,请刷新页面

加载更多

用PyTorch创建一个图像分类器?So easy!(Part 1)

摘要: 本文将为你介绍为何要重用神经网络?哪部分可以重用,哪部分不可以重用。了解完这些基础概念,你就可以自行创建一个图像分类器了。 经过了几个月的学习和实践,我完成了优达学城网站上...

阿里云官方博客
14分钟前
0
0
ssh使用正确的密码登录服务器被拒绝

1、用一个普通用户登录服务器被拒绝。 2、在服务器上,tail -f /var/log/secure, 看到: Dec 19 11:03:20 mmi5 sshd[11126]: pam_tally2(sshd:auth): user carrot (1003) tally 144, deny 3 ......

gelare
14分钟前
0
0
基于腾讯AI Lab词向量进行未知词、短语向量补齐与域内相似词搜索

AI Lab开源大规模高质量中文词向量数据,800万中文词随你用,质量非常高,就是一个词向量.txt文件都有16G之多,太夸张了。。不过的确非常有特点: ⒈ 覆盖率(Coverage): 该词向量数据包含...

火力全開
17分钟前
0
0
Shiro简介——《跟我学Shiro》

1、《跟我学Shiro》PDF完结版下载 2、shiro简介——《跟我学Shiro》 3、shiro demo

近在咫尺远在天涯
18分钟前
0
0
教你一个vue小技巧,一般人我不说的

本文由云+社区发表 1. 需求 最近的项目中,需要实现在vue框架中动态渲染带提示框的单选/多选文本框,具体的效果如下图所示,在输入框聚焦时,前端组件通过接收的kv参数渲染出选项,用户点击选...

腾讯云加社区
21分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部