Great Programmers Write Debuggable Code

发布于 2014/03/25 17:24
字数 795
阅读 7
收藏 0
点赞 0
评论 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.


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.


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.


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.


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?


共有 人打赏支持


粉丝 1
博文 10
码字总数 1048
作品 0

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

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

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 ⋅ 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 ⋅ 1


Documentations Python Documentations The Python documentations are pretty much a must-read for those who want to learn the language, so Whether you're planning to use Python 3.x......

好铁 ⋅ 2016/01/22 ⋅ 0

Top 10 React Libraries on GitHub to Follow

Created by Facebook, React has gone on to become a really powerful JavaScript framework, making the job of front-end development easier for programmers. If you have spent some t......

Anton Shaleynikov ⋅ 2017/12/13 ⋅ 0


The following is from an EMail message that I sent to to an individual on 12-Apr-2001. You may find this EMail message useful. The Writer asked: Here is my reply: Thank you for ......

凌晨4点半 ⋅ 2014/10/27 ⋅ 0

When Will 100% Remote Be an Accepted Norm?

I am a member of a few different Slack channels. Recently, one of those channels featured the topic of working remote and how the idea of being 100% remote is still a foreign co......

John Vester ⋅ 2017/11/22 ⋅ 0


Robert's Rules (Of Coding)I have gathered these rules through several years of programming. Some of them come directly from my own experience, and some come from the experiences......

晨曦之光 ⋅ 2012/05/21 ⋅ 0

《Team Geek》前言(中文,自己翻译的,很不错的一本书)

Introduction 前言 “Engineeringis easy. People are hard.” ——BillCoughran, former senior vice presidentof engineering at Google “做工程容易,做人难。” ——BillCoughran,谷歌......

snsn1984 ⋅ 2012/11/08 ⋅ 3






安装Firewall命令: yum install firewalld firewalld-config Firewall开启常见端口命令: firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --zone=public --add-po......

cavion ⋅ 51分钟前 ⋅ 0


直接上代码吧,没啥好说的。头疼。 #include <iostream>#include <string>#include <ctime>#include <sstream>#include <iomanip>#include <thread>#include <chrono>using ......

muqiusangyang ⋅ 54分钟前 ⋅ 0



故久呵呵 ⋅ 今天 ⋅ 0


本周四,苹果发表声明称 iOS 中加入了一项名为“USB 限制模式”的功能,可以防止 iPhone 在连接其他设备的时候被破解,并且强调这一功能并不是针对 FBI 等执法部门,为的是保护用户数据安全。...

六库科技 ⋅ 今天 ⋅ 0

MyBtais整合Spring Boot整合,TypeHandler对枚举类(enum)处理

概要 问题描述 我想用枚举类来表示用户当前状态,枚举类由 code 和 msg 组成,但我只想把 code 保存到数据库,查询处理,能知道用户当前状态,这应该怎么做呢?在 Spring 整合MyBatis 的时候...

Wenyi_Feng ⋅ 今天 ⋅ 0


# <center>王梦龙的读书笔记第一篇</center> ## <center>-synchronized与Lock的区别</centre> ###一、从使用场景来说 + synchronized 是能够注释代码块、类、方法但是它的加锁是和解锁使用一......

我不想加班 ⋅ 今天 ⋅ 0


手机端控制台打印输出,方便bug的排查。 首先需要引入vconsole.min.js 文件,然后在文件中创造实例。就能直接使用了。 var vConsole = new VConsole(); vConsole的文件地址...

大美琴 ⋅ 今天 ⋅ 0

Java NIO之字符集

1 字符集和编解码的概念 首先,解释一下什么是字符集。顾名思义,就是字符的集合。它的初衷是把现实世界的符号映射为计算机可以理解的字节。比如我创造一个字符集,叫做sex字符集,就包含两个...

士别三日 ⋅ 今天 ⋅ 0

Spring Bean基础

1、Bean之间引用 <!--如果Bean配置在同一个XML文件中,使用local引用--><ref bean="someBean"/><!--如果Bean配置在不同的XML文件中,使用ref引用--><ref local="someBean"/> 其实两种......

霍淇滨 ⋅ 今天 ⋅ 0


1、Consul环境搭建 下载consul_0.7.5_linux_amd64.zip到/usr/local/src目录 cd /usr/local/srcwget https://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip 解压consu......

北岩 ⋅ 今天 ⋅ 0