TranslateProject/sources/tech/20190322 Easy means easy to debug.md
darksun bd5dd5d245 选题: 20190322 Easy means easy to debug
sources/tech/20190322 Easy means easy to debug.md
2019-03-25 12:56:16 +08:00

5.4 KiB
Raw Blame History

What does it mean for a framework, library, or tool to be “easy”? There are many possible definitions one could use, but my definition is usually that its easy to debug. I often see people advertise a particular program, framework, library, file format, or something else as easy because “look with how little effort I can do task X, this is so easy!” Thats great, but an incomplete picture.

You only write software once, but will almost always go through several debugging cycles. With debugging cycle I dont mean “there is a bug in the code you need to fix”, but rather “I need to look at this code to fix the bug”. To debug code, you need to understand it, so “easy to debug” by extension means “easy to understand”.

Abstractions which make something easier to write often come at the cost of make things harder to understand. Sometimes this is a good trade-off, but often its not. In general I will happily spend a little but more effort writing something now if that makes things easier to understand and debug later on, as its often a net time-saver.

Simplicity isnt the only thing that makes programs easier to debug, but it is probably the most important. Good documentation helps too, but unfortunately good documentation is uncommon (note that quality is not measured by word count!)

This is not exactly a novel insight; from the 1974 The Elements of Programming Style by Brian W. Kernighan and P. J. Plauger:

Everyone knows that debugging is twice as hard as writing a program in the first place. So if youre as clever as you can be when you write it, how will you ever debug it?

A lot of stuff I see seems to be written “as clever as can be” and is consequently hard to debug. Ill list a few examples of this pattern below. Its not my intention to argue that any of these things are bad per se, I just want to highlight the trade-offs in “easy to use” vs. “easy to debug”.

  • When I tried running Lets Encrypt a few years ago it required running a daemon as root(!) to automatically rewrite nginx files. I looked at the source a bit to understand how it worked and it was all pretty complex, so I was “lets not” and opted to just pay €10 to the CA mafia, as not much can go wrong with putting a file in /etc/nginx/, whereas a lot can go wrong with complex Python daemons running as root.

(I dont know the current state/options for Lets Encrypt; at a quick glance there may be better/alternative ACME clients that suck less now.)

  • Some people claim that systemd is easier than SysV init.d scripts because its easier to write systemd unit files than it is to write shell scripts. In particular, this is the argument Lennart Poettering used in his systemd myths post (point 5).

I think is completely missing the point. I agree with Poettering that shell scripts are hard I wrote an entire post about that but by making the interface easier doesnt mean the entire system becomes easier. Look at this issue I encountered and the fix for it. Does that look easy to you?

  • Many JavaScript frameworks Ive used can be hard to fully understand. Clever state keeping logic is great and all, until that state wont work as you expect, and then you better hope theres a Stack Overflow post or GitHub issue to help you out.

  • Docker is great, right up to the point you get:

 ERROR: for elasticsearch Cannot start service elasticsearch:
oci runtime error: container_linux.go:247: starting container process caused "process_linux.go:258:
applying cgroup configuration for process caused \"failed to write 898 to cgroup.procs: write
/sys/fs/cgroup/cpu,cpuacct/docker/b13312efc203e518e3864fc3f9d00b4561168ebd4d9aad590cc56da610b8dd0e/cgroup.procs:
invalid argument\""

or

ERROR: for elasticsearch Cannot start service elasticsearch: EOF

And … now what?

  • Many testing libraries can make things harder to debug. Rubys rspec is a good example where Ive occasionally used the library wrong by accident and had to spend quite a long time figuring out what exactly went wrong (as the errors it gave me were very confusing!)

I wrote a bit more about that in my Testing isnt everything post.

  • ORM libraries can make database queries a lot easier, at the cost of making things a lot harder to understand once you want to solve a problem.

via: https://arp242.net/weblog/easy.html

作者:Martin Tournoij 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出