Small Programming Tricks

Small Programming Tricks / 编程小技巧

Day to day, I think a surprising amount of engineering productivity comes from small nuggets of knowledge: being aware that a language feature exists; knowing that an unexplained tcp delay is probably related to the TCP_NO_DELAY setting and Nagle’s algorithm; knowing the right git incantation to get out of a pickle; or knowing a trick with sed to rewrite a file. 在日常工作中,我认为相当一部分工程生产力其实源于一些零碎的知识点:比如意识到某种语言特性的存在;知道无法解释的 TCP 延迟可能与 TCP_NO_DELAY 设置和 Nagle 算法有关;知道如何使用正确的 git 命令来摆脱困境;或者掌握用 sed 重写文件的技巧。

In one sense, this is self-evident: anything you know is going to be made up of smaller pieces of knowledge. Of course those smaller pieces of knowledge matter. But I think there are some nuggets of knowledge that are particularly valuable and don’t require a lot of supporting mental infrastructure. You don’t need to know any python to use python3 -m http.server to start a simple server in a directory, but it might still make your work marginally easier. 从某种意义上说,这是不言而喻的:你所掌握的任何知识都是由更小的知识碎片组成的。当然,这些碎片化的知识很重要。但我认为,有些知识点特别有价值,且不需要太多的背景知识储备。你不需要精通 Python,只需使用 python3 -m http.server 就能在当前目录下启动一个简单的服务器,但这确实能让你的工作变得轻松一些。

Let me share a few examples: You probably know that ctrl + r allows searching your terminal’s command history, but if you install fzf, you can set it up so that ctrl + r does a fuzzy search. If you want even more power, atuin replaces your shell history with a searchable SQLite database. per-directory-history lets you switch back and forth between searching for commands that have been run in a specific directory or searching all previous commands. Finally, you can configure how much history to store: stackoverflow question. 让我分享几个例子:你可能知道 ctrl + r 可以搜索终端的命令历史,但如果你安装了 fzf,你可以将其配置为进行模糊搜索。如果你需要更强大的功能,atuin 可以用可搜索的 SQLite 数据库替换你的 shell 历史记录。per-directory-history 则允许你在“搜索特定目录下的历史命令”和“搜索所有历史命令”之间切换。最后,你还可以配置历史记录的存储上限:StackOverflow 相关问题

You can SELECT without a FROM. This can be useful for testing out how a function in your database actually works or reminding yourself how SELECT TRUE <> NULL works. PostgresSQL and MySQL both support explain analyze which will actually run the query you’re trying to optimize and give you a ton more information about its performance. 你可以使用不带 FROMSELECT 语句。这在测试数据库函数如何工作,或者回顾 SELECT TRUE <> NULL 的逻辑时非常有用。PostgreSQL 和 MySQL 都支持 explain analyze,它会实际运行你想要优化的查询,并提供大量关于其性能的信息。

In regular expressions, \b, the word boundary assertion, makes it easy to look for the beginnings or ends of words. You can use logarithms with metrics to get a sense of the distribution of values for a field you’re interested in: 在正则表达式中,\b(单词边界断言)可以让你轻松查找单词的开头或结尾。你可以将对数(logarithms)与指标(metrics)结合使用,以了解你感兴趣字段的值分布情况:

const bucket = Math.floor(Math.log10(userInGroupCount))
metrics.increment("my_metric", { bucket });

Modern JS now supports Array.flatMap, Object.entries, and Promise.withResolvers. In NodeJS, you can keep a connection open to an external resource by creating an https.Agent and then providing it to your http requests: fetch(url, {method, agent}). This can have a dramatic impact on latency. 现代 JavaScript 现在支持 Array.flatMapObject.entriesPromise.withResolvers。在 NodeJS 中,你可以通过创建一个 https.Agent 并将其提供给 http 请求(如 fetch(url, {method, agent}))来保持与外部资源的连接。这可以显著降低延迟。

git log -S pattern (”git pickaxe”) can give you all commits that added or removed a string in a codebase. It’s amazingly useful especially with older codebases! (git log -G pattern is similar, but will also show when that line was moved). Similar to cd -, you can use git checkout - to check out your previous HEAD. git log -S pattern(即“git pickaxe”)可以列出代码库中所有添加或删除了某个字符串的提交。这在处理旧代码库时非常有用!(git log -G pattern 类似,但还会显示该行代码被移动的情况)。类似于 cd -,你可以使用 git checkout - 来切换回上一个 HEAD。

You probably don’t need find. A lot of find commands can be replaced with globs like **/*.md. Most shells support this out of the box, but with bash, you need to turn this on with shopt -s globstar. In a similar vein, most folks will probably want to use rg (ripgrep) rather than grep, ack, or ag. 你可能并不需要 find 命令。许多 find 命令可以用 **/*.md 这样的通配符代替。大多数 shell 开箱即用,但在 bash 中,你需要通过 shopt -s globstar 来开启此功能。同样地,大多数人可能更愿意使用 rg (ripgrep) 而不是 grepackag

zsh’s advanced autocompletion features aren’t turned on by default: zsh 的高级自动补全功能默认并未开启:

if type brew &>/dev/null; then
  FPATH="$(brew --prefix)/share/zsh/site-functions:${FPATH}"
fi
autoload -Uz compinit
compinit

You might have already known all of these things! Or you might work in a domain that makes all of these little tricks totally useless. Even if this particular set of tricks isn’t useful for you, I bet you have your own stash of tricks that you’ve accumulated over the years that makes your work easier. 你可能已经掌握了所有这些技巧!或者你所处的领域让这些小技巧毫无用武之地。即使这套特定的技巧对你没用,我相信你一定也有自己多年积累的“私藏技巧”,让你的工作变得更轻松。

At a company, I think even more knowledge tends to be this sort of small high-leverage nugget: 在公司里,我认为更多的知识往往也属于这种“小而高效”的碎片:

  • To debug $PROBLEM, use $DATA_SOURCE.
  • $PERSON knows a ton about $AREA and they’re happy to help if you get stuck.
  • There are good docs about $HARD_THING $OVER_HERE.
  • When $THING happens, it means we should manually scale out.
  • To do a rolling restart of a service, run $THIS_COMMAND.
  • This $UTIL makes $THAT_PROBLEM easy to script.
  • 调试 $PROBLEM 时,请使用 $DATA_SOURCE。
  • $PERSON 对 $AREA 非常了解,如果你卡住了,他们很乐意帮忙。
  • 关于 $HARD_THING 的好文档在 $OVER_HERE。
  • 当 $THING 发生时,意味着我们需要手动扩容。
  • 要对服务进行滚动重启,请运行 $THIS_COMMAND。
  • 这个 $UTIL 让 $THAT_PROBLEM 的脚本化变得简单。

At a previous company, I shared a trick on slack every day with the engineering team, both technical and company-specific, and folks found them pretty useful. Even if you knew 9/10 tricks, that 10th doc or technique might save you some time! And one trick per day was the right number to avoid overwhelming people with knowledge, and it could occasionally spark useful discussion. 在上一家公司时,我每天都会在 Slack 上与工程团队分享一个技巧(包括技术类和公司内部特有的),大家觉得非常有用。即使你已经掌握了 10 个技巧中的 9 个,那第 10 个文档或方法可能就会为你节省时间!每天分享一个技巧是避免信息过载的合适频率,而且偶尔还能引发有益的讨论。

If you’re a more senior engineer at your company, you might think about doing something similar. I think I first saw this technique on Julia Evan’s blog, and I think her writing often perfectly encapsulates the idea behind this post: “small bits of knowledge are powerful! and fun! and approachable!!” 如果你是公司里的资深工程师,不妨考虑做类似的事情。我记得最初是在 Julia Evans 的博客上看到这个方法的,我认为她的文字完美地概括了这篇文章背后的理念:“小知识点既强大、有趣,又平易近人!!”