C # Coding specifications summarized by myself-5. How to Write comments,

Source: Internet
Author: User

C # Coding specifications summarized by myself-5. How to Write comments,

This article is a practical method for writing comments after reading several books mentioned in the preface and combining their own ideas:

  • How to write comments
  • Avoid using ambiguous pronouns

In some cases, pronouns such as "it" and "this" are prone to ambiguity. The safest way is not to replace all Pronouns that may be ambiguous with actually referred words.

For example: // Insert the data into the cache, but check if it's too big first.

"It" refers to "data" or "cache "? No one knows who it refers to before reading the remaining code. What else do I need to comment out? After you replace it with the word you want to refer to, the reader can directly know what to do next in the code.

// Insert the data into the cache, but check if the data is too big first.

  • Accurately describe the behavior of a Method

Annotations must accurately describe the behavior of methods. Avoid incorrect calls due to incorrect annotations.

If you write a method to count the number of rows in the file

// Return the number of lines in this file

Public long CountLinesInFile (string fileName)

The comment above is not very accurate, because there are many ways to define rows. In the following situations, the return values of this method cannot be quickly determined based on the comment.

Assuming that the implementation of this method is to count the number of line breaks (\ n), the following comments are better than the original comments.

// Count how many newline symbols ('\ n') are this file

This comment contains more information. The reader can know that if there is no line break, this function will return 0. The reader also knows that the carriage return (\ r) will be ignored.

  • Use Input and Output examples to illustrate special situations

For comments, a well-selected example is more effective than a thousand words, more straightforward and effective, and faster reading.

For example: // <summary>

/// Remove the suffix/prefix of charsToRemove from the input source

/// </Summary>

Public string StripPrefixAndSuffix (string source, string charsToRemove)

This comment is not very accurate because it cannot answer the following questions.

In a good example, you can simply answer these questions:

/// <Summary>

/// Example: StripPrefixAndSuffix ("abbayabbazbaba", "AB") returns "yababz"

/// </Summary>

  • Use the name function as appropriate

Suppose you see such a function call:

ConnectMailServer (100, false );

It is difficult to understand this call because the values and bool values are passed in directly. In this case, you can use the C # name function so that the code reader can quickly understand the meanings of these two parameters.

Assume that the function is like this: public void ConnectMailServer (int timeout_s, bool useEncryption)

Then we can use the name function as follows:

ConnectMailServer (timeout_s: 100, useEncryption: false );

The code reader can see the functions of these two parameters at a glance.

  • Use General terminology

The reader and caller of the Code are both programmers. There are a lot of special terms between the programmers that can be used to describe some patterns and solutions. Using these words can give a simple and clear explanation of the meaning of your code.

Assume that your original comment is as follows:

You can simply say:

// This class acts asCaching layerTo the database.

The programmer can understand what the caching layer is doing. Even if a newbie doesn't understand it, it's hard to explain your comments to him. It's better to give him a keyword to Google or ask someone else. This effect is much better than your comments.

  • Remember to update comments when updating code

Even better comments will become meaningless as the content changes. Sometimes, they may even mislead readers and generate unnecessary bugs. After changing the code, remember to update the comments of the changed code to express the meaning of the latest code.

  • Only comments that can be read by others are qualified comments.

When you are not sure whether your comments are qualified, ask your colleagues around you to read your comments and see if they want to express their ideas after reading the comments, whether information is missing or misunderstood.


C language knowledge Summary

C language Overview
Chapter 1. Overview
1. Basic knowledge of C Language
1.1 C language execution steps
Edit-enter the program code to generate the source program *. c
Compile-syntax analysis error: translation to generate the Target Program *. obj
(Syntax or logic error, starting from the first change, variable definition, statement format, expression format, etc)
Link-assemble with other target programs or libraries to generate executable programs *. exe
Run
1.2 Basic knowledge of the main Function
Location of the main () function
C Programs are always executed from the main () function.
A c program can contain a main function, that is, the main () function. It can also contain a main () function and several other functions.
1.3. c program structure
Functions and main functions
A program consists of one or more functions.
There must be one and only one main function main ()
Program Execution starts from main and ends in main. Other functions can be executed through nested calls.
Program Statement
A c program consists of statements.
Use ";" as the statement Terminator
Note
//
Or
/**/Is a comment and cannot be nested
No compilation code
1.4 c program writing rules
Used to Use lowercase letters, case sensitive
No row number. No program line concept: A statement usually occupies one row.
Empty rows and spaces can be used.
It is commonly used for the writing format of the tooth form; statements of the same hierarchy are aligned up and down.
Chapter 2 Basic Data Types and operations
2.1. Data Type of c program
Note the differences between types and variables (the types are fixed names and variables are their own names)
Storage space occupied by Variables
Data Type
Basic types: integer, complex, and floating point (single precision and Double Precision)
Construction type: array type and struct type
Pointer type
Null type
Note the initial value Assignment Method for the basic type.
Basic Data Type Representation
Integer Data
Decimal: starts with a non-zero number, for example, 123,-9, 0
Octal; starts with 0, for example, 0123,067
Hexadecimal: starting with 0x, for example, 0x123, 0xff
Real Data
Decimal: it must contain a decimal point, for example, 123.0,-9.0.
Exponential form, for example, 1.23E3, 0.9e-2, 5e2
Balanced data
Common characters: for example, 'A', '2', 'h ','#'
Escape characters: for example, '\ n',' \ 100', '\ xlf ,'\\'
(Realize the alignment of several columns: Specify the width. For example, % 100 \ '\ t)
(String Length. "Abc \ n \ t \" strlen 6; sizeof 7)
Storage length of the basic data type
Integer
Int Byte Count 2-digit 16-32768-32767
Short 2 16-32768-32767
Long 4 32-2147483648-2147483647
Real-type
Float 4 32 3.4e-38---3. 4e38
Double 8 64 1.7e-308---1. 7e308
Character Type
Char 1 8-128----127
2.2 identifier naming rules
C-language flag naming rules
An identifier consists of numbers, letters, and underscores.
The identifier must start with a letter or underline.
The identifier cannot be a reserved word (keyword) in C language)
For example, auto extern sizeof float static case for struct char goto switch continue in typedef const if union default long ...... remaining full text>

C language practice summary

Khan .... You can change the questions in the book ~~~ This is basically a form !! Don't spend too much time on it ~~

C language is true ~~

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.