(微软一站式代码示例)编程规范(4)

2019-03-10 16:10

Page 12

?一定请利用2进制强大的能力,因为它可以自由的进行位异或运算。举例,

// C++ sample:

enum AttributeTargets {

Assembly = 0x0001, Class = 0x0002, Struct = 0x0004 ... };

// C# sample: [Flags]

publicenumAttributeTargets {

Assembly = 0x0001, Class = 0x0002, Struct = 0x0004, ... }

' VB.NET sample:

_

PublicEnum AttributeTargets Assembly = &H1 Class = &H2 Struct = &H4 ... EndEnum

?您应该提供一些特殊的枚举值,以便进行常见的标志枚举的组合运算。位运算属于高级任务,所以在简单任务中无需使用它们。FileAccess.ReadWrite 便是标志枚举特殊值的一个示例。然而,如果一个标志枚举中的某些值组合起来是非法的,您就不应该创建这样的标志枚举。

// C++ sample: enum FileAccess {

Read = 0x1, Write = 0x2,

ReadWrite = Read | Write };

// C# sample:

? 2015 Microsoft Corporation. All rights reserved. All-In-One Code Framework (http://1code.codeplex.com)

Page 13

[Flags]

publicenumFileAccess {

Read = 0x1, Write = 0x2,

ReadWrite = Read | Write }

' VB.NET sample: _

PublicEnum FileAccess Read = &H1 Write = &H2

ReadWrite = Read Or Write EndEnum

?您不应该在标志枚举中使用0值,除非它代表“所有标志被清除了”,并被恰当的命名为类似“None”的名字。如下C#示例展示了一常见的检查某一标志是否被设置了的实现(查看如下 if-语句)。该检查运行结果正确,但是有一处例外,便是对于0值的检查,它的布尔表达式结果恒为true。

Bad:

[Flags]

publicenumSomeFlag {

ValueA = 0, // This might be confusing to users ValueB = 1, ValueC = 2,

ValueBAndC = ValueB | ValueC, }

SomeFlag flags = GetValue();

if((flags &SomeFlag.ValueA) == SomeFlag.ValueA) { ... }

Good:

[Flags]

publicenumBorderStyle {

Fixed3D = 0x1, FixedSingle = 0x2, None = 0x0 }

if(foo.BorderStyle == BorderStyle.None)

? 2015 Microsoft Corporation. All rights reserved. All-In-One Code Framework (http://1code.codeplex.com)

Page 14

{ ... }

2.9 空格

2.9.1 空行

?您应该使用空行来分隔相关语句块。省略额外的空行会加大代码阅读难度。比如,您可以在变量声明和代码之间有一行空行。

Good:

// C++ sample:

void ProcessItem(const Item& item) {

int counter = 0;

if(...) { } }

Bad:

// C++ sample:

void ProcessItem(const Item& item) {

int counter = 0;

// Implementation starts here // if(...) { } }

在本例中,过多的空行造成了空行滥用,并不能使代码更易于阅读。

?您应该使用2行空行来分隔方法实现或类型声明。

? 2015 Microsoft Corporation. All rights reserved. All-In-One Code Framework (http://1code.codeplex.com)

Page 15

2.9.2 空格

空格通过降低代码密度以增加可读性。以下是使用空格符的一些指导规范:

?您应该像如下般在一行代码中使用空格。

Good:

// C++ / C# sample:

CreateFoo();// No space between function name and parenthesis Method(myChar, 0, 1);// Single space after a comma x = array[index];// No spaces inside brackets

while(x == y)// Single space before flow control statements if(x == y)// Single space separates operators

' VB.NET sample:

CreateFoo()' No space between function name and parenthesis Method(myChar, 0, 1)' Single space after a comma x = array(index)' No spaces inside brackets

While(x = y)' Single space before flow control statements If(x = y)Then ' Single space separates operators

Bad:

// C++ / C# sample:

CreateFoo ();// Space between function name and parenthesis Method(myChar,0,1);// No spaces after commas

CreateFoo( myChar, 0, 1 );// Space before first arg, after last arg x = array[ index ];// Spaces inside brackets

while(x == y)// No space before flow control statements if(x==y)// No space separates operators

' VB.NET sample:

CreateFoo ()' Space between function name and parenthesis Method(myChar,0,1)' No spaces after commas

CreateFoo( myChar, 0, 1 )' Space before first arg, after last arg x = array( index )' Spaces inside brackets

While(x = y)' No space before flow control statements If(x=y)Then ' No space separates operators

2.10 大括号

?一定请在一站式示例代码库的代码示例中使用Allman风格的大括号用法。

? 2015 Microsoft Corporation. All rights reserved. All-In-One Code Framework (http://1code.codeplex.com)

Page 16

Allman 风格是以Eric Allman命名的,有时也被称为\风格\。该风格将大括号与相关代码置于下一行内,与控制语句的缩进相同。大括号内的语句缩进一个等级。

Good:

// C++ / C# sample: if(x > 5) { y = 0; }

' VB.NET sample: If(x > 5)Then y = 0 EndIf

Bad(in All-In-One Code Framework samples):

// C++ / C# sample: if(x > 5) { y = 0; }

?您应该在即使是单行条件式的情况下也使用大括号。这样做使得将来增加条件式更简便,并减少制表符引起的歧义。

Good:

// C++ / C# sample: if(x > 5) { y = 0; }

' VB.NET sample: If(x > 5)Then y = 0 EndIf

Bad:

// C++ / C# sample: if(x > 5) y = 0;

' VB.NET sample: If(x > 5)Then y = 0

? 2015 Microsoft Corporation. All rights reserved. All-In-One Code Framework (http://1code.codeplex.com)


(微软一站式代码示例)编程规范(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:一年级语文综合实践活动教案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: