yidabu 2007-4-26 18:38
45 D编程风格 The D Style
45 D编程风格 The D Style知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070426点击下面网址查看原文:http://www.d-programming-language-china.org by: uFramer D语言论坛 http://www.d-programming-language-china.org from: http://www.digitalmars.com/d/dstyle.html version: 基于D 1.012 (Apr 12, 2007) D 编程风格 是编写 D 程序的一系列风格习惯。D 编程风格并不由编译器强制实施,它纯粹是一种装饰和选择。但是,采用 D 编程风格将使其他人更容易理解你的代码,同时也会使你更容易在其他人的代码上工作。D 编程风格可以为你的项目团队奠定一个项目编码风格的基础。 提交到Phobos和其他官方源代码都要遵守下面的编程风格。White Space空白 一行一条语句。 Hardware tabs are at 8 column increments. Avoid using hardware tabs if they are set at a different value. Each indentation level will be four columns. 运算符和它们的操作数之间有一个空格。 函数体之间有两行的间距。 函数体中的变量声明和语句之间有一个空行。Comments注释 使用 // 为单行语句编写单行注释: [Copy to clipboard] [ - ]CODE: statement; // comment statement; // comment 使用块注释为多行语句编写多行注释: [Copy to clipboard] [ - ]CODE: /* * comment * comment */ statement; statement; 使用嵌套注释‘注释掉’一块实验性代码:( 本文出处: http://www.d-programming-language-china.org ) [Copy to clipboard] [ - ]CODE: /+++++ /* * comment * comment */ statement; statement; +++++/ Naming Conventions命名习惯 1 一般规则 如果名字是由多个单词组成的话,每个单词的开头字母都应该大写以同其他单词区分开来。名字不要用下划线'_'开头。 [Copy to clipboard] [ - ]CODE: int myFunc(); 2 模块 模块名和包名全部小写,只用字符[a..z][0..9][_]。这样可以避免在某些不区分大小写文件系统中造成问题。 C 模块 同 C 函数接口的模块位于 "c" 包中,例如: [Copy to clipboard] [ - ]CODE: import std.c.stdio; 模块名全部小写。 3 类、结构、联合、枚举和模板名 开头字母大写。( 本文出处: http://www.d-programming-language-china.org ) [Copy to clipboard] [ - ]CODE: class Foo; class FooAndBar; 4 函数名 函数名开头字母不大写。 [Copy to clipboard] [ - ]CODE: int done(); int doneProcessing(); 5 常量名 所有字母大写。 6 枚举成员名 所有字母大写。Meaningless Type Aliases无意义的类型名 类似于: [Copy to clipboard] [ - ]CODE: alias void VOID; alias int INT; alias int* pint; 的东西应该避免。( 本文出处: http://www.d-programming-language-china.org )Declaration Style声明风格 因为在 D 中声明是左结合的,将它们向左对齐: [Copy to clipboard] [ - ]CODE: int[] x, y; // 含义清楚,x 和 y 类型相同 int** p, q; // 含义清楚,p 和 q 类型相同 以强调它们之间的关系。不要使用 C 风格的声明: [Copy to clipboard] [ - ]CODE: int []x, y; // 令人迷惑,因为 y 也是 int[] int **p, q; // 令人迷惑,因为 q 也是 int** Operator Overloading运算符重载 运算符重载是扩展语言基本类型的有力工具。但是也因为十分有力,它也很可能使代码令人费解。尤其是,现有的 D 运算符都有其约定俗成的含义,如‘+’的意思是‘加’,‘<<’的意思是‘左移’。重载运算符‘+’使其的含义不同于‘加’显然会引起误解,这种情况应该避免。Hungarian Notation匈牙利命名法 Using hungarian notation to denote the type of a variable is a bad idea. However, using notation to denote the purpose of a variable (that cannot be expressed by its type) is often a good practice.Documentation文档 公开的文档应该用Ddoc格式.( 本文出处: http://www.d-programming-language-china.org )Unit Tests单元测试 As much as practical, all functions will be exercised by unit tests using unittest blocks immediately following the function to be tested. Every path of code should be executed at least once, verified by the code coverage analyzer.( lastupdate:20070426 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:yidabu提倡在交流中学习,在分享中提高收集感兴趣的知识,写下心得,通过网络与别人一起分享理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台