IDesign C # Programming specification (II)

Source: Internet
Author: User
Tags assert bool comments constant error code numeric value readable throw exception
Programming | One of the specifications, chick shooter went on to translate the IDesign code code to the second chapter of the front.

2 Coding Conventions
Coding practices


1. Avoid placing multiple classes in a single file.
Avoid putting multiple classes in a single file.
2. A file should provide a type only for one namespace. Avoid having multiple namespaces in the same file.
A single file should only contribute types to a single namespace. Avoid has multiple namespaces in the same file.
3. Avoid the file length of more than 500 lines (except the machine automatically generated code).
Avoid files with more than lines (excluding machine-generated code).
4. Avoid a method definition of more than 25 lines.
Avoid methods with more than lines.
5. Methods to avoid exceeding 5 parameters. Use structs to pass multiple parameters.
Avoid methods with more than 5 arguments. Use the structures for passing multiple arguments.
6. Each line should be no more than 80 characters.
Lines should not exceed characters.
7. Do not manually edit any machine generated code.
Don't manually edit any machine generated code.
A If you modify the code generated by the machine, modify the code format and style to conform to this coding standard.
If modifying machine generated code, modify the format and style to match this coding standard.
(b) Use the partial class as much as possible to break out the parts that need to be maintained.
Use partial classes whenever possible to factor out the maintained.
8. Avoid commenting on what is obvious.
Avoid comments that explain the obvious.
A the code should be self explanatory. Good code made up of readable variables and methods should not require annotations.
The Code should be self explanatory. Good code with readable variable and method names the should not require comments.
9. Write documents only on the premise of operation, intrinsic algorithm, etc.
Document only operational assumptions, algorithm insights and.
10. Avoid document at the method level.
Avoid method-level documentation.
A a large number of external documents are used for API documentation.
Use the extensive external documentation for API documentation.
b The method-level comment serves only as a hint to other developers.
Use Method-level comments as tool tips for the other developers.
11. Never hard-code a value, but always declare a constant.
Never Hard-code A numeric value, always declare a constant instead.
12. Use the const modifier only for values that are inherently constants, such as days of the week.
Use the "Const directive only" natural constants such as the number of days of week.
13. Avoid using the const modifier on read-only variables. In this case, the ReadOnly modifier is used.
Avoid using const on READ-ONLY variables. for which, use the readonly directive.
public class MyClass
{
public readonly int number;
Public MyClass (int somevalue)
{
Number = Somevalue;
}
public const int Daysinweek = 7;
}
14. Assert the use of any assumption.
Assert every assumption.
(a) On average, one row in every 5 lines is an assertion.
On average, every fifth line is a assertion.
Using System.Diagnostics;
Object GetObject ()
{
Object obj = GetObject ();
Debug.Assert (obj!= null);
15. Each line of code should pass the white box test.
Every line of code should is walked through in a 搘 Hite box?testing.
16. Capture only exceptions that have been explicitly handled.
Only catch exceptions for which you have explicit handling.
17. In a catch statement that throws an exception, always throws the original exception to keep the stack location of the original error.
In a catch statement that throws a exception, always throw the original exception to maintain stack location of original Error.
catch (Exception Exception)
{
MessageBox.Show (Exception. message);
Throw Same as throw exception;
}
18. Avoid the error code as the return value of the method.
Avoid error code as methods return values.
19. Avoid defining custom exception classes.
Avoid defining custom exception classes.
20. When defining a custom exception:
When defining custom exceptions:
A) inherit from ApplicationException
Derive the custom exception from ApplicationException.
b) to provide custom serialization.
Provide custom serialization.
21. Avoid having multiple main () methods in an assembly.
Avoid multiple Main () methods in a single assembly.
22. Only the type that is most needed is marked public, and the other is internal.
Make is the most necessary types public, mark others as internal.
23. Avoid using friend assemblies, as this increases the coupling between assemblies.
Avoid friend assemblies, as it increases inter-assembly coupling.
24. Avoid using code that relies on assemblies that run from a specific location.
Avoid code that relies the assembly running from a particular location.
25. Minimize the code for application sets (client-side EXE assemblies). Use class libraries instead of business logic layer code.
Minimize code in Application assemblies (EXE client assemblies). Use class libraries instead to contain business logic.
26. Avoid providing explicit values for enumerations.
Avoid providing explicit values for enums.
Correct
public enum Color
{
Red,green,blue
}
Avoid
public enum Color
{
Red = 1,green = 2,blue = 3
}
27. Avoid specifying types for enumerations.
Avoid specifying a type for an enum.
Avoid
public enum Color:long
{
Red,green,blue
}
An. If statement always uses parentheses, even if it contains a statement.
Always use a curly brace scope of if statement, even if it conditions a single statement.
29. Avoid using?: Conditional operator.
Avoid using the trinary conditional operator.
30. Avoid calling functions in Boolean conditional statements. Assign values to local variables and check their values.
Avoid function calls in Boolean conditional statements. Assign into the local variables and check on them:
BOOL Iseverythingok ()
{...}
Avoid:
Avoid:
if (Iseverythingok ())
{...}
Using:
Instead:
bool OK = Iseverythingok ();
if (OK)
{...}
31. Always use an array starting with 0.
Always use zero-based arrays.
32. Always use a For loop to explicitly initialize an array of reference types.
Always explicitly initialize an array of reference types using a for loop.
public class MyClass
{}
myclass[] array = new MYCLASS[100];
for (int index = 0; index < array. Length; index++)
{
Array[index] = new MyClass ();
}
33. Do not provide public or protected member variables, but use attributes.
Don't provide public or protected member variables. Use properties instead.

The third


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.