Linus torvalds talking about good taste

“The mind behind Linux | Linus Torvalds”

Sometimes you can see a problem in a different way and rewrite it so that a special case goes away and becomes the normal case. And that’s good code.

To me , the sign of good people I really want to work with is that they have good taste.

This is a small example , Good Taste is much bigger than this.

Good taste is about really seeing the big patterns, and kind of instinctively and knowing what’s the right way to do things.

install ruby-3.2.0 on macos

Background

Steps

1
2
3
4
5
6
7
rvm reinstall "ruby-3.2.0" --with-openssl-dir=`brew --prefix openssl@3` # not ok, [ruby don't support openssl 3 yet.](https://github.com/ruby/openssl/issues/369)

brew uninstall openssl@3
brew reinstall openssl@1.1

# new shell
rvm reinstall "ruby-3.2.0" --with-openssl-dir=`brew --prefix openssl@1.1` --disable-binary # ok

Other ref

Reading List of 2023

  • TODO learn you a haskell
  • OK gopl
  • TODO beyond feelings
  • TODO sicp
  • TODO unix network programming volume 1: the sockets networking api, 3rd edition
  • TODO [optional] High-Performance-Browser-Networking
  • OK [optional] clean code
  • OK algs4 (20230421)
  • OK A philosophy of software design(20230421)

Reading List of 2022

  • review: unix-processes 15 * 15 min = 225 min(done 20220517)

  • read: 元编程 240 页 / (10页/15 min) = 360 min(done 20220521)

  • listen/review: 史蒂夫乔布斯传 (76 - 18 + 1) * 20 min = 1180 min(done 20220524)

  • read: 暗时间 244 页 / (10页/15 min) = 366 min(done 20220623)

  • read: CSAPP 730 页 / (10页/20 min) = 1460 min(ff)(done 20220512 -> 20220925)

  • review: 现实一种(余华) (done 20220925 -> 20221001)

  • read: unix编程艺术 (done 20200704 -> 20220924)

  • read: refactoring (done 20220929 -> 20221012)

  • read: DDD (done 20221107 -> 20221213)

Testing and Refactoring sharing

  • Bi-weekly Tech Sharing

Agenda

  • Experience
  • Testing
  • Refactoring
  • References

Experience

  • best thing learned this year

  • I’m proud that we have testing culture and code review

  • how I test my code before?

    • coding -> run program -> postman / repl -> verify by eye -> QA
  • how I test my code now?

    • coding -> add tests -> green -> refactoring -> coding -> repeat -> code review
  • hopefully I’ll benefit from TDD style sooner

Testing

  • raise a question: we’re relying on tests to guarantee code work as expected, who guarantees our tests are correct then?

    • short answer: we can only rely on simple tests
    • too simple to be wrong(ideal world)
  • testing proportion

  • testing helped on

    • building confidence
    • building solid(bug free) code as possible
    • gives me a button to click
      • “if you ever get that button, you’ll never want to lose it or work other way round so you wouldn’t have it.”
  • testing drives me to write simple code

    • I want class to be small
    • I want responsiblilty to be single/simple/clear
    • I want method to be short
    • I want interface to be well defined
    • I love good names
  • example of using let(private git repo pull request)

    • demo for independant tests
    • avoid mutable variable
    • discipline posed upon assignment/mutability
  • key points:

    • write simple code & test when possible
    • more unit tests
    • “write enough tests to be confident”

Refactoring

  • let’s put performance aside while coding & refactoring

  • let’s focus on clarity/simplicity/readability/maintainability, not performance

    • make it simple
    • make it easier to read/review
    • make it easier to change/maintain
  • How I do code refactoring?

    • clean up feature flags
    • check code coverage and make it up to 100%
    • remove code smell
    • simplify code
    • enjoy the tiny changes/improvements
    • enjoy the green dots
    • enjoy the red dots(it shows I didn’t fully understand my code)
    • remove unused/dead code(unused)
    • check with flog(rely on metric tools, not “feeling”)
    • choose a better name for variables/methods/class/file/moudles
    • simplify tests
    • I got better understanding of the code, and I can repeat the above process…
  • example of simplifying code(private git repo pull request)

  • The point is: “business complixity is the lower limit of system complixity”

    • so I want my code to be as simple/readable as possible
    • try functional thinking: e.g. use filter/map/reduce
    • ruby: Enumerable module is amazing
  • key points:

    • lean on tests, no tests, no refactoring
    • do your refactoring with small steps(highlighted in both “refactoring” and “99 bottles of OOP”)
    • refactoring is just pattern matching(focus on code smell) and follow the solution

References

At last

these are just my limited experience on testing & refactoring; I’m pretty sure I have a lot more to learn

I really want to learn from everyone about these two topics

pls correct me if I’m wrong. pls discuss with me if you’re interested~ Orz

A leading space in command wastes lots of my time

This one doesn’t have a TL;DR

Background

What I did

  • I wrote two specific shell aliases for rspec, re for running specs without error backtraces, ree for with error backtraces

    1
    2
    alias re="DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile"
    alias ree="DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile --backtrace"
  • One day, I found it looks better if I change them to this(alignment):

    1
    2
    alias re=" DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile"
    alias ree="DISABLE_SPRING=1 bundle exec rspec --format=progress --no-profile --backtrace"

  • Later, I forgot the change

  • Somehow, I find the frequently used re command never got remembered by type re and press the up arrow(which acts as searching from history in OMZ)

Trouble shooting

  • I didn’t think too much about what I’v recently changed
  • I thought I screwed up my OMZ environment, so I keep creating new terminal session, didn’t work
  • I thought my history file is too big, so I cleared the history file, didn’t work
  • I put this issue aside for a few days without any solution, suffering from typing/copy-paste the frequently used rspec commands again and again
  • A few days later, I can’t stand it anymore, and at this time, I found that only that rspec related command won’t be remembered by history!(still didn’t connect to previous changes…)
  • Until second day when I tried to optimize my OMZ plugin, I noticed the git history: THERE IS A LEADING SPACE IN MY RSPEC ALIAS!!! because of the code alignment

The reason

The fix

  • Remove the leading space in shell alias, simple enough

Learnings

rails activejob vs sidekiq

WIP

最初一直不喜欢rails里的activejob

原因很简单, 日志里它的日志很难懂

后来花时间读了一下文档, 了解到了gid这个概念, 知道了它里面有一些和controller里一致的rescue_from等等功能, 觉得它确实有优势

日志也能看懂了, 不那么排斥它了

甚至因为gid的缘故, 有点喜欢这个设计

但是, sidekiq里提倡的最佳实践是: 使用简单参数, 很清晰易懂, 用gid其实也做到了一样的事, 而且他可以传递model实例, 挺好的优势

直到昨天, 看到同事的一个pr修改了activejob的参数(由model实例改为id), 我提出是不是没必要这样, 因为前面说到的原因

但是他展示给我 如果在log里搜索 “Sidekiq::Extensions::DelayedClass ARGS” 会发现, 虽然perform接口的model实例会被序列化为gid, 但是这种延时的任务, 实际上还是序列化为了yml

所有model实例的所有信息都被存储了下来, 然后做反序列化, 所有信息都被记入了log里面, 太糟糕了

  • 反思.1: sidekiq的代码没读全, activejob的代码没读全, 重大的疏漏
  • 反思.2: 简单的是好的, 简单的最佳实践也需要认真了解其背后的逻辑
  • 反思.3: 不要对自己信奉的东西有太强的自信, 多多沟通和交流, 谦虚的向大家学习

vagrant booting error

Error.1 NS_ERROR_FAILURE

Using macos to debug c program is annoying…

Tried to use vagrant vm, but I met error while booting the vm

1
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine

Solution

https://stackoverflow.com/questions/52689672/virtualbox-ns-error-failure-0x80004005-macos

I tried reinstall Virtualbox, not working

I tried reboot mbp, this time it worked

Don’t know the root reason yet

Error.2 umount: /mnt: not mounted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ vagrant up

...

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

umount /mnt

Stdout from the command:



Stderr from the command:

umount: /mnt: not mounted

then I can vagrant ssh into the machine, seem above error is not a big deal

But I want to make it go away…

Reason

seems related to vagrant plugin vagrant-vbguest

1
2
vagrant plugin list
vagrant plugin uninstall vagrant-vbguest

Solution

Unistall the plugin and reboot the vagrant vm

1
2
3
4
vagrant halt
vagrant destroy
vagrant up
vagrant ssh

Error.3 Remote connection disconnect. Retrying and ask for ssh password

https://github.com/puphpet/puphpet/issues/1253#issuecomment-145429092

for some dumb people like me, do not use /home/vagrant as your shared folder (in your vm), because the .ssh files are not accesible.

don’t understand the reason, but this is the cause, remove the config.vm.synced_folder resolved the problem

How to debug

1
2
vagrant ssh-config
vagrant ssh --debug

Solution

rm config.vm.synced_folder in Vagrantfile and try again