Whether it's chicks or e-code comments for programming specifications

Source: Internet
Author: User

Annotations make the code easier to understand and trace. Excellent comments are like a good design blueprint that can guide readers through the twists and turns of your application to explain the expected running results and possible exceptions. Annotations can bring great convenience to later maintainers. They are indispensable for class, method, and variable annotations. Without comments, the code is "tianshu". Some readers may say that it is not difficult to spend some time! Imagine how time-consuming and laborious it is to read thousands, tens of thousands, and hundreds of thousands of codes that are not annotated as described above, but these times can be saved by several lines of comments.

Take the code above as an example. Sadly, the code in this article is the same as the code written by most people-no comments.

1.
Note writers share the same characteristics, and those who do not write comments have their own reasons.

L
Writing comments is too time-consuming

In fact, it does not take much time to add comments when writing code.

L
Some processes are difficult to comment out.

Generally, if a part of the code is difficult to comment out, it is more difficult for others to understand your code without commenting.

PS: the complicated and difficult-to-Comment Code may not be a good code.

If you find it difficult to add comments to all or part of the process, check your code and you may find a better solution.

2.
Annotation principle (excerpt) for your reference

L
Do not simply repeat the code and do not add comments to the code.

L
Annotations are used to describe when and why errors may occur, facilitating later debugging.

L
Comments before writing the code to show your ideas in text format.

L
Enhance the readability of comments, complete statements, and clear ideographic statements. Beautiful operations such as indentation and alignment in the format are also indispensable.

L
Assign a comment header to each process. // (''in VB) in C # clearly guides the process during process calling.

L
Internal Notes:

(1)
Add a comment before each if and select statement to let the reader branch the statement and the result of different branches.

(3)
Add comments before each loop to ensure that the reader understands the role of this loop.

(4)
Add comments before each statement that modifies the global variable (if any) to facilitate later code debugging.

Modified code

   1:      ''' <summary>
2: ''' export data from the dview to excel
   3:      ''' <summary>
4: ''' <Param name = "dgvsouse"> datagridview dview to be exported to excel <param>
5: ''' <Param name = "blnisonlyvisible"> determine whether to add all contents in the dview <param>
   6:      ''' <remarks><remarks>
   7:      Sub ExportExcel(ByVal dgvSouse As DataGridView, ByVal blnIsOnlyVisible As Boolean)
8: 'Try to execute the export operation. If an exception occurs, it will be thrown.
   9:          Try
10: dim intcolumnnumhead as integer defines the number of columns in the loop of the header.
11: dim intrownumtext as integer 'defines the number of rows in the content loop.
12: dim intcolumnnumtext as integer defines the number of columns in the content loop.
13: dim intcurrentrow as integer defines the current column.
14: dim intcurrentcol as integer defines the current row.
15: dim excel as Excel. Application = new excel. Application 'defines the EXCEL program.
16: add an Excel file in Excel. application. workbooks. Add (true.
17: intcurrentcol = 1' record the current number of columns.
18: 'fill in the header in Excel cyclically
  19:              For IntColumnNumHead = 0 To dgvSouse.ColumnCount - 1
20: 'determine whether the user adds all content.
  21:                  If blnIsOnlyVisible Then
22: 'If you only add visible content, you can determine which columns are invisible.
  23:                      If dgvSouse.Columns(IntColumnNumHead).Visible Then
24: 'add only visible columns.
  25:                          Excel.Cells(1, IntCurrentCol) = dgvSouse.Columns(IntColumnNumHead).HeaderText
  26:                          IntCurrentCol = IntCurrentCol + 1
  27:                      End If
  28:                  Else
29: 'add all content if the user adds all content.
  30:                      Excel.Cells(1, IntColumnNumHead + 1) = dgvSouse.Columns(IntColumnNumHead).HeaderText
  31:                  End If
  32:              Next
33: intcurrentrow = 2' record the current number of rows.
34: 'fill in the content in excel in a loop.
  35:              For IntRowNumText = 0 To dgvSouse.RowCount - 1
  36:                  IntCurrentCol = 1
  37:                  For IntColumnNumText = 0 To dgvSouse.ColumnCount - 1
38: 'determine whether the user adds all content.
  39:                      If blnIsOnlyVisible Then
40: 'If you only add visible content, you can determine which columns are invisible.
  41:                          If dgvSouse.Columns(IntColumnNumText).Visible Then
42: 'add only visible columns.
  43:                              Excel.Cells(IntRowNumText + 2, IntCurrentCol) = dgvSouse.Rows(IntRowNumText).Cells(IntColumnNumText).Value
  44:                              IntCurrentCol = IntCurrentCol + 1
  45:                          End If
  46:                      Else
47: 'add all content if the user adds all content.
  48:                          Excel.Cells(IntRowNumText + 2, IntColumnNumText + 1) = dgvSouse.Rows(IntRowNumText).Cells(IntColumnNumText).Value
  49:                      End If
  50:                  Next
  51:              Next
52: excel. Visible = true' set EXCEL to visible.
  53:          Catch ex As Exception
54: Throw new exception ("Export failed") 'throws an exception.
  55:          End Try
  56:      End Sub

Since then, writing code is no longer for running (not worth mentioning), but for later users (including themselves) to read it!

Appendix: stylecop

Note: young, young (mostly birds); Young, and chicken. -- Shuo wen (from Baidu encyclopedia)

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.