“Reading 1: Static Checking” 静态检查心得体会

Reading 1: Static Checking 静态检查
这是在看了这篇文章后,觉得一些挺好的内容。

一、Hailstone Sequence(雹石序列)

Starting with a number n, the next number in the sequence is n/2 if n is even, or 3n+1 if n is odd. The sequence ends when it reaches 1. Here are some examples:

以数字n开始,下一个在序列中的数字,如果是偶数的话就除以2,如果是奇数的话就乘以3再加上1,这个序列最终都会以1结尾。

2, 1
3, 10, 5, 16, 8, 4, 2, 1
4, 2, 1
2n, 2n-1 , … , 4, 2, 1
5, 16, 8, 4, 2, 1
7, 22, 11, 34, 17, 52, 26, 13, 40, …? (where does this stop?)

二、Computing Hailstones(雹石序列的计算)

// Java
int n = 3;
while (n != 1) {
    System.out.println(n);
    if (n % 2 == 0) {
        n = n / 2;
    } else {
        n = 3 * n + 1;
    }
}
System.out.println(n);
# Python
n = 3
while n != 1:
    print(n)
    if n % 2 == 0:
        n = n / 2
    else:
        n = 3 * n + 1


print(n)

You should always indent the block, even though Java won’t pay any attention to your extra spaces. Programming is a form of communication, and you’re communicating not only to the compiler, but to human beings. Humans need that indentation.

你应该总是缩进块,即使Java不会注意你的额外空间。编程是一种交流形式,您不仅要与编译器进行通信,还要与人类进行通信。人类需要缩进。

三、types(类型)

A type is a set of values, along with operations that can be performed on those values.(类型是一组值,可对它们进行操作。)

Java has several primitive types, among them:

  • int
  • long
  • boolean
  • double
  • char

Java also has object types, for example:

  • String
  • BigInteger

By Java convention, primitive types are lowercase, while object types start with a capital letter.

java约定,原始数据类型是小写,而对象类型(Object Type)首字母大写

Here are three different syntaxes for an operation in Python or Java:

  • As an infix, prefix, or postfix operator. For example, a + b invokes the operation + : int × int → int
  • (In this notation: + is the name of the operation, int × int before the arrow describes the two inputs, and int after the arrow describes the output.)
  • As a method of an object. For example, bigint1.add(bigint2) calls the operation add: BigInteger × BigInteger → BigInteger.
  • As a function. For example, Math.sin(theta) calls the operation sin: double → double. Here, Math is not an object. It’s the class that contains the sin function.

以下是Python或Java中的三种不同语法:

  • 作为中缀,前缀或后缀运算符。例如,a + b调用操作+ : int × int → int (在此表示法中:+是int × int箭头描述两个输入之前的操作名称,在int箭头描述输出之后)。
  • 作为对象的方法。例如,bigint1.add(bigint2)调用该操作add: BigInteger × BigInteger → BigInteger。
  • 作为一个功能。例如,Math.sin(theta)调用该操作sin: double → double。在这里,Math不是一个对象。它是包含该sin函数的类。

四、Static Typing (静态类型)

Java is a statically-typed language. The types of all variables are known at compile time (before the program runs), and the compiler can therefore deduce the types of all expressions as well. If a and b are declared as ints, then the compiler concludes that a+b is also an int. The Eclipse environment does this while you’re writing the code, in fact, so you find out about many errors while you’re still typing.

Java是一个静态类型语言。所有变量的类型在编译时(程序运行之前)都是已知的,因此编译器也可以推断出所有表达式的类型。例如a和b都是int类型,那么a+b也是int类型。IDEA或Eclipse在你编写代码的过程中这样做,因此你可以在你编程的过程中发现很多error.

In dynamically-typed languages like Python, this kind of checking is deferred until runtime (while the program is running).

在动态类型像Python这样的语言,这种检查延迟到运行时(程序运行时)。

Static typing is a particular kind of static checking, which means checking for bugs at compile time.

静态类型是一种特殊的静态检查(static checking),意味着可在编译时期检查bugs。

五、the way of checking (检查方式)

1、三种检查方式

  • Static checking: the bug is found automatically before the program even runs.
  • Dynamic checking: the bug is found automatically when the code is executed.
  • No checking: the language doesn’t help you find the error at all. You have to watch for it yourself, or end up with wrong answers.
  • 静态检查:在程序运行之前自动找到错误。
  • 动态检查:执行代码时会自动找到错误。
  • 没有检查:语言根本无法帮助您找到错误。你必须自己观察,或者得到错误的答案。

2、检查方式的详细说明

Static checking can catch:

  • syntax errors, like extra punctuation or spurious words. Even dynamically-typed languages like Python do this kind of static checking. If you have an indentation error in your Python program, you’ll find out before the program starts running.
  • wrong names, like Math.sine(2). (The right name is sin.)
  • wrong number of arguments, like Math.sin(30, 20).
  • wrong argument types, like Math.sin(“30”).
  • wrong return types, like return “30”; from a function that’s declared to return an int.

静态检查可以捕获:

  • 语法错误,如额外的标点符号或虚假的单词。即使像Python这样的动态类型语言也会进行这种静态检查。如果您的Python程序中有缩进错误,您将在程序开始运行之前找到答案。
  • 错误的名字,比如Math.sine(2)。(正确的名字是sin。)
  • 错误的参数数量,如Math.sin(30, 20)。
  • 错误的参数类型,如Math.sin(“30”)。
  • 错误的返回类型,例如return “30”;来自声明返回的函数int。

Dynamic checking can catch:

  • illegal argument values. For example, the integer expression x/y is only erroneous when y is actually zero; otherwise it works. So in this expression, divide-by-zero is not a static error, but a dynamic error.
  • unrepresentable return values, i.e., when the specific return value can’t be represented in the type.
  • out-of-range indexes, e.g., using a negative or too-large index on a string.
  • calling a method on a null object reference (null is like Python None).

动态检查可以捕获:

  • 非法参数值。例如,整数表达式x/y只有在y实际为零时才是错误的;否则它有效。所以在这个表达式中,除零不是静态错误,而是动态错误。
  • 不可代表的返回值,即特定返回值无法在类型中表示的情况。
  • 超出范围的索引,例如,对字符串使用负或过大的索引。
  • 在null对象引用上调用方法(null就像Python一样None)

3、小结

Static checking tends to be about type errors, errors that are independent of the specific value that a variable has. A type is a set of values. Static typing guarantees that a variable will have some value from that set, but we don’t know until runtime exactly which value it has. So if the error would be caused only by certain values, like divide-by-zero or index-out-of-range then the compiler won’t raise a static error about it.

Dynamic checking, by contrast, tends to be about errors caused by specific values.

静态检查往往是关于类型错误,错误与变量具有的特定值无关。类型是一组值。静态类型保证变量将具有该集合中的某些值,但我们直到运行时才知道它具有哪个值。因此,如果错误仅由某些值引起,例如除零或索引超出范围,则编译器不会引发关于它的静态错误。

相反,动态检查往往是由特定值引起的错误。

六、Summary(总结)

The main idea we introduced today is static checking. Here’s how this idea relates to the goals of the course:

  • Safe from bugs. Static checking helps with safety by catching type errors and other bugs before runtime.
  • Easy to understand. It helps with understanding, because types are explicitly stated in the code.
  • Ready for change. Static checking makes it easier to change your code by identifying other places that need to change in tandem. For example, when you change the name or type of a variable, the compiler immediately displays errors at all the places where that variable is used, reminding you to update them as well.

我们今天介绍的主要思想是静态检查。以下是这个想法与课程目标的关系:

  • 安全无bugs。静态检查通过在运行时捕获类型错误和其他错误来帮助安全
  • 易于理解。它有助于理解,因为类型在代码中明确说明。
  • 准备接收改变通过识别需要同时更改的其他位置,静态检查可以更轻松地更改代码。例如,当您更改变量的名称或类型时,编译器会立即在使用该变量的所有位置显示错误,并提醒您也要更新它们。

结语

以上是我阅读后再结合搜集的资料,觉得一些比较好的内容,如有不足请多指教。

参考文献

https://ocw.mit.edu/ans7870/6/6.005/s16/classes/01-static-checking/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值