3/9/2021 - 5:04 PM


Table of Contents(目录)

Single-responsibility principle

The single-responsibility principle (SRP) is a computer-programming principle that states that every class in a computer program should have responsibility over a single part of that program's functionality, which it should encapsulate. All of that module, class or function's services should be narrowly aligned with that responsibility. --wikipedia


评论(comments): wiki给了一个不错的示例,比如一个生成报表的程序:报表的内容是可变的,报表的格式也是可变。根据这个srp原则, 这两件不同事情有不同的责任,应当拆分到不同的模块或者类里面去。It would be a bad design to couple two things that change for different reasons at different times. 耦合(couple)两个事物不是一个好设计。


In computer science, locality of reference(wikipedia) , also known as the principle of locality, is the tendency of a processor to access the same set of memory locations repetitively over a short period of time. There are two basic types of reference locality--temporal and spatial locality.

  • Temporal locality refers to the reuse of specific data, and/or resources, within a relatively small time duration.
  • Spatial locality (also termed data locality) refers to the use of data elements within relatively close storage locations. Sequential locality, a special case of spatial locality, occurs when data elements are arranged and accessed linearly, such as, traversing the elements in a one-dimensional array.

我第一次看到Locality的概念,是在2016年读的CSAPP一书上,隐隐只记得它在强调读写相近的存储空间会有更好的程序性能,也就是Spatial locality

今天,我在看Resource acquisition is initialization 时,注意它有提到了locality,原话是: locality (it allows acquisition and release logic to be written next to each other). 复习一下,就知道了这个是属于Temporal locality的范畴,它常常容易被忽略。比如我们在代码的开头申请了一个变量,在代码快结束的时候才使用这个变量,这违反了temporal locality的原则。


Low coupling

Low coupling is often a sign of a well-structured computer system and a good design, and when combined with high cohesion, supports the general goals of high readability and maintainability. Coupling and cohesion are terms which occur together very frequently. Coupling refers to the interdependencies between modules, while cohesion describes how related the functions within a single module are. Low cohesion implies that a given module performs tasks which are not very related to each other and hence can create problems as the module becomes large.