2010-08-05

为他人惠

最近业余在翻译一篇教程“Understanding Haskell Monads”,取得了原作者的同意后准备将其译为中文,现在是用xelatex排版的,放在了github里。对于没有TeX环境的同学,可以直接阅读生成的PDF文档(会不定期更新)。

本人对于翻译没有什么经验,特别是对于一些专业术语,踟蹰良久,自己重读的时候有些地方也感觉多少有点拗口。如果有表达不清楚或者不准确的地方,欢迎拍砖。

标签: ,

2010-08-03

How to make debugging easier

本文系为组内NewsLetter创刊所写,属于“Experience Sharing”一节。本文不算是个正儿八经的东西,缺了点插图,多了些罗嗦。

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”

– Brian Kernighan

So, what is debugging?

“The process of identifying divergences between the intent and the implementation is known as debugging. It is hard to do this if the intent is not known.”

– Robert Love, “Linux Kernel Development”

Among citations about debugging, the above two are my favorite. I myself learned the following lessons and glad to share with you.

  1. Clean and simplicity is the most crucial elements for writing good software. Only with a clean interface and abstraction layer we can make our intention clear, thus easy for maintenance and debugging. This is essentially the first step toward reliable software.
  2. Write test code to express intention. Each test case is a precise definition of an interface, and they can help produce regression bugs when developers are in continuous integration or after refactoring. Update test case along with a bug fix.
  3. Test your code as soon/much as possible. Test cases mostly are dealing with logic defects, while there are other factors of good software: memory usage, code efficiency and security concerns etc. Use all the tools you know to test and profile your code, before its releasing to QA engineer, at least before its deploy in end-user environment.
  4. Make invisible things visible. Log crucial actions in your code. One of my tips for debugging static compiled language such as C/C++ is to add some debug functions in the code which is surrounded by #ifdef DEBUG and #endif. For example, if there is a ring buffer shared by multi-threads, and you can add an print_ringbuf(), and use “call print_ringbuf()” in GDB to dump it.
  5. Manage your code with a VCS tool. I personally prefer Hg and Git, since they provide a fast `grep’ command to lookup the whole repository. Also it’s easy and fast to do a diff between different change-sets or branches.

Last but not least, I’d like to say that do NOT rely on debugging techniques! Instead, we should try the best to write comprehensive software and avoid debugging. Imagine you will never have a debugger and “always program as if the next person who will maintain your code was a psychopath killer that knows where you live”. I believe it will make you a better programmer!

标签:

2010-07-30

ntpdate

前些日子 qhe 说我在 github 上的 commit 时间有点错乱,我看了一下果然如此。三台虚拟机中最离谱的是我笔记本上vmware workstation里跑的 ArchLinux,`date’ 命令的显示结果和真实时间差了几乎两天。

网上搜了一下,vmware 官方有份文档写的很赞:
Timekeeping in VMware Virtual Machines

我的解决方法很省事,直接用ntp吧:


yaourt -S ntpdate

sudo ntpdate pool.ntp.org

标签:

勇敢的虾

晚餐图省事,买了点基围虾。看着它们一个个活蹦乱跳的不想安分的呆在盆子里,于是我就在上面罩了一只塑料漏筛。没想到居然镇不住这些小东西。

丢了葱姜的水慢慢煮沸,翻过来拿起漏筛望锅里倒。虾之将死,其跳也快。没想到有一只居然凌空一个弧线跳在锅外。我一把抓住望锅里扔,在丢入的一刹那,它用力掀起一尾沸水,精准的打中我的右手臂。勇哉,斯虾!

有打破牢笼的强烈欲望,有求生的坚决意志,有玉石俱焚的无畏精神。吾为人尚不如虾。

标签:

2010-07-28

llscheme – 3 – interpreter

qhe 同学为 llscheme 加 了一个 interpreter 接口,安装好之后 `llscheme -i’ 即可进入解释器。然而在开发过程中间碰到了一个有趣的问题:我们有个lsrt_error(),调用它会导致进程结束。这对于编译出来的代码是没问题的, 然而对于解释器,大家可能希望它打印错误信息后能继续工作。

解决该问题的办法是在lsrt_error()调用exit(-1)之前插入一个钩子函数,并且该钩子函数式个弱符号(weak symbol),我们只在调用解释器的时候定义该钩子函数:


extern "C" void lsrt_exit_hook() { throw Error(""); }

函数lsrt_exit_hook()假模假式的抛了个异常,避免了对exit(-1)的调用,而解释器只要捕获改异常,然后继续REPL即可。完整补丁:

git diff 1a6bd0..6749de

标签: ,