编程语言/命名:修订间差异
来自康健生活
| (未显示同一用户的22个中间版本) | |||
| 第2行: | 第2行: | ||
原则<ref>Dustin Boswell, Trevor Foucher. 编写可读代码的艺术(The Art of Readable Code)[M].尹哲, 郑秀雯. 北京:机械工业出版社,2012:12-16.</ref>: | 原则<ref>Dustin Boswell, Trevor Foucher. 编写可读代码的艺术(The Art of Readable Code)[M].尹哲, 郑秀雯. 北京:机械工业出版社,2012:12-16.</ref>: | ||
* 选择专业的词汇 | * 选择专业的词汇 | ||
*:: eq: 如果是获取一个缓存网页,使用 <code>getPage</code> 不如 <code>fetchPage</code>、<code>downloadPage</code> 更语义化。 | |||
* 选择更具表现力的词汇 | * 选择更具表现力的词汇 | ||
* 避免模糊的词汇 | * 避免模糊的词汇 | ||
*:: eq: <code>tmp</code>、<code>retval</code>、<code>get</code>、等。 | |||
* 避免简洁的词汇 | * 避免简洁的词汇 | ||
* 避免使用过长的词汇 | |||
* 作用域内可适当使用短词汇 | |||
* 适当缩略词汇 | |||
*:: eq: <code>string</code>-><code>str</code>、<code>array</code>-><code>arr</code>、等。 | |||
* 附带更多信息在词尾 | * 附带更多信息在词尾 | ||
*:: eq: <code>_ms</code> 毫秒、<code>_secs</code>、<code>_mb</code>、<code>_kbps</code> | *:: eq: <code>_ms</code> 毫秒、<code>_secs</code>、<code>_mb</code>、<code>_kbps</code>、等。 | ||
== 单词补全 == | |||
在众多编辑器中,大部分都集成了单词补全功能。 | |||
{| class="wikitable" | |||
|- | |||
! VI | |||
| Ctrl + p | |||
! EMACS | |||
| esc + / | |||
! ECLIPSE | |||
| Alt + / | |||
|- | |||
! INTELLIJ IDEA | |||
| Alt + / | |||
! WEBSTORM | |||
| Alt + / | |||
! TEXTMATE | |||
| Esc | |||
|} | |||
== 组成 == | |||
使用{{underline|大写|red}}、{{underline|小写|red}}、{{underline|数字|red}}、{{underline|中划线(-)|red}}、{{underline|下划线(_)|red}}、等组成。 | |||
== 格式 == | |||
=== 区间 === | |||
* 使用 <code>min</code>、<code>max</code> 表示<code>极限</code>。 | |||
* 使用 <code>first</code>、<code>last</code> 或 <code>begin</code>、<code>end</code> 表示 <code>范围</code>。 | |||
=== 布尔 === | |||
确保语义明确。 | |||
== 命名方法 == | == 命名方法 == | ||
| 第13行: | 第48行: | ||
=== 函数命名 === | === 函数命名 === | ||
==== 普通函数命名 ==== | ==== 普通函数命名 ==== | ||
构造函数首字母应该{{underline|小| | 构造函数首字母应该{{underline|小|blue}}写<ref>Douglas Crockford. JavaScript: The Good Parts. O'Reily. 2008.</ref>。 | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
var | var nowDateStrLength = dateStrLength(nowDate); // dateStrLength is a ordinary function.</syntaxhighlight> | ||
==== 构造函数命名 ==== | ==== 构造函数命名 ==== | ||
构造函数首字母应该{{underline|大| | 构造函数首字母应该{{underline|大|blue}}写。 | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
var nowDate = new DateFormat(); // DateFormat is a constructor function.</syntaxhighlight> | var nowDate = new DateFormat(); // DateFormat is a constructor function.</syntaxhighlight> | ||
=== 常量 === | |||
使用驼峰命名法。 | |||
<syntaxhighlight lang="javascript"> | |||
var contentLength = 100;</syntaxhighlight> | |||
=== 变量 === | |||
=== 类 === | |||
==== 类成员变量 ==== | |||
与普通变量相似,但以下划线( _ )结尾。 | |||
eq: | |||
<syntaxhighlight lang="javascript"> | |||
var offset_;</syntaxhighlight> | |||
=== 宏 === | |||
在 python 中定义宏。 | |||
<syntaxhighlight lang="python"> | |||
#define MACRO_NAME</syntaxhighlight> | |||
== 实例 == | == 实例 == | ||