Rob Pike's 5 Rules of Programming

Rob Pike’s 5 Rules of Programming Rule 1. You can’t tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don’t try to second guess and put in a speed hack until you’ve proven that’s where the bottleneck is. Rule 2. Measure. Don’t tune for speed until you’ve measured, and even then don’t unless one part of the code overwhelms the rest. Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don’t get fancy. (Even if n does get big, use Rule 2 first.) Rule 4. Fancy algorithms are buggier than simple ones, and they’re much harder to implement. Use simple algorithms as well as simple data structures. Rule 5. Data dominates. If you’ve chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming. Pike’s rules 1 and 2 restate Tony Hoare’s famous maxim “Premature optimization is the root of all evil.” Ken Thompson rephrased Pike’s rules 3 and 4 as “When in doubt, use brute force.”. Rules 3 and 4 are instances of the design philosophy KISS. Rule 5 was previously stated by Fred Brooks in The Mythical Man-Month. Rule 5 is often shortened to “write stupid code that uses smart objects”.

January 20, 2023 · 2 min · ChaosNyaruko

今天摸鱼水段子时,在/r/ProgrammerHumor/上发现一个有意思的现象 熟悉Python的同学可能都踩过坑,但我确实是第一次知道这个现象,于是我在本地试了一下,发现确实如此,于是我求助于谷歌,并最终在Python的官方文档里找到了解释 Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise, the return value has the same type as number. ...

January 3, 2023 · 1 min · ChaosNyaruko

Get Server A basic VPS is enough for personal usage. Nginx Setup apt install nginx main directories and files: /etc/nginx/sites-available /etc/nginx/sites-enabled(typically symbolic links for “available”) /etc/nginx/nginx.conf /var/log/nginx/access.log /var/log/nginx/error.log Get HTTPS Cert Support Using HTTPS should be modern and more secure. https://snapcraft.io/docs/installing-snap-on-debian https://certbot.eff.org/instructions?ws=nginx&os=debianbuster We use certbot to get https support for your website, and renew automatically. Debian10/11 operation example: apt install snapd snap install core snap install hello-world snap refresh core snap install --classic certbot ln -s /snap/bin/certbot /usr/bin/certbot certbot --nginx / certbot certonly --nginx certbot renew --dry-run The job should be in: ...

December 16, 2022 · 1 min · ChaosNyaruko

Don't use Vim

The ORIGINAL post Don’t use Vim Don’t do the crime, if you can’t do the time. – Anthony Vincenzo “Tony” Baretta Vim is an amazing text editor. I love it. Really, I wouldn’t organize a Vim advent calendar if I didn’t. But, as amazing as it is, Vim is not for everyone. It can’t solve all your problems, or be a TUI version of your favorite IDE, or make you a better programmer, or land you that dream job in the Bay Area. But Vim can help you be more mindful, focused, and efficient, as long as you approach it with the right mindset. ...

August 29, 2022 · 10 min · Me

Vim OR Emacs?

随便聊聊我对Vim和Emacs的看法吧,说说我对这两大成名已久的古老编辑器的看法以及我的立场。 我并不打算在这里详细说明Vim或Emacs是怎么使用的,因为互联网上已经有足够多的资料,肯定比我讲的好的多。而且我自己也只用了Vim两年左右,并不资深,而Emacs我更是几乎等于一窍不通。所以我仅列出Vim和Emacs在我有限的了解里表现出的优点和缺点。可以的话,尽量互相对应。 免责声明:这场旷日持久的战争对于很多人,尤其是Linux爱好者来说,恐怕已经不是什么新鲜事了。我相信每个人都有自己的立场,也有自己的理由。所以在这里,我只是表达我的个人看法,对特性的总结可能有偏差,但立场没有对错,也没有优劣。 Vim “编辑器之神” 严格来说Vim是上个世纪90年代才诞生的,远不如Emacs来的古老(70年代)。但其前身Vi(Vim = Vi IMproved,但好像有个说法是谦虚的Bram Moolenaar最一开始将m定义为IMitation1),也是在70年代诞生的。这段历史其实很有意思,可以从TAOUP中略窥一二,但这里暂且不说。Vi和Vim在纯文本的操作逻辑上是非常非常相似的,而这种操作模式也是Vim最广为人知的特性,所以通常意义上我认为Vim和Emacs同样古老。 Vim 作为最有名的“模式”编辑器之一,我认为这种“模式”正是Vim给后继者/现代编辑器提供的最宝贵的财富。初学者一般会认为Vim的学习曲线非常陡峭,但实际上,跨过了模式编辑的适应期之后,多数人-可能是主体是程序员吧:),在大多场景下应该会爱上这种方式。因为它解放了键盘上的很多按键,进而解放了很多操作,从而让一种“操作文本”的迷你语言(我认为这就是一个层面的语言)成为一种高效而核心的理念。这种操作模式现在可以说是广泛应用在现代编辑器中,例如VSCode、JetBrains家的IDE,都有对应的vim插件。这类插件本质上提供的就是这个操作模式。甚至不少人表示,即使Vim这个软件看起来有些过时,但如果重新实现一个,仍然会保留这种模式。其集大成者自然是NeoVim。这里不展开。 优点 模式编辑,以及引入模式带来的默认情况下更高效/人性化的键位设置,和这套模式在现代其他编辑器里的广泛移植。 完美运行在TUI环境下,劫持全键盘操作 跨平台,而且几乎在所有的UNIX发行版上都默认安装,至少是Vi。这也是为什么Linux运维人员使用Vi/Vim的比例要高得多 占用资源少 超高度的扩展和定制性。你几乎可以修改所有你能想到的配置来满足自己平常的编辑需求 开源软件,活跃的社区。 完备的内部帮助文档 更符合UNIX哲学(小而精工具组合完成更复杂的任务) 缺点 不好上手。StackOverflow点击最多的问题之一, “How do I exit Vim?” 图形界面支持一般,不管是Windows上的GVim还是MacOS上的MacVim,都只是TUI版本Vim的一层简单包装。所以很多在现代编辑器上习以为常的操作,像右击唤出菜单什么的,可能做需要额外配置。可能也正因如此,Vim作为本质上为Terminal开发的编辑器,在“前端交互”或者说“作为其他开发工具的前端”这点上,远不如Emacs灵活 中文支持不好。一方面是字符集的问题,像分词之类的功能受到影响;另一方面是输入法的问题,在中文输入法下,Normal模式的很多操作会被输入法劫持,需要额外的解决方案。多数情况下,这些方案或许管用,但都并不优雅。英语母语者就很少有这两个问题 Vimscript(原生Vim使用的脚本语言)比起lisp虽然形式上更像现代的脚本语言,但真的足够晦涩难懂,表达力欠缺。NeoVim的蓬勃发展或许能解决这个问题 作为一个软件本身看上去有点过时,而且软件工程组织并不好,维护也更多是集中在一个人身上 Emacs “神之编辑器” Emacs我使用的并不多,只是偶尔看到Battle贴心血来潮会偶尔使用一下,或者是看到颇具盛名的doomemacs/spacemacs的安利贴时进行一些尝试,但我仍然斗胆表达一下我的看法。另外说一句,Emacs是自由软件运动的标志性产物,自由软件运动更多的是一种思想解放:)。 它是“伪装成编辑器的操作系统” 优点 上手快 跨平台,TUI/GUI下都能很好地使用,在GUI工作得更好,同时也支持全键盘操作 lisp/elisp的最佳学习平台 比Vim更高的扩展性,因为它自己就是个Lisp解释器,Lisp是图灵完备的语言。所以,你可以在Emacs里面实现一个Vim,甚至可以用它煮咖啡,字面意义上的。 开源软件,较活跃的社区 完备的帮助和提示系统,我认为这里做的比Vim更好,比如可以通过 C-h + k 来查看对应快捷键绑定了什么功能 Org mode,可能是最强的GTD工具 作为软件工程实践更加出色,Richard Stallman的开发和维护工具顺利交接给了其他人。比Vim具有更低的选择复杂度,这点在TAOUP中也有提到 缺点 默认情况下近乎反人类的键位设置,以及其小拇指的伤害:(,因为默认键位用到大量功能键,有的还会与操作系统默认配置,或者与其他软件冲突。比如ctrl+space很多情况下会被操作系统设置为切换输入法,从而使其在Emacs中的功能失效,需要额外配置 多数情况下占用资源相对Vim较多,启动也更慢,这在现代计算机上这并不是什么问题,但在很多小型或嵌入式设备上,可能无法使用自己顺手的Emacs配置 容易陷入“玩”Emacs的“陷阱”中,而且lisp是一门经典且值得学习,但并不广泛应用的语言,你几乎只会在Emacs中大量用到它。相比之下,Vim可以使用python、lua等语言进行插件编写,不用重复造轮子 与传统UNIX哲学稍稍相悖,everything in one and elisp. 现代流行度稍逊于Vim,很多问题可能得自己解决,很让人头秃 两者的选择 简单概括2 Emacs, “a great operating system, lacking only a decent editor ...

July 23, 2022 · 1 min · ChaosNyaruko