八、开发者工具和指南(五) Code conventions

Definitions

  • Camel case首字母小写,词之间不加任何分隔字符,词的首字母大写
  • Pascal case 每个词的首字母大写,词之间不加任何分隔字符

C# Coding Conventions

C#编码约定文档:C# Coding Guidelines,下面的例外:

  • 开括号与语句块开始在同一行,前面多一个空格(这与javascript一致),又名K&R约定
  • 私有字段使用下划线”_"前缀,并采用camel-cased
  • using指字在namespace之前出现,不是在它里面

JavaScript Coding Conventions

  • Namespaces are Pascal-cased.
  • Class names are Pascal-cased.
  • Plugin names are Camel-cased.
  • Properties, fields, local variables are Camel-cased.
  • Parameters are Camel-cased.
  • Function names are Camel-cased unless they really are class constructors or namespaces (in other words, global/local functions and methods are Camel-cased).
  • Private/internal/protected members are underscore-prefixed and Camel-cased.
  • Constants are just static fields (apply same rules as for fields).
  • JavaScript coding conventions follow C# conventions except for Pascal vs. Camel.
  • " and ' are interchangeable (strictly equivalent). XHTML attributes should be in double quotes and if code needs to be in there, it has to use single quotes. ex: (note: this kind of DOM-0 event creating is itself discouraged and is only shown here as an example). In pure JS code, use double quotes for string delimiters. When the string is one character and the intent is a character, use single quote for consistency with managed code.
  • There is no need for String.Empty, just use "".
  • Localizable strings need to be isolated into resource dictionaries until we figure out our client localization story. ex. alert(Foo.badArgument); ... Foo = {badArgument: "Teh argument was bad."};
  • Don't worry about string concatenation unless you have specific evidence that regular concatenation is significantly harming performance in your specific scenario.
  • Use the K&R style for opening braces (put the opening brace on the opening line). This is because in JavaScript, the semicolon is optional, which can cause difficult to spot bugs (seehttp://msmvps.com/blogs/luisabreu/archive/2009/08/26/the-semicolon-bug.aspx for an example).
posted @ 2012-03-25 18:04  commanderss  阅读(352)  评论(0编辑  收藏  举报