代码之美(Beautiful Code)
https://book.douban.com/subject/3224524/
太多章节的内容需要特定的领域知识才能理解比如内核,数学计算,生物信息,编译器的宏改写等,而那种只需要通用知识就可以理解的章节,好像内容也不是特别深入。
我觉得这本书最好把先关章节放在网上,读者可以挑选自己感兴趣和自己领域相关的章节阅读,并且不要期望从中学到太多技术性的东西,练习英语或者是陶冶情操可以。
我挑选了几个我觉得有点意思的章节记录一下。
Python’s Dictionary Implementation:
- 使用开放式散列,超过容量的2/3就会进行扩表
- 扩表比例根据keys的多少,keys比较少的话使用*4,keys比较多的话使用*2
- 开放式散列决定了没有办法真正删除而是标记删除
- 针对小字典以及key=string的字典有专门的优化
- 使用freelist来加快字典对象的分配
重散列的实现代码是
/* Starting slot */ slot = hash; /* Initial perturbation value */ perturb = hash; while (<slot is full> && <item in slot doesn't equal the key>) { slot = (5*slot) + 1 + perturb; perturb >>= 5; }
ERP5: Designing for Maximum Adaptability
ERP是个什么东西呢?
ERP is software that aims to integrate all the data and processes of an organization into a unique system. Since this is a real challenge, the ERP industry offers different versions of the same ERP software for different economic segments, such as oil and gas, mechanical, pharmaceutical, automobile, and government.
ERP software generally consists of a series of modules that automate the operations of the organization. The most common modules include finance, inventory control, payroll, pro- duction planning and control, sales, and accounting. Those modules are designed for cus- tomization and adaptation at the user’s site, because even though the organizations of a given economic segment share certain common practices, every organization wants to adapt the ERP system to its specific needs. ERP software also evolves quickly to accom- pany the evolution of the businesses it serves, and more and more modules are added to it over time.
Beautiful Concurrency: Simon Peyton Jones(SPJ)
谈到为什么Locks这种东西不好的时候,我觉得挺有启发性的。就是lock这个东西是没有办法组合的,使用了lock的function放在一起的时候需要特别小心,而使用stm的function则可以放心地进行compose.
However, as we also discussed in the section “Locks Are Bad,” the worst problem with lock-based programming is that locks do not compose. In contrast, any function with an STM type in Haskell can be composed, using sequencing or choice, with any other function with an STM type to make a new function of STM type. Furthermore, the compound func- tion will guarantee all the same atomicity properties that the individual functions did. In particular, blocking (retry) and choice (orElse), which are fundamentally non-modular when expressed using locks, are fully modular in STM.
Haskell在类型上对使用STM有很大的帮助:
- 要求STM的类型必须是 `STM a`
- 操作中绝对不能含有涉及到 `IO a` 的操作
- 只能操作 `TVar a` 类型的变量
Treating Code As an Essay: Yukihiro Matsumoto(松本行弘)
为什么代码的可读性这么重要?因为它是人类对操控计算机这件事情的意图上进行沟通和协作的工具,准确和清晰的表达是至关重要的。
Most programs are not write-once. They are reworked and rewritten again and again in their lives. Bugs must be debugged. Changing requirements and the need for increased functionality mean the program itself may be modified on an ongoing basis. During this process, human beings must be able to read and understand the original code; it is there- fore more important by far for humans to be able to understand the program than it is for the computer.
在简洁,保守,简单和灵活方面达成平衡。关于保守,作者说到了其实人类是非常保守的动物,所以在语言设计上步子不能迈得太大,要让所有人都能够来使用你的语言。
Balance is the final element of beautiful code. So far I have talked about brevity, conserva- tism, simplicity, and flexibility. No element by itself will ensure a beautiful program. When balanced together and kept in mind from the very beginning, each element will work harmoniously with the others to create beautiful code. And if you also make sure to have fun writing and reading code, you will experience happiness as a programmer.