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

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== of 还是 for == 遇到一个命名上很纠结的问题,假如我有一个表 User,现在想向表 User 中添加模拟数据,创建函数过程中应该…”
 
Kangkk​(留言 | 贡献)
 
(未显示同一用户的34个中间版本)
第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 create UI for the user to interact with your add-in?
{| class="wikitable"
*:: eq: Create a directory for the user and give him the necessary permissions.
|-
* create of user :“创建用户”
! VI
*:: eq: This would mean you have to manually create a number of user subscriptions,
| Ctrl + p
*:: eq: It may take a few seconds to create all of the new user's 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]] || 示例 || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}