编程语言/命名:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的33个中间版本)
第1行: 第1行:
== of 还是 for ==
== 引言 ==
遇到一个命名上很纠结的问题,假如我有一个表 User,现在想向表 User 中添加模拟数据,创建函数过程中应该如何为这个添加模拟数据的逻辑函数命名呢?
原则<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>、等。


有两种选择,哪个合适:
== 单词补全 ==
* create for user :“为用户创建”
在众多编辑器中,大部分都集成了单词补全功能。
*:: eq: Would you like to {{Underline|create|red}} UI {{Underline|for|red}} the {{Underline|user|red}} to interact with your add-in?
{| class="wikitable"
*:: eq: {{Underline}}Create a directory {{Underline}}for the {{Underline}}user and give him the necessary permissions.
|-
* create of user :“创建用户”
! VI
*:: eq: This would mean you have to manually {{Underline|create|red}} a number {{Underline|of user|red}} subscriptions,
| Ctrl + p
*:: eq: It may take a few seconds to {{Underline|create|red}} all {{Underline|of|red}} the new {{Underline|user's|red}} files.
! 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>。
 
=== 布尔 ===
确保语义明确。
 
== 命名方法 ==
=== 匈牙利表示法 ===
== 其他 ==
=== 函数命名 ===
==== 普通函数命名 ====
构造函数首字母应该{{underline|小|blue}}写<ref>Douglas Crockford. JavaScript: The Good Parts. O'Reily. 2008.</ref>。
<syntaxhighlight lang="javascript">
var nowDateStrLength = dateStrLength(nowDate); // dateStrLength is a ordinary function.</syntaxhighlight>
 
==== 构造函数命名 ====
构造函数首字母应该{{underline|大|blue}}写。
<syntaxhighlight lang="javascript">
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>
 
== 实例 ==
{| class="wikitable"
|-
| [[/of还是for|of还是for]] || 示例 || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}

2020年6月12日 (五) 05:49的最新版本

引言

原则[1]:

  • 选择专业的词汇
    eq: 如果是获取一个缓存网页,使用 getPage 不如 fetchPage、downloadPage 更语义化。
  • 选择更具表现力的词汇
  • 避免模糊的词汇
    eq: tmp、retval、get、等。
  • 避免简洁的词汇
  • 避免使用过长的词汇
  • 作用域内可适当使用短词汇
  • 适当缩略词汇
    eq: string->str、array->arr、等。
  • 附带更多信息在词尾
    eq: _ms 毫秒、_secs、_mb、_kbps、等。

单词补全

在众多编辑器中,大部分都集成了单词补全功能。

VI Ctrl + p EMACS esc + / ECLIPSE Alt + /
INTELLIJ IDEA Alt + / WEBSTORM Alt + / TEXTMATE Esc

组成

使用大写、小写、数字、中划线(-)、下划线(_)、等组成。

格式

区间

  • 使用 min、max 表示极限。
  • 使用 first、last 或 begin、end 表示 范围。

布尔

确保语义明确。

命名方法

匈牙利表示法

其他

函数命名

普通函数命名

构造函数首字母应该小写[2]。

var nowDateStrLength = dateStrLength(nowDate); // dateStrLength is a ordinary function.

构造函数命名

构造函数首字母应该大写。

var nowDate = new DateFormat(); // DateFormat is a constructor function.

常量

使用驼峰命名法。

var contentLength = 100;

变量

类

类成员变量

与普通变量相似,但以下划线( _ )结尾。 eq:

var offset_;

宏

在 python 中定义宏。

#define MACRO_NAME

实例

of还是for 示例 示例
示例 示例 示例
示例 示例 示例
  1. ↑ Dustin Boswell, Trevor Foucher. 编写可读代码的艺术(The Art of Readable Code)[M].尹哲, 郑秀雯. 北京:机械工业出版社,2012:12-16.
  2. ↑ Douglas Crockford. JavaScript: The Good Parts. O'Reily. 2008.