auto commit
This commit is contained in:
parent
b981096466
commit
f19e3b135e
@ -54,7 +54,7 @@
|
|||||||
|
|
||||||
排列整齐的注释:
|
排列整齐的注释:
|
||||||
|
|
||||||
```
|
```java
|
||||||
int a = 1; // 注释
|
int a = 1; // 注释
|
||||||
int b = 11; // 注释
|
int b = 11; // 注释
|
||||||
int c = 111; // 注释
|
int c = 111; // 注释
|
||||||
@ -87,20 +87,20 @@ int c = 111; // 注释
|
|||||||
|
|
||||||
尽量简洁明了:
|
尽量简洁明了:
|
||||||
|
|
||||||
```
|
```java
|
||||||
// The first String is student's name
|
// The first String is student's name
|
||||||
// The Second Integer is student's score
|
// The Second Integer is student's score
|
||||||
Map<String, Integer> scoreMap = new HashMap<>();
|
Map<String, Integer> scoreMap = new HashMap<>();
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```java
|
||||||
// Student' name -> Student's score
|
// Student' name -> Student's score
|
||||||
Map<String, Integer> scoreMap = new HashMap<>();
|
Map<String, Integer> scoreMap = new HashMap<>();
|
||||||
```
|
```
|
||||||
|
|
||||||
添加测试用例来说明:
|
添加测试用例来说明:
|
||||||
|
|
||||||
```
|
```java
|
||||||
//...
|
//...
|
||||||
// Example: add(1, 2), return 3
|
// Example: add(1, 2), return 3
|
||||||
int add(int x, int y) {
|
int add(int x, int y) {
|
||||||
@ -110,7 +110,7 @@ int add(int x, int y) {
|
|||||||
|
|
||||||
在很复杂的函数调用中对每个参数标上名字:
|
在很复杂的函数调用中对每个参数标上名字:
|
||||||
|
|
||||||
```
|
```java
|
||||||
int a = 1;
|
int a = 1;
|
||||||
int b = 2;
|
int b = 2;
|
||||||
int num = add(\* x = *\ a, \* y = *\ b);
|
int num = add(\* x = *\ a, \* y = *\ b);
|
||||||
@ -122,13 +122,14 @@ int num = add(\* x = *\ a, \* y = *\ b);
|
|||||||
|
|
||||||
条件表达式中,左侧是变量,右侧是常数。比如下面第一个语句正确:
|
条件表达式中,左侧是变量,右侧是常数。比如下面第一个语句正确:
|
||||||
|
|
||||||
```
|
```java
|
||||||
if(len < 10)
|
if(len < 10)
|
||||||
if(10 > len)
|
if(10 > len)
|
||||||
```
|
```
|
||||||
|
|
||||||
if / else 条件语句,逻辑的处理顺序为:① 正逻辑;② 关键逻辑;③ 简单逻辑。
|
if / else 条件语句,逻辑的处理顺序为:① 正逻辑;② 关键逻辑;③ 简单逻辑。
|
||||||
```
|
|
||||||
|
```java
|
||||||
if(a == b) {
|
if(a == b) {
|
||||||
// 正逻辑
|
// 正逻辑
|
||||||
} else{
|
} else{
|
||||||
@ -148,11 +149,11 @@ do / while 的条件放在后面,不够简单明了,并且会有一些迷惑
|
|||||||
|
|
||||||
长表达式的可读性很差,可以引入一些解释变量从而拆分表达式:
|
长表达式的可读性很差,可以引入一些解释变量从而拆分表达式:
|
||||||
|
|
||||||
```
|
```python
|
||||||
if line.split(':')[0].strip() == "root":
|
if line.split(':')[0].strip() == "root":
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
```
|
```python
|
||||||
username = line.split(':')[0].strip()
|
username = line.split(':')[0].strip()
|
||||||
if username == "root":
|
if username == "root":
|
||||||
...
|
...
|
||||||
@ -160,12 +161,12 @@ if username == "root":
|
|||||||
|
|
||||||
使用摩根定理简化一些逻辑表达式:
|
使用摩根定理简化一些逻辑表达式:
|
||||||
|
|
||||||
```
|
```java
|
||||||
if(!a && !b) {
|
if(!a && !b) {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
```
|
```java
|
||||||
if(!(a || b)) {
|
if(!(a || b)) {
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
@ -175,7 +176,7 @@ if(!(a || b)) {
|
|||||||
|
|
||||||
**去除控制流变量** 。在循环中通过使用 break 或者 return 可以减少控制流变量的使用。
|
**去除控制流变量** 。在循环中通过使用 break 或者 return 可以减少控制流变量的使用。
|
||||||
|
|
||||||
```
|
```java
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
while(/* condition */ && !done) {
|
while(/* condition */ && !done) {
|
||||||
...
|
...
|
||||||
@ -198,7 +199,7 @@ while(/* condition */) {
|
|||||||
|
|
||||||
JavaScript 可以用闭包减小作用域。以下代码中 submit_form 是函数变量,submitted 变量控制函数不会被提交两次。第一个实现中 submitted 是全局变量,第二个实现把 submitted 放到匿名函数中,从而限制了起作用域范围。
|
JavaScript 可以用闭包减小作用域。以下代码中 submit_form 是函数变量,submitted 变量控制函数不会被提交两次。第一个实现中 submitted 是全局变量,第二个实现把 submitted 放到匿名函数中,从而限制了起作用域范围。
|
||||||
|
|
||||||
```
|
```js
|
||||||
submitted = false;
|
submitted = false;
|
||||||
var submit_form = function(form_name) {
|
var submit_form = function(form_name) {
|
||||||
if(submitted) {
|
if(submitted) {
|
||||||
@ -208,7 +209,7 @@ var submit_form = function(form_name) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```js
|
||||||
var submit_form = (function() {
|
var submit_form = (function() {
|
||||||
var submitted = false;
|
var submitted = false;
|
||||||
return function(form_name) {
|
return function(form_name) {
|
||||||
@ -228,7 +229,7 @@ JavaScript 中没有用 var 声明的变量都是全局变量,而全局变量
|
|||||||
|
|
||||||
在一个网页中有以下文本输入字段:
|
在一个网页中有以下文本输入字段:
|
||||||
|
|
||||||
```
|
```html
|
||||||
<input type = "text" id = "input1" value = "a">
|
<input type = "text" id = "input1" value = "a">
|
||||||
<input type = "text" id = "input2" value = "b">
|
<input type = "text" id = "input2" value = "b">
|
||||||
<input type = "text" id = "input3" value = "">
|
<input type = "text" id = "input3" value = "">
|
||||||
@ -237,7 +238,7 @@ JavaScript 中没有用 var 声明的变量都是全局变量,而全局变量
|
|||||||
|
|
||||||
现在要接受一个字符串并把它放到第一个空的 input 字段中,初始实现如下:
|
现在要接受一个字符串并把它放到第一个空的 input 字段中,初始实现如下:
|
||||||
|
|
||||||
```
|
```js
|
||||||
var setFirstEmptyInput = function(new_alue) {
|
var setFirstEmptyInput = function(new_alue) {
|
||||||
var found = false;
|
var found = false;
|
||||||
var i = 1;
|
var i = 1;
|
||||||
@ -261,7 +262,7 @@ var setFirstEmptyInput = function(new_alue) {
|
|||||||
- elem 作用域过大;
|
- elem 作用域过大;
|
||||||
- 可以用 for 循环代替 while 循环;
|
- 可以用 for 循环代替 while 循环;
|
||||||
|
|
||||||
```
|
```js
|
||||||
var setFirstEmptyInput = function(new_value) {
|
var setFirstEmptyInput = function(new_value) {
|
||||||
for(var i = 1; true; i++) {
|
for(var i = 1; true; i++) {
|
||||||
var elem = document.getElementById('input' + i);
|
var elem = document.getElementById('input' + i);
|
||||||
@ -284,7 +285,7 @@ var setFirstEmptyInput = function(new_value) {
|
|||||||
|
|
||||||
介绍性的代码:
|
介绍性的代码:
|
||||||
|
|
||||||
```
|
```java
|
||||||
int findClostElement(int[] arr) {
|
int findClostElement(int[] arr) {
|
||||||
int clostIdx;
|
int clostIdx;
|
||||||
int clostDist = Interger.MAX_VALUE;
|
int clostDist = Interger.MAX_VALUE;
|
||||||
@ -305,7 +306,7 @@ int findClostElement(int[] arr) {
|
|||||||
|
|
||||||
以上代码中循环部分主要计算距离,这部分不属于代码高层次目标,高层次目标是寻找最小距离的值,因此可以把这部分代替提取到独立的函数中。这样做也带来一个额外的好处有:可以单独进行测试、可以快速找到程序错误并修改。
|
以上代码中循环部分主要计算距离,这部分不属于代码高层次目标,高层次目标是寻找最小距离的值,因此可以把这部分代替提取到独立的函数中。这样做也带来一个额外的好处有:可以单独进行测试、可以快速找到程序错误并修改。
|
||||||
|
|
||||||
```
|
```java
|
||||||
public int findClostElement(int[] arr) {
|
public int findClostElement(int[] arr) {
|
||||||
int clostIdx;
|
int clostIdx;
|
||||||
int clostDist = Interger.MAX_VALUE;
|
int clostDist = Interger.MAX_VALUE;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user