跳到主要内容

我的编程之道

· 阅读需 6 分钟
  • 能不能做
  • 该怎么做
  • 这样做会遇到什么问题
  • 我有没有办法能够协调这些问题
  • 将来会有什么样的改变
  • 我需要运行在什么环境下
  • 用什么语言写
  • 运行时需要依赖什么条件
  • 测试是否通过

我的观念

缩短变量作用域

  • 可以减少代码错误
  • 可以重用变量名
  • 可以方便代码的重构
  • 模块化更强
  • 避免全局变量,有些全局变量可以用 singleton 模式来替代

写完整的单词,准确的名字

  • 可以减少对注释的需求
  • 代码更清晰,明白

适当的注释,可文档化的注释

  • 便于生成代码文档,方便查阅
  • 便于协作者使用你的代码和接口
  • 一般 ide 支持的注释可以直接显示出来,各种方便
  • 动态类型语言的文档可以提供更多的类型信息,比如 js
  • jsdoc,javadoc,doxgen,luadoc .....

尽量的偷懒

  • 在准备写之前多查查已经有的库 (github, google code, sf.net, ....)
  • 代码模块化越强,就越能偷懒
  • 写代码时,考虑下怎么做尽量小的修改就可以做其他的东西
  • 为重用而编码
  • 使用代码生成, lombok, less, dart .....

我的代码风格

缩进风格

我的代码风格主要和 Horstmann 相近,但是也有些不同的地方.

Horstmann 风格:

while (x == y)
{ something();
somethingelse();
//...
if (x < 0)
{ printf("Negative");
negative(x);
}
else
{ printf("Non-negative");
nonnegative(x);
}
}
finalthing();

我的风格:

while (x == y)
{
something();
somethingelse();
//...
if (x < 0)
{
printf("Negative");
negative(x);
}else
{
printf("Non-negative");
nonnegative(x);
}
}
finalthing();

个人观点:

  • 因为常用 {} 来缩减作用域,开括号换行这样让代码更清晰,也便于自动缩进
  • 因为 { 单独占了一行,这样让代码更长,但是也更多空白,看起来更轻松

英文单词的选择

title vs name

See http://stackoverflow.com/questions/5527632/c-sharp-naming-convention-title-vs-name

Name is used more frequently for an object with an internal name or for business objects that naturally has a Name property (such as a person)

Title is used more often to refer to a User Interface Control object or a business object that naturally has a title, like an article.

ID vs Id

See http://stackoverflow.com/questions/1151338/id-or-id-on-user-interface

Filename, FileName, filename

See http://english.stackexchange.com/questions/5366/which-is-correct-filename-file-name-or-filename

一般我选择 Filename.

OK vs Ok : OK

OK 是正确的写法

DB vs Db

See http://stackoverflow.com/questions/1345103/why-is-db-an-acronym-and-not-abbreviation

感觉 DB 比 Db 更好

标识符

  • 在 c 类函数式语言常用 _ 来分割单词, 因为 c 的函数大多是这个风格 e.g. void enter_stage();
  • 在 java 类 oo 语言常用驼峰命名 e.g. function enterStage()
  • 静态方法第一个字母大写
  • 用 set/get 前缀,并且 setter 是可链的. e.g Action setTime(int t){time = t; return this;}
  • 如果不用 set/get 则用 流式的 API.
class Action
{
int time;
Action time(int t){time = t; return this;}
int time(){ return time;}
}
  • 如果返回类型为 boolean 则用 is 作为 getter 前缀 e.g. boolean isTimeOut();
  • 在不原生支持 事件 的语言里,用 On 作为事件的前缀. e.g. void OnKeyInput(KeyEvent e);

我的实践

在一些特定语言里,我的习惯

JS

小技巧和习惯

// 从string转换为number
+numberString;

// 强制转换为 boolean
!!object;

// 比较时使用 ===
if(!!objA === !!objB)
{}

// 用前缀 $ 来标识node或jq查询
var $main = $('#main');
var $wener = document.querySelector('#wener');

// 用 []/{} 来初始化数组和类
var list = [];// 而不是 new Array()
var obj = {};// 而不是 new Object()

文件模式

// 文件 action.js
(function()
{
// code for action.js
})();

类模式

// 类 Action
(function(window)
{
function Action(){}
Action.prototype['doSomthing'] = function(){};
// export
// window['Action'] 比 window.Action 在压缩的时候更好
// google closure compiler 的导出方法
window['Action'] = Action;
})(window);

包/命名空间/类 模式

// 包/命名空间 ui
// label.js
(function(ui)
{
function Label(){}
// export
ui['Label'] = Label;
})(window.ui || (window.ui = {}));
// button.js
(function(ui)
{
function Button(){}
// export
ui['Button'] = Button;
})(window.ui || (window.ui = {}));

static 方法

// 类 Action
(function(window)
{
function Action(){}
Action['doSomthing'] = function(){};
// export
window['Action'] = Action;
})(window);

singleton 模式

// 类 Action
(function(window)
{
var instance = null;
function Action()
{
if(instance != null)
throw new Error("already init Action");
}
Action['getInstance'] = function()
{
if(instance == null)
instance = new Action();
return instance;
};
// export
window['Action'] = Action;
})(window);

避免使用一次性的匿名函数

  • 便于重构
  • 便于更改
  • 代码清晰
  • 可重用
function OnLoad(){}
window.onload = OnLoad; // 而不是 window.onload = function(){};

资源