02月27, 2020

软件构造 - Week01

阅读材料: MIT Software Construction Reading1:Static Checking

第一节课

讲述了从三个维度看软件

  • 代码层级/组件层级
  • 瞬时视角/周期视角
  • 编码视角/运行视角

第二节课

讲述了软件的内/外部质量目标,以及软件的五个关键的质量目标

  • Easy to understand
  • Ready for change
  • Cheap for develop
  • Safe from bugs
  • Efficient to run

以及在目标之间矛盾时如何折中

材料阅读、思考

该文章的主要目的(应该)让MIT的学生了解python与java的一些区别,让学生快速从python过渡到java,介绍了静态代码检查,从计算冰雹序列的问题出发,介绍了类型与操作符,并引出java是一种静态类型的语言,即所有变量的类型在编译时就已经知道了,所以在编写代码时IDE就可以检查问题。(与之相对应的动态类型语言的类型检查发生在程序运行的时候)

我又查阅资料得知静态动态类型不是通过变量的定义方式来区分的,而是通过编译/运行时拒绝ill behaved区分的

弱类型、强类型、动态类型、静态类型语言的区别是什么? - rainoftime的回答 - 知乎

W01P01.png

编程语言通常能提供以下三种自动检查的方法:

  • 静态检查: bug在程序运行前发现(如 1+"2")
  • 动态检查: bug在程序运行中发现(如 1/0)
  • 无检查: 编程语言本身不帮助你发现错误,你必须通过特定的条件(例如输出的结果)检查代码的正确性。(如 1.0/0)

文章的后面介绍了两个编程的目标:

  • 与计算机交流 —— 保证软件的逻辑正确
  • 与其他人交流 —— 使程序易于理解

为了提高易读性,可以使用文档记录设想(Documenting Assumptions),在编译的时候 Java 就会检查这个设想, 并且保证在你的代码中没有任何一处违背这个设想;并且在别人阅读程序时,这个文档能帮助他快速理解代码。

文章还介绍了两种代码风格:Hacking 与 Engineering

对于软件质量来说,Engineering风格显然更好:

  • 在编码时一次只写一点点, 一边写一边测试
  • 记录编码的设想、意图

READING EXERCISES

1

int n = 5;
if (n) {
  n = n + 1;
}
  • [x] static error
  • [ ] dynamic error
  • [ ] no error, wrong answer

2

int big = 200000; // 200,000
big = big * big;  // big should be 40 billion now
  • [ ] static error
  • [ ] dynamic error
  • [x] no error, wrong answer

3

double probability = 1/5;
  • [ ] static error
  • [ ] dynamic error
  • [x] no error, wrong answer

4

int sum = 0;
int n = 0;
int average = sum/n;
  • [ ] static error
  • [x] dynamic error
  • [ ] no error, wrong answer

5

double sum = 7;
double n = 0;
double average = sum/n;
  • [ ] static error
  • [ ] dynamic error
  • [x] no error, wrong answer

6

Consider the following simple Python function:

from math import sqrt
def funFactAbout(person):
  if sqrt(person.age) == int(sqrt(person.age)):
    print("The age of " + person.name + " is a perfect square: " + str(person.age))

Which of the following are assumptions made by this code – i.e. they must be true in order for the code to run without errors?

  • [x] person must be an object with age and name instance variables
  • [x] person is not None
  • [x] person.age must be a nonnegative number
  • [ ] person.age must be an integer
  • [x] person.name must be a string

If this code were written in Java instead, which of the following assumptions could be documented by type declarations and statically checked by the Java compiler? (Answer this question independently of the question above, i.e. include assumptions that the code doesn’t actually depend on.)

  • [x] person must be an object with age and name instance variables
  • [ ] person is not null
  • [ ] person.age must be a nonnegative number
  • [x] person.age must be an integer
  • [x] person.name must be a string

SFB

Which of the following ideas discussed in this reading help make your code more safe from bugs?

  • [x] dynamic checking
  • [x] final variables
  • [ ] integer overflow
  • [x] static typing

ETU

Which of the following ideas discussed in this reading help make your code more easy to understand?

  • [x] documented assumptions in comments
  • [x] final variables
  • [ ] overloading
  • [x] static typing

RFC

Which of the following ideas discussed in this reading help make your code more ready for change?

  • [x] documented assumptions in comments
  • [ ] fixed-length arrays
  • [x] methods  

本文链接:http://blog.zireaels.com/post/SCW01.html

-- EOF --

Comments