he:dokuwiki
הבדלים
כאן מוצגים ההבדלים בין הגרסה שנבחרה והגרסה הנוכחית של הדף.
גירסה קודמת בשני הצדדיםהגירסה הקודמתהגירסה הבאה | הגירסה הקודמת | ||
he:dokuwiki [2013-07-27 00:36] – 79.182.145.170 | he:dokuwiki [2015-12-20 23:54] (הגרסה הנוכחית) – נמחק ach | ||
---|---|---|---|
שורה 1: | שורה 1: | ||
- | <div dir=" | ||
- | קונבנציות קוד מטרתן ליצור אחידות בעת כתיבת קוד מקור של ספריות או אפליקציות ובכך להקל על הקריאות והתמיכתיות של קוד המקור בהווה ובעתיד. להלן הגדרת הצורך בשמירה על קונבנציות קוד אחידות (מתוך ויקיפדיה): | ||
- | </ | ||
- | |||
- | <div dir=" | ||
- | < | ||
- | Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language. These conventions usually cover file organisation, | ||
- | </ | ||
- | </ | ||
- | |||
- | <div dir=" | ||
- | בתי תוכנה שונים יוצרים עבור עצמם סט של חוקים וכללים מנחים התואמים את התרבות הארגונית, | ||
- | ערך זה בא להציג את אוסף הכללים והקונבציות המרכיבות את הקונבנציות הקוד של הענף. כללים אלה מקיפים תחומים רבים של פיתוח קוד, החל מקונבנצית השמות של משתנים ומחלקות ועד לצורת הכינון וסידור קבצי קוד המקור. | ||
- | |||
- | תוכן הערך הנ" | ||
- | </ | ||
- | |||
- | <div dir=" | ||
- | |||
- | |||
- | == Naming Conventions == | ||
- | Naming guidelines provide guidance on selecting appropriate identifiers for the elements that make up class libraries including assemblies, namespaces, types, members, and parameters. Choosing identifiers that conform to these guidelines improves the usability of your library and encourages users to trust that your library will not require learning a new set of conventions. | ||
- | |||
- | To provide a consistent developer experience, these guidelines are required for publicly exposed elements such as public classes and protected methods. However, for consistency throughout your code and improved maintainability, | ||
- | |||
- | The guidelines are organised as simple recommendations prefixed with the terms ''' | ||
- | |||
- | |||
- | === General === | ||
- | This section describes general naming conventions that relate to word choice, guidelines on using abbreviations and acronyms, and recommendations on how to avoid using language-specific names. | ||
- | |||
- | ==== Word Choice ==== | ||
- | *√ DO choose easily readable identifier names. | ||
- | For example, a property named < | ||
- | |||
- | *√ DO favor readability over brevity. | ||
- | The property name < | ||
- | |||
- | *X DO NOT use underscores, | ||
- | |||
- | *X DO NOT use Hungarian notation. | ||
- | |||
- | *X AVOID using identifiers that conflict with keywords of widely used programming languages. | ||
- | According to Rule 4 of the Common Language Specification (CLS), all compliant languages must provide a mechanism that allows access to named items that use a keyword of that language as an identifier. C#, for example, uses the @ sign as an escape mechanism in this case. However, it is still a good idea to avoid common keywords because it is much more difficult to use a method with the escape sequence than one without it. | ||
- | |||
- | ==== Abbreviations and Acronyms ==== | ||
- | *X DO NOT use abbreviations or contractions as part of identifier names. | ||
- | For example, use < | ||
- | *X DO NOT use any acronyms that are not widely accepted, and even if they are, only when necessary. | ||
- | |||
- | ==== Language-Specific Names ==== | ||
- | *√ DO use semantically interesting names rather than language-specific keywords for type names. | ||
- | For example, < | ||
- | |||
- | *√ DO use a generic CLR type name, rather than a language-specific name, in the rare cases when an identifier has no semantic meaning beyond its type. | ||
- | |||
- | *√ DO use a common name, such as '' | ||
- | |||
- | Do use a generic common language runtime (CLR) type name, rather than a language-specific name, in the rare cases when an identifier has no semantic meaning beyond its type. | ||
- | For example, a method that converts data to < | ||
- | |||
- | |||
- | === Capitalization Conventions === | ||
- | The guidelines in this chapter lay out a simple method for using case that, when applied consistently, | ||
- | |||
- | ==== Identifiers ==== | ||
- | To differentiate words in an identifier, capitalize the first letter of each word in the identifier. Do not use underscores to differentiate words, or for that matter, anywhere in identifiers. There are two appropriate ways to capitalize identifiers, | ||
- | *PascalCasing | ||
- | *camelCasing | ||
- | |||
- | The PascalCasing convention, used for all identifiers except parameter names, capitalizes the first character of each word (including acronyms over two letters in length), as shown in the following examples: < | ||
- | |||
- | A special case is made for two-letter acronyms in which both letters are capitalized, | ||
- | |||
- | The camelCasing convention, used only for parameter names, capitalizes the first character of each word except the first word, as shown in the following examples. As the example also shows, two-letter acronyms that begin a camel-cased identifier are both lowercase: < | ||
- | |||
- | *√ DO use PascalCasing for all public member, type, and namespace names consisting of multiple words. | ||
- | *√ DO use camelCasing for parameter names. | ||
- | The following table describes the capitalization rules for different types of identifiers. | ||
- | |||
- | {| class=" | ||
- | |- | ||
- | ! Identifier !! Casing !! Example | ||
- | |- | ||
- | | Namespace || Pascal | ||
- | |- | ||
- | | Type || Pascal | ||
- | |- | ||
- | | Interface || Pascal | ||
- | |- | ||
- | | Method || Pascal | ||
- | public virtual string ToString(); | ||
- | }</ | ||
- | |- | ||
- | | Property || Pascal | ||
- | public int Length { get; } | ||
- | }</ | ||
- | |- | ||
- | | Event || Pascal | ||
- | public event EventHandler Exited; | ||
- | }</ | ||
- | |- | ||
- | | Field || Pascal | ||
- | public static readonly TimeSpan | ||
- | InfiniteTimeout; | ||
- | } | ||
- | public struct UInt32 { | ||
- | public const Min = 0; | ||
- | }</ | ||
- | |- | ||
- | | Enum value || Pascal | ||
- | Append, | ||
- | ... | ||
- | }</ | ||
- | |- | ||
- | | Parameter || Camel || <source lang=Csharp enclose=none> | ||
- | public static int ToInt32(string value); | ||
- | }</ | ||
- | |} | ||
- | |||
- | ==== Capitalizing Compound Words and Common Terms ==== | ||
- | Most compound terms are treated as single words for purposes of capitalization. | ||
- | *X DO NOT capitalize each word in so-called closed-form compound words. | ||
- | These are compound words written as a single word, such as endpoint. For the purpose of casing guidelines, treat a closed-form compound word as a single word. | ||
- | |||
- | ==== Case Sensitivity ==== | ||
- | Languages that can run on the CLR are not required to support case-sensitivity, | ||
- | *X DO NOT assume that all programming languages are case sensitive. They are not. Names cannot differ by case alone. | ||
- | |||
- | |||
- | === Names of Assemblies and DLLs === | ||
- | |||
- | An assembly is the unit of deployment and identity for managed code programs. Although assemblies can span one or more files, typically an assembly maps one-to-one with a DLL. Therefore, this section describes only DLL naming conventions, | ||
- | |||
- | *√ DO choose names for your assembly DLLs that suggest large chunks of functionality, | ||
- | Assembly and DLL names don’t have to correspond to namespace names, but it is reasonable to follow the namespace name when naming assemblies. A good rule of thumb is to name the DLL based on the common prefix of the assemblies contained in the assembly. For example, an assembly with two namespaces, < | ||
- | |||
- | *√ CONSIDER naming DLLs according to the following pattern: < | ||
- | |||
- | |||
- | === Names of Namespaces === | ||
- | As with other naming guidelines, the goal when naming namespaces is creating sufficient clarity for the programmer using the framework to immediately know what the content of the namespace is likely to be. The following template specifies the general rule for naming namespaces: | ||
- | < | ||
- | The following are examples: | ||
- | |||
- | < | ||
- | *√ DO prefix namespace names with a company name to prevent namespaces from different companies from having the same name. | ||
- | *√ DO use a stable, version-independent product name at the second level of a namespace name. | ||
- | *X DO NOT use organisational hierarchies as the basis for names in namespace hierarchies, | ||
- | *√ DO use PascalCasing, | ||
- | *√ CONSIDER using plural namespace names where appropriate. | ||
- | For example, use < | ||
- | *X DO NOT use the same name for a namespace and a type in that namespace. | ||
- | For example, do not use Debug as a namespace name and then also provide a class named Debugin the same namespace. Several compilers require such types to be fully qualified. | ||
- | ==== Namespaces and Type Name Conflicts ==== | ||
- | *X DO NOT introduce generic type names such as < | ||
- | There is a very high probability that doing so will lead to type name conflicts in common scenarios. You should qualify the generic type names (< | ||
- | There are specific guidelines for avoiding type name conflicts for different categories of namespaces. | ||
- | *''' | ||
- | **X DO NOT give the same name to types in namespaces within a single application model. For example, do not add a type named Page to the < | ||
- | *''' | ||
- | *''' | ||
- | **X DO NOT give types names that would conflict with any type in the aCore namespaces. For example, never use < | ||
- | *''' | ||
- | **X DO NOT assign type names that would conflict with other types within a single technology. | ||
- | **X DO NOT introduce type name conflicts between types in technology namespaces and an application model namespace (unless the technology is not intended to be used with the application model). | ||
- | |||
- | |||
- | === Names of Classes, Structs, and Interfaces === | ||
- | The naming guidelines that follow apply to general type naming. | ||
- | *√ DO name classes and structs with nouns or noun phrases, using PascalCasing. | ||
- | This distinguishes type names from methods, which are named with verb phrases. | ||
- | *√ DO name interfaces with adjective phrases, or occasionally with nouns or noun phrases. | ||
- | Nouns and noun phrases should be used rarely and they might indicate that the type should be an abstract class, and not an interface. | ||
- | *X DO NOT give class names a prefix (e.g., " | ||
- | *√ CONSIDER ending the name of derived classes with the name of the base class. | ||
- | This is very readable and explains the relationship clearly. Some examples of this in code are: < | ||
- | *√ DO prefix interface names with the letter I, to indicate that the type is an interface. | ||
- | For example, < | ||
- | *√ DO ensure that the names differ only by the " | ||
- | ==== Name of Generic Type Parameters ==== | ||
- | *√ DO name generic type parameters with descriptive names unless a single-letter name is completely self-explanatory and a descriptive name would not add value. | ||
- | *√ CONSIDER using T as the type parameter name for types with one single-letter type parameter. | ||
- | <source lang=csharp enclose=pre> | ||
- | public delegate bool Predicate< | ||
- | public struct Nullable< | ||
- | </ | ||
- | *√ DO prefix descriptive type parameter names with T. | ||
- | <source lang=csharp enclose=pre> | ||
- | { | ||
- | TSession Session { get; } | ||
- | } | ||
- | </ | ||
- | *√ CONSIDER indicating constraints placed on a type parameter in the name of the parameter. | ||
- | For example, a parameter constrained to < | ||
- | |||
- | ==== Names of Common Types ==== | ||
- | *√ DO follow the guidelines described in the following table when naming types derived from or implementing certain .NET Framework types. | ||
- | |||
- | {| class=" | ||
- | |- | ||
- | ! Base Type!! Derived/ | ||
- | |- | ||
- | | < | ||
- | |- | ||
- | | < | ||
- | √ DO add the suffix " | ||
- | X DO NOT add the suffix " | ||
- | |- | ||
- | | < | ||
- | |- | ||
- | |< | ||
- | X DO NOT add the suffix " | ||
- | |- | ||
- | | < | ||
- | |- | ||
- | | < | ||
- | |- | ||
- | | < | ||
- | |- | ||
- | | < | ||
- | |- | ||
- | | < | ||
- | |} | ||
- | |||
- | ==== Naming Enumerations ==== | ||
- | Names of enumeration types (also called enums) in general should follow the standard type-naming rules (PascalCasing, | ||
- | *√ DO use a singular type name for an enumeration unless its values are bit fields. | ||
- | *√ DO use a plural type name for an enumeration with bit fields as values, also called flags enum. | ||
- | *X DO NOT use an " | ||
- | *X DO NOT use " | ||
- | *X DO NOT use a prefix on enumeration value names (e.g., " | ||
- | |||
- | ==== Interop Classes ==== | ||
- | Classes that are there for interop wrappers (< | ||
- | *''' | ||
- | *''' | ||
- | *''' | ||
- | Examples: | ||
- | <source lang=csharp enclose=pre> | ||
- | { | ||
- | | ||
- | |||
- | | ||
- | | ||
- | } | ||
- | |||
- | [SuppressUnmanagedCode] | ||
- | class UnsafeNativeMethods | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | [SuppressUnmanagedCode] | ||
- | class SafeNativeMethods | ||
- | { | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | </ | ||
- | All interop classes must be ''' | ||
- | |||
- | === Names of Type Members === | ||
- | |||
- | Types are made of members: methods, properties, events, constructors, | ||
- | |||
- | ==== Names of Methods ==== | ||
- | Because methods are the means of taking action, the design guidelines require that method names be verbs or verb phrases. Following this guideline also serves to distinguish method names from property and type names, which are noun or adjective phrases. | ||
- | *√ DO give methods names that are verbs or verb phrases. | ||
- | <source lang=csharp enclose=pre> | ||
- | | ||
- | | ||
- | | ||
- | }</ | ||
- | |||
- | ==== Names of Properties ==== | ||
- | Unlike other members, properties should be given noun phrase or adjective names. That is because a property refers to data, and the name of the property reflects that. PascalCasing is always used for property names. | ||
- | *√ DO name properties using a noun, noun phrase, or adjective. | ||
- | *X DO NOT have properties that match the name of " | ||
- | <source lang=csharp enclose=pre> | ||
- | public string GetTextWriter(int value) { ... } </ | ||
- | This pattern typically indicates that the property should really be a method. | ||
- | *√ DO name collection properties with a plural phrase describing the items in the collection instead of using a singular phrase followed by " | ||
- | *√ DO name Boolean properties with an affirmative phrase (CanSeek instead of CantSeek). Optionally, you can also prefix Boolean properties with " | ||
- | *√ CONSIDER giving a property the same name as its type. | ||
- | For example, the following property correctly gets and sets an enum value named Color, so the property is named Color: | ||
- | <source lang=csharp enclose=pre> | ||
- | public class Control { | ||
- | | ||
- | }</ | ||
- | |||
- | ==== Names of Events ==== | ||
- | Events always refer to some action, either one that is happening or one that has occurred. Therefore, as with methods, events are named with verbs, and verb tense is used to indicate the time when the event is raised. | ||
- | *√ DO name events with a verb or a verb phrase. | ||
- | Examples include < | ||
- | *√ DO give events names with a concept of before and after, using the present and past tenses. | ||
- | For example, a close event that is raised before a window is closed would be called < | ||
- | *X DO NOT use " | ||
- | *√ DO name event handlers (delegates used as types of events) with the " | ||
- | *√ DO use two parameters named '' | ||
- | The sender parameter represents the object that raised the event. The sender parameter is typically of type < | ||
- | *√ DO name event argument classes with the " | ||
- | |||
- | ==== Names of Fields ==== | ||
- | |||
- | The field-naming guidelines apply to static public and protected fields. Internal and private fields are not covered by guidelines, and public or protected instance fields are not allowed by the [http:// | ||
- | *√ DO use PascalCasing in field names. | ||
- | *√ DO name fields using a noun, noun phrase, or adjective. | ||
- | *X DO NOT use a prefix for field names. | ||
- | For example, do not use " | ||
- | |||
- | |||
- | === Names of Parameters === | ||
- | Beyond the obvious reason of readability, | ||
- | *√ DO use camelCasing in parameter names. | ||
- | *√ DO use descriptive parameter names. | ||
- | *√ CONSIDER using names based on a parameter’s meaning rather than the parameter’s type. | ||
- | ==== Naming Operator Overload Parameters ==== | ||
- | *√ DO use '' | ||
- | *√ DO use '' | ||
- | *√ CONSIDER meaningful names for operator overload parameters if doing so adds significant value. | ||
- | *X DO NOT use abbreviations or numeric indices for operator overload parameter names. | ||
- | |||
- | === Names of Resources === | ||
- | Because localizable resources can be referenced via certain objects as if they were properties, the naming guidelines for resources are similar to property guidelines. | ||
- | *√ DO use PascalCasing in resource keys. | ||
- | *√ DO provide descriptive rather than short identifiers. | ||
- | *X DO NOT use language-specific keywords of the main CLR languages. | ||
- | *√ DO use only alphanumeric characters and underscores in naming resources. | ||
- | *√ DO use the following naming convention for exception message resources. | ||
- | The resource identifier should be the exception type name plus a short identifier of the exception: < | ||
- | ArgumentExceptionInvalidName, | ||
- | ArgumentExceptionFileNameIsMalformed | ||
- | </ | ||
- | |||
- | |||
- | == Style Guidelines == | ||
- | |||
- | === Tabs and Indenting === | ||
- | *√ DO Tab characters (< | ||
- | |||
- | |||
- | === Bracing === | ||
- | *√ DO Open braces should always be at the beginning of the line after the statement that begins the block. Contents of the brace should be indented by 4 spaces. For example: | ||
- | |||
- | <source lang=csharp enclose=pre> | ||
- | { | ||
- | | ||
- | } | ||
- | else | ||
- | { | ||
- | | ||
- | } | ||
- | </ | ||
- | |||
- | *√ DO “case” statements should be indented from the switch statement like this: | ||
- | <source lang=csharp enclose=pre> | ||
- | |||
- | { | ||
- | case 0: | ||
- | DoSomething(); | ||
- | break; | ||
- | case 1: | ||
- | DoSomethingElse(); | ||
- | break; | ||
- | case 2: | ||
- | { | ||
- | int n = 1; | ||
- | DoAnotherThing(n); | ||
- | } | ||
- | break; | ||
- | }</ | ||
- | |||
- | *√ DO NOT Braces should never be considered optional. Even for single statement blocks, you should always use braces. This increases code readability and maintainability. For example: | ||
- | <source lang=csharp enclose=pre> | ||
- | |||
- | |||
- | === Single Line Statements === | ||
- | *√ DO Single line statements can have braces that begin and end on the same line. | ||
- | <source lang=csharp enclose=pre> | ||
- | { | ||
- | int bar; | ||
- | | ||
- | { | ||
- | get { return bar; } | ||
- | set { bar = value; } | ||
- | } | ||
- | } | ||
- | </ | ||
- | *√ CONSIDER It is suggested that all control structures (if, while, for, etc.) use braces, but it is not required. | ||
- | |||
- | |||
- | === Commenting === | ||
- | Comments should be used to describe intention, algorithmic overview, and/or logical flow. It would be ideal, if from reading the comments alone, someone other than the author could understand a function’s intended behavior and general operation. While there are no minimum comment requirements and certainly some very small routines need no commenting at all, it is hoped that most routines will have comments reflecting the programmer’s intent and approach. | ||
- | |||
- | ==== Documentation Comments ==== | ||
- | All methods should use XML doc comments. For internal dev comments, the < | ||
- | <source lang=csharp enclose=pre> | ||
- | { | ||
- | /// < | ||
- | /// <param name=”bar”> | ||
- | /// < | ||
- | /// | ||
- | public void MyMethod(int bar) { … } | ||
- | } | ||
- | </ | ||
- | However, it is common that you would want to move the XML documentation to an external file – for that, use the < | ||
- | <source lang=csharp enclose=pre> | ||
- | { | ||
- | /// <include file=' | ||
- | /// | ||
- | | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Comments Style ==== | ||
- | The // (two slashes) style of comment tags should be used in most situations. Where ever possible, place comments above the code instead of beside it. Here are some examples: | ||
- | <source lang=csharp enclose=pre>// | ||
- | GlobalProxySelection.Select = new WebProxy(" | ||
- | |||
- | |||
- | // Create object to access Internet resources// | ||
- | WebClient myClient = new WebClient(); | ||
- | </ | ||
- | |||
- | Comments can be placed at the end of a line when space allows: | ||
- | <source lang=csharp enclose=pre> | ||
- | private int itemHash; | ||
- | private static bool hasDoneSomething; | ||
- | }</ | ||
- | |||
- | |||
- | === Spacing === | ||
- | Spaces improve readability by decreasing code density. Here are some guidelines for the use of space characters within code: | ||
- | *√ DO use a single space after a comma between function arguments. | ||
- | **Right: < | ||
- | **Wrong: < | ||
- | *X DO NOT use a space after the parenthesis and function arguments. | ||
- | **Right: < | ||
- | **Wrong: < | ||
- | *X DO NOT use spaces between a function name and parenthesis. | ||
- | **Right: < | ||
- | **Wrong: < | ||
- | *X DO NOT use spaces inside brackets. | ||
- | **Right: < | ||
- | **Wrong: < | ||
- | *√ DO use a single space before flow control statements. | ||
- | **Right: < | ||
- | **Wrong: < | ||
- | *√ DO use a single space before and after comparison operators. | ||
- | **Right: < | ||
- | **Wrong: < | ||
- | |||
- | |||
- | == File Organisation == | ||
- | |||
- | === Internal Structure === | ||
- | *Source files should contain only one public type, although multiple internal classes are allowed. | ||
- | *Source files should be given the name of the public class in the file. | ||
- | *Directory names should follow the namespace for the class. For example, I would expect to find the public class < | ||
- | *Classes member should be ''' | ||
- | *Using statements should be outside the namespace declaration. | ||
- | |||
- | Example: | ||
- | <source lang=csharp enclose=pre> | ||
- | using System; | ||
- | |||
- | namespace MyNamespace | ||
- | { | ||
- | | ||
- | { | ||
- | #region Members | ||
- | int foo; | ||
- | #endregion | ||
- | |||
- | #region Constructors | ||
- | public MyClass() { … } | ||
- | #endregion | ||
- | |||
- | #region Properties | ||
- | public int Foo { get { … } set { … } } | ||
- | #endregion | ||
- | |||
- | #region Events | ||
- | public event EventHandler FooChanged { add { … } remove { … } } | ||
- | #endregion | ||
- | |||
- | |||
- | #region Methods | ||
- | void DoSomething() { … } | ||
- | void FindSomethind() { … } | ||
- | #endregion | ||
- | |||
- | |||
- | #region Interface Implementations | ||
- | void IFoo.DoSomething() { DoSomething(); | ||
- | #endregion | ||
- | |||
- | |||
- | #region Nested Types | ||
- | class NestedType { … } | ||
- | #endregion | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | === Assembly Structure === | ||
- | ''' | ||
- | |||
- | </ | ||
he/dokuwiki.1374878162.txt.gz · מועד השינוי האחרון: 2013-07-27 00:36 על ידי 79.182.145.170