C # coding standard - Coding habits cuike519 (translation)

zhaozj2021-02-16  173

C # coding standard - Coding habits cuike519 (translation) 1. Avoid putting multiple classes in a file.

2. A file should have only one namespace to avoid placing multiple namespaces in the same file.

3. One file is preferably not more than 500 lines of code (excluding the code generated by the machine).

4. The length of the code is preferably not more than 25 lines.

5. Avoid more than 5 parameters in the method. Use the structure to pass multiple parameters.

6. Do not exceed 80 characters per line code.

7. Do not manually modify the code generated by the machine.

a) If you need to edit the code generated by the machine, edit the format and style to match the coding standard.

b) Use Partial Classes WHENEVER POSIBLE TO FACTOR OUT The Maintained Portions.

8. Avoid using the annotation interpretation of the code.

a) The code should be self-explanatory. A good code is named by readable variables and methods therefore do not need to comment.

9. Document Only Operational Assumptions, Algorithm Insights and So ON.

10. Avoid using the method level document.

a) Use the extended API document description.

b) The method level comment is only used when the method needs to be used by other developers. (In C # is ////)

11. Do not hardly encode the value of the numbers, always use the constructor to set its value.

12. Only the natural structure can be used directly, such as a day of days.

13. Avoid using const on read-only variables. If you want to implement read-only, you can use Readonly directly.

Public Class Myclass

{

Public Readonly Int Number;

Public MyClass (int SomeValue)

{

Number = SomeValue;

}

Public const INT daysinweek = 7;

}

14. Each hypothesis must be checked using an ASSERT

a) Average every 15 rows (Assert)

Using system.diagnostics;

Object getObject ()

{...}

Object obj = getObject ();

Debug.Assert (OBJ! = NULL);

15. Each line of the code should be tested through a white box.

16. Only the abnormality that has been displayed has been thrown.

17. In the throwing abnormality clause of the catch statement, it always throws the stack assignment of the original anomaly maintenance.

Catch (Exception Exception)

{

Messagebox.show; "Exception.Message);

Throw; // and throw Exception.

}

18. Avoid the return value of the method is the error code.

19. Try to avoid defining a custom anomalous class.

20. When you need to define custom anomalies:

a) Custom exception should be inherited in ApplicationException.

b) Provide custom serialization.

21. Avoid using multiple main methods in a single assembly.

22. Only the necessary operations are published, and others are Internal.

23. Avoid Friend Assemblies, AS IT Increases Inter-Assembly Coupling.

24. Avoid Code That Relies on an assembly running from a particular location.

25. Make the app set as much as possible for minimization code (EXE customer program). Use the class library to replace the included business logic.

26. Avoid providing an explicit value to the enumeration variable.

/ / Correct method

Public Enum Color, PUBLIC ENUM

{

RED, Green, Blue}

//avoid

Public Enum Color, PUBLIC ENUM

{

RED = 1, Green = 2, Blue = 3

}

27. Avoid designating special types of enumerated variables.

//avoid

Public Enum Color: Long

{

Red, Green, Blue

}

28. Even if the IF statement has only one sentence, the content of the IF statement is to be launched by brace.

29. Avoid using Trinary conditional operators.

30. Avoid calling a function that returns a BOOL value in a condition statement. Particular variables can be used and these local variables are checked.

Bool ISeverythingok ()

{...}

//avoid

IF (ISeverythingok ())

{...}

// Replacement scheme

Bool OK = ISEVERYTHINGOK ();

IF (ok)

{...}

31. Always use 0-based arrays.

32. The array of explicit initialization reference types in the loop.

Public Class Myclass

{}

Myclass [] array = new myclass [100];

For (int index = 0; index

{

Array [index] = new myclass ();

}

33. Do not provide members variables for public and protected, using properties instead of them.

34. Avoid replacement using Override in inheritance.

35. The method of PUBLIC and PROTECTEDs are always marked into Virtual in classes that are not Sealed.

36. Do not use unsafe code (unsafe code) unless you use the Interop (COM or other DLL) code.

37. Avoid displaying conversions, use the AS operator to convert the compatible type.

DOG DOG = New Germanshepherd ();

Germanshepherd shepherd = dog as germanshepherd;

IF (Shepherd! = NULL)

{...}

38. When class members include commissioned

a) Copy a delegate to a local variable before publishing to Avoid Concurrency Race

Condition.

b) Be sure to check if it is NULL before calling the delegation.

Public class mysource

{

Public Event athandler myevent;

Public void fireEvent ()

{

Eventhandler temp = myevent;

IF (Temp! = NULL)

{

Temp (this, eventargs.empty);

}

}

}

39. Do not provide public event member variables, replace these variables with event accessors.

Public class mysource

{

MyDelegate m_someevent;

Public Event MyDelegate Someevent

{

Add

{

m_someevent = value;

}

Remove

{

M_Someevent - = Value;

}

}

}

40. Use an event to help classes to publish the definition of an event.

41. Always use the interface.

42. The method and attributes in classes and interfaces are at least 2: 1 ratio.

43. Avoid only one member in an interface.

44. Try to enable 3-5 members in each interface.

45. Members in the interface should not exceed 20.

a) The actual situation may limit 12

46. ​​Avoid including events in interface members.

47. Avoid replace the interface to avoid using an abstract method.

48. Displays the interface in the class level. 49. Recommended explicit interface implementation.

50. Never assume that a type is compatible with an interface. Defensively query for That interface.

Sometype obj1;

Imyinterface obj2;

/ * Assume that there is already the code initializes Obj1, next * /

Obj2 = Obj1 as ImyInterface;

IF (Obj2! = NULL)

{

Obj2.method1 ();

}

Else

{

// Handling error

}

51. Do not use hardcodes to be replaced with the resource files to be replaced with the resource file.

52. Do not hardcode the configuration-based string that may be changed, such as connecting strings.

53. When you need to build a long string, use StringBuilder not to use String

54. Avoid providing a method in the structure.

a) It is recommended to use parameterized constructor

b) can be retired

55. Always provide a static constructor to static variables.

56. You can use early binding or use later binding.

57. Using the application's logs and tracking.

58. Do not use a goto statement unless otherwise in an incomplete Switch statement.

59. There is always a DEFAULT clause to display information (Assert) in the switch statement.

INT number = someMethod ();

Switch (Number)

{

Case 1:

Trace.WriteLine ("Case 1:");

Break;

Case 2:

Trace.writeline ("Case 2:");

Break;

DEFAULT:

Debug.assert (false);

Break;

}

60. Do not use the THIS pointer unless otherwise call other constructors in the constructor.

/ / Correctly use this example

Public Class Myclass

{

Public myclass (String Message)

{}

Public myclass (): this ("Hello")

{}

}

61. Unless you want to rewrite a member of the subclass with a name conflict or call the base class constructor, do not use Base to access a member of the base class.

/ / Correctly use base

Public Class Dog

{

Public Dog (String Name)

{}

Virtual public void bar (int!

{}

}

Public Class Germanshepherd: Dog

{

Public Germanshe PHERD (STRING NAME): Base (Name)

{}

Override public void bar (int!)

{

Base .bark (howlong);

}

}

62. To achieve DISPOSE () and Finalize () by template.

63. Usually avoiding the code from system.object to convert and convert by System.Object, and replacing it with a forced conversion or AS operator.

Class SomeClass

{}

//avoid:

Class myclass

{

Void SomeMethod (t t)

{

Object temp = t;

SomeClass Obj = (SomeClass) TEMP;

}

}

/ / Correct:

Class Myclass Where T: SomeClass

{

Void SomeMethod (t t)

{

SomeClass Obj = T;

}

}

64. Do not fix the interface with a restriction in general. The limit level of the interface can usually replace with a strong type. Public Class Customer PUBLIC CLASS CUSTOMER

{...}

//avoid:

Public Interface IList Where T: Customer

{...}

//correct:

Public interface icustomerlist: IList

{...}

65. The limitation of the specific method in the interface is not determined.

66. Always choose the data structure of the C # built-in (general generics).

Because I have limited level, if I have any mistakes in the translation, I will say it in a crazy English, but I must give me the right reason, thank you for letting me lose your face. In addition, there are a few in the articles, that is, I really can't get it, I will put the original text there hopate the expert guidance! Thank you!

转载请注明原文地址:https://www.9cbs.com/read-8932.html

New Post(0)