Coding Standards (c #)

Oaks from little acorns, good coding style line and make it more interesting to others, and vice versa think Tucao this is what X wrote silly, this is what a mess of stuff?

Then look at the back of his past comments found written, that the situation was very awkward. . . .

Statute not the rule , not have to be this way, like you go to a team, they are in English, then you are Chinese, you also how to start work this exchange? I'm afraid to let you get out before long.

Here is a brief summary of some of my work, for reference, so what's wrong please correct me.

First, the statute programming

(A) naming style

1. Do not use the code names Pinyin and English mixed mode, but does not allow direct use of Chinese (company rules, no way)

Description: correct English spelling and grammar can make the reader understand the avoidance of doubt, even if purely phonetic name should be avoided.

Some people would say, before news that people see a sister in Chinese declare a variable, and then was a senior designer, like

 I do not care public string = "You do not know how scarce you bastard Yuan programs?";

2. The name of the class, class attributes, method names, namespaces using UpperCamelCase capital hump style, the English word capitalized

3 parameter name, member variables, local variables, local variables are unified with lowerCamelCase small hump style (lowercase first letter of the first word, behind each word capitalized)

localCache/userList

4 Constant naming all uppercase, separated by an underscore between words

5 Abstract abstract class named by the beginning or base; by the end of the exception class Exception; test class to test class names start to end Test

6 eliminate completely non-standard abbreviations to avoid Wen Wang Yi know ex: AbstractClass abbreviation AbsClass

7 Using design patterns, reflecting the specific mode is recommended in the class name.

public class OrderFactory

8 class declaration, methods and properties combined with effective Summery Notes

///  <Summary> 
/// This class is a class of user operation of the controller
 ///  </ Summary> 
public  class SysuserController 
{ 
  ///  <Summary> 
  /// username
  ///  </ Summary> 
  public  String name;
  ///  <Summary> 
  /// obtain a user name
  ///  </ Summary> 
  public String getName () 
  { 
    // return this.name; 
  } 
}

9 enum class name is recommended to bring Enum E prefix or suffix, enumeration member name needs all uppercase, words separated by underscores

(Ii) constant defines

1. appears not allow any undefined constant that appears directly in your code. Counterexample string key = "caheKey _" + user.Name;

(C) the format of the Statute

1. Ctr + K + D code formatting shortcuts

2 is empty braces, abbreviated as {}, do not need wrap; if a non-null code, the left and right parentheses own line, the contents of a new line

public string getName() { }
public string getName()
{
  return this.Name;
}

Must be spaces between 3.if / for / while / switch / do with reserved words such as left and right parentheses

4. Any operator must around a space

5 indented using four spaces, try not to use tab characters (vs the tab default is 4)

public  static  void main ( String [] args) 
{ 
     // indented four spaces 
  String name = "Jack";
  // operator must be a space all 
  BOOL Isme = to true ;
  // between the bracket and the key if necessary there is a space frame, the method does not require space in parentheses body 
  IF (== Isme to true ) 
  { 
    Console.WriteLine ( "My name iS " + name); 
  } 
  the else 
  { 
    Console.WriteLine ( "your name iS " + name); 
  } 
}

Method 6 and incoming parameters when defining a plurality of parameter space after the comma

7 is not necessary to increase a number of spaces to cause a corresponding row of characters are aligned.

int a = 3long b = 4string str = “hello world”;

Finally, for the mix

(D) control statements

1. In a switch block, or in each case terminated by a break / return, etc., or explanatory notes program execution will continue until which case, must contain a default statement, and placed at the end

2. must use braces if / else / for / while / do statement, even if only one line of code, avoid the following form

IF (for condition Condition) 
{ 
  // ... the Do something; 
  return obj; 
} 
   
// the else's business logic code

3. Expression branch abnormal, if-else less manner, if not to have to use more than 3 layers

4 in addition to common methods (e.g. GetXXX / IsXXX) is not performed in other complex conditional statements, the logic judgment result of a complex is assigned to a variable meaningful bool

bool exsisted = file.Exsist() && obj != null && ...;
  If (exsisted)
  {
    ...
  }

(E) Notes Statute

1. class, class attributes, methods using class Specification Summary

  /// <summary>
  /// ....
  /// </summary>

2. All abstract method Summary notes, in addition to the return values, parameters, exception description, we must also point out that the way to do something, to achieve what functions

3. The method of single-line comments inside, over a separate line, with // comment, with multiple lines / ** / comment

4. colleagues code changes, annotations should be modified accordingly

5. As far as possible with the annotated code, rather than simply commented.

Second, exception handling

1. Do not abnormal used for process control, control conditions

2. For a large section of code try-catch, this is irresponsible. Code should be stable and unstable stage Code

3. catch the exception to handle it, but do not capture the process and discard what does not, do not want to deal with, it is the abnormal thrown to his caller.

4. There try block code into a transaction, the catch abnormal, if you want to roll back the transaction, we must pay attention to manually roll back

5.finally block must be closed to resource object, stream object, a database connection has abnormality do try-catch

6. Throws capture abnormalities, must match exactly, or catch an exception is thrown exception parent

The method may return value is null, or empty set not mandatory return null object, annotated shows what happens null return means for determining null, null reference exception prevented

Third, database

(A) Statute of the construction of the table

1. Expression is the concept of a field or not, are named using is_xxx (1 is represented, 0 indicates NO), the operational state of the field is, the intermediate representation with some number, such as 3 represents return

2. The table names, field names and data with lower-case letters, numbers do not appear at the beginning, not the middle two numbers appear only underscore

3. Do not use a plural noun table names

4 disabled reserved words, such as desc, range, etc.

The primary key index field name called pk_; uk_ unique index name field name; general name index field name idx_

6. The presence of type decimal precision loss decimal, float and double, the possible values ​​of the comparison, the results are not correct

7. If the string length is almost equal, the use of fixed-length char

8. New field shall increase the field injection analysis, if the expression relates to digital meanings, and each number represents the meanings indicated, such as a completed business representatives, 0 represents service start, service retracted represents

9. Table name plus business name _ the best table action

10. The number of lines to the table more than five million licensed capacity of more than a single table 2GB, it is recommended sub-library sub-table

11. Suitable storage length character, not only save table space databases, saving the index storage, more importantly, to enhance the retrieval speed

(B) of the Statute of the index

Field has a unique characteristic 1. service, even if a combination of a plurality of fields, also create unique index

2. Establish an index on varchar fields are, shall specify the length of the index, no need for indexing the whole field

3. Do not search page left vague or fuzzy the whole, if you need to go search engine

4. There is order by scene, pay attention to the orderly use of the index

The use of an index to cover the query, back to the table to avoid

6. Use the delay associated or sub-query optimization over many paging scene

When the 7 key composite index, sitting height discrimination in the leftmost

8. implicit conversion to prevent different types of field caused by failure cause index

(C) SQL statements

1. Do not use count (column name) or count (constant) instead of count ()

2. Use the ISNULL () to determine whether a NULL value

3. When the paging query logic 0 if the count is to be returned directly in the code, to avoid later statement execution tab

4.in operation can be avoided, avoid

The stored procedure difficult to debug and expansion, and no graft, avoiding the use of

6. data revision, confirmation before performing an update

7.truncate table faster than delete speed, and low system resource usage and transaction logs, but no transaction does not trigger trigger, may cause an accident, this statement is not recommended

Guess you like

Origin www.cnblogs.com/carlpeng/p/12035143.html