Size: 4629
Comment:
|
Size: 11436
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 12: | Line 12: |
= Coding Style = | = C = == Coding Style == |
Line 14: | Line 15: |
= C = |
=== Indentation === * 8-char indent * If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program. * Don't put multiple statements on a single line unless you have something to hide. * Get a decent editor, and don't leave whitespace at the end of lines. === Breaking long lines and strings === * Coding style is all about readability and maintainabilty using commonly avaible tools. * The limit on the length of lines is 80 colums and this is a hard limit. === Placing braces === * The preferred way, as shown to us by the prophets Kernighan and Ritchie, is to put the opening brace last on the line, and put the closing brace first. * namely function have the opening brace at the begining of the next line. === Naming === * C is a Spartan language, and so should your naming be. * GLOBAL variables (to be used only if you _really_need them) need to have descriptive name, as do global functions. * LOCAL variable name should be short, and to the point. === Fuctions === * Fuctions should be short and sweet, and do just one thind. * Another measure of the functions is the number of local variables, They shouldn't exceed 5-10, or you're doing something wrong. === Centralized exiting of functions === * About gogo statement. === Commenting === * Comments are good, but there is also a demage of over-commenting. * Generally, you want your comments to tell WHAT your code does, not HOW. === You've made a mess of it === * About GNU Emacs and GNU indent. * 'indent' is not a fix for bad programing. === Data structures === * Data structures that have visibility outside the single-threaded environment they are created and destoryed in should always have reference counts. * Locking is _not_ a replacement for reference counting. * If another thread can find your data structure, and you don't have a reference count on it, you almost certainly have a bug. === Macros, Enums, Inline functions and RTL === * Names of macros defining contants and labels in enums are capitalized. * Enums are preferred when defining serval related constants. * Generally, inline functions are preferable to macros resembling functions. * Macros with multiple statements should be enclosed in a do-while block. |
Line 27: | Line 61: |
* If you set '''hlsearch''' option, Vim will highlight all matches for the pattern with a yellow background. | * If you set '''hlsearch''' option, Vim will highlight all matches for the pattern with a yellow background. ''':nohlsearch''' will close it. |
Line 33: | Line 67: |
* 可视(visual)模式,用于选定文本块;可以在正常模式下输入“v”(小写)来按字符选定,输入“V”(大写)来按行选定,或输入“Ctrl-V”来按方块选定。 | * 可视(visual)模式,用于选定文本块;可以在正常模式下输入'''v'''(小写)来按字符选定,输入'''V'''(大写)来按行选定,或输入'''Ctrl-V'''来按方块选定。 * '''`.'''(跳转到最近修改过的位置) * '''ZQ'''(无条件退出) * '''ZZ'''(存盘退出) * '''ga'''(显示光标下的字符在当前使用的 encoding 下的内码) * '''guw'''(光标下的单词变为小写) * '''gUw'''(光标下的单词变为大写) * ''':TOhtml''' (根据 Vim 的语法加亮的方式生成 HTML 代码) * '''K''' (显示光标下单词的 man 手册页) * 将当前行提到页首:'''zt''',页尾:'''zb'''。 * '''gg''' 定位到文件开头,'''G''' 定位到文件的结尾。 |
Line 36: | Line 80: |
* 去掉所有的行尾空格:“''':%s/\s\+$//'''”。 * 去掉所有的空白行:“''':%s/\(\s*\n\)\+/\r/'''”。 * 去掉所有的“//”注释:“''':%s!\s*//.*!!'''”。 * 去掉所有的“/* */”注释:“''':%s!\s*/\*\_.\{-}\*/\s*! !g'''”。 |
|
Line 39: | Line 87: |
* 寄存器 * 除了有一个无名寄存器外,Vim 还有一大堆有名的寄存器,可以通过“"”(参见“:help "”)或“Ctrl-R”(参见“:help i_CTRL-R”和“:help c_CTRL-R”)加寄存器名(字母、数字和某些特殊字符,参见“:help registers”;“无名”寄存器的名字是“"”)来访问。比如,你先使用“"ayy”复制了一行,然后使用“dd”删掉了一行,然后移动光标到要复制到的位置,就可以使用“"aP”把先前复制的内容粘贴上去了。 * 在使用 X Window 系统时,有两个特殊的寄存器是需要注意一下的:“"*”访问的寄存器是 X 的主选择区域(primary selection),“"+”访问的寄存器是 X 的剪贴板(clipboard)。 * 还有一个很特殊的“寄存器”:“=”。在插入模式或命令模式中,键入“Ctrl-R=”,Vim 会提示你输入一个表达式,普通的整数运算在此完全有效。 |
|
Line 43: | Line 95: |
* Modeline for C coding style(see above): '''/* vim:shiftwidth=8:tabstop=8:filetype=c:norl: */''' * 把光标移到要重新格式化的文本开头,使用'''gq'''命令后面跟一个光标移动命令确定重新格式化的范围。比如'''gq}'''(格式化一段),'''gq5j'''(格式化 5 行),'''gqG'''(格式化至文件末尾)。 * ''':set list''' 查看所有不可见字符; ''':set nolist''' 关闭 |
|
Line 47: | Line 102: |
* :tag 关键字(跳转到与“关键字”匹配的标记处) * :tselect [关键字](显示与“关键字”匹配的标记列表,输入数字跳转到指定的标记) * :tjump [关键字](类似于“:tselect”,但当匹配项只有一个时直接跳转至标记处而不再显示列表) * :tn(跳转到下一个匹配的标记处) * :tp(跳转到上一个匹配的标记处) * Ctrl-](跳转到与光标下的关键字匹配的标记处;除“关键字”直接从光标位置自动获得外,功能与“:tags”相同) * g](与“Ctrl-]”功能类似,但使用的命令是“:tselect”) * g Ctrl-](与“Ctrl-]”功能类似,但使用的命令是“:tjump”) * Ctrl-T(跳转回上次使用以上命令跳转前的位置) |
|
Line 50: | Line 114: |
* ''':split''' 上下分屏; ''':vsplit''' 左右分屏。 | |
Line 51: | Line 116: |
* 可以使用“gf”命令方便地跳转到光标下的文件名所代表的文件中。使用“Ctrl-O”(参见“:help CTRL-O”)可返回到原先的文件中。 | |
Line 53: | Line 119: |
* 如果想把外部命令执行的结果插入到当前编辑的缓冲区中,可以考虑使用“:r!”。 * 当使用可视模式选中文本行后然后键入“:!”(命令行上将出现“:'<,'>!”,表示命令的范围是选定的文本),或者使用“:%!”(表示命令的范围是整个缓冲区中的文本),Vim 在执行后面的命令时,将把命令范围里的文本行作为后面执行的命令标准输入,并用命令执行后的标准输出替换当前缓冲区中的这些文本行。 * 要对所有的非空行进行编号,只需要“:%!nl”;要对包含空行的所有行进行编号?OK,“:%!nl -ba”。 |
|
Line 55: | Line 124: |
* :cn(显示下一个错误) * :cp(显示上一个错误) * :cl(列出所有的错误及其编号) * :cc(跳转到指定编号的错误) * :copen(打开快速修订窗口,在其中显示所有错误,可在错误上双击鼠标或按回车键跳转至该错误) |
|
Line 58: | Line 132: |
=== Task === ==== Editing in hex mode ==== 1. vim -b NSLU2?_v23R29.bin 2. :%!xxd 3. When completed, :%!xxd -r |
1. 关于我
个人信息
Jick Nan |
|
邮件 |
|
主页 |
|
Blog |
2. C
2.1. Coding Style
先看 [http://lxr.linux.no/source/Documentation/CodingStyle Linux kernel coding style]
2.1.1. Indentation
- 8-char indent
- If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.
- Don't put multiple statements on a single line unless you have something to hide.
- Get a decent editor, and don't leave whitespace at the end of lines.
2.1.2. Breaking long lines and strings
- Coding style is all about readability and maintainabilty using commonly avaible tools.
- The limit on the length of lines is 80 colums and this is a hard limit.
2.1.3. Placing braces
- The preferred way, as shown to us by the prophets Kernighan and Ritchie, is to put the opening brace last on the line, and put the closing brace first.
- namely function have the opening brace at the begining of the next line.
2.1.4. Naming
- C is a Spartan language, and so should your naming be.
- GLOBAL variables (to be used only if you _really_need them) need to have descriptive name, as do global functions.
- LOCAL variable name should be short, and to the point.
2.1.5. Fuctions
- Fuctions should be short and sweet, and do just one thind.
- Another measure of the functions is the number of local variables, They shouldn't exceed 5-10, or you're doing something wrong.
2.1.6. Centralized exiting of functions
- About gogo statement.
2.1.7. Commenting
- Comments are good, but there is also a demage of over-commenting.
- Generally, you want your comments to tell WHAT your code does, not HOW.
2.1.8. You've made a mess of it
- About GNU Emacs and GNU indent.
- 'indent' is not a fix for bad programing.
2.1.9. Data structures
- Data structures that have visibility outside the single-threaded environment they are created and destoryed in should always have reference counts.
- Locking is _not_ a replacement for reference counting.
- If another thread can find your data structure, and you don't have a reference count on it, you almost certainly have a bug.
2.1.10. Macros, Enums, Inline functions and RTL
- Names of macros defining contants and labels in enums are capitalized.
- Enums are preferred when defining serval related constants.
- Generally, inline functions are preferable to macros resembling functions.
- Macros with multiple statements should be enclosed in a do-while block.
2.2. malloc, calloc, free
- 一个严重的错误:释放不是通过调用 malloc, calloc 得到的指针所指向的存储空间。
- 使用已释放空间同样是错误的。
3. Gnu Tools
3.1. Vim
先看 [http://chinaunix.net/jh/28/194152.html 进行有效编辑的七种习惯] by Bram Moolenaar, the author of vim.
再看 [http://www-128.ibm.com/developerworks/cn/linux/l-tip-vim1/ Vim 实用技术] at DeveloperWorks.
3.1.1. Edit a file
3.1.1.1. Move around quickly
If you set the incsearch option, Vim will show the first match for the pattern, while you are still typing it. This quickly shows a typo in the pattern.
If you set hlsearch option, Vim will highlight all matches for the pattern with a yellow background. :nohlsearch will close it.
If you see a specific word and want to search for other occurrences of the same word, use the * command to grab the word from under the cursor and search for the next one, while # search for the previous one.
- In structured text(C/C++)
% can jump to many different matching items. It is very useful to check if() and {} constructs are balanced properly.
Use [{ to jump back to the "{" at the start of the current code block, and ]} for '}'.
Use gd to jump from the use of variable to its local declaration.
可视(visual)模式,用于选定文本块;可以在正常模式下输入v(小写)来按字符选定,输入V(大写)来按行选定,或输入Ctrl-V来按方块选定。
`.(跳转到最近修改过的位置)
ZQ(无条件退出)
ZZ(存盘退出)
ga(显示光标下的字符在当前使用的 encoding 下的内码)
guw(光标下的单词变为小写)
gUw(光标下的单词变为大写)
:TOhtml (根据 Vim 的语法加亮的方式生成 HTML 代码)
K (显示光标下单词的 man 手册页)
将当前行提到页首:zt,页尾:zb。
gg 定位到文件开头,G 定位到文件的结尾。
3.1.1.2. Don't type it twice
If you want to change one word into another in the whole file, you can use the :s(substitute) command.
去掉所有的行尾空格:“:%s/\s\+$//”。
去掉所有的空白行:“:%s/\(\s*\n\)\+/\r/”。
去掉所有的“//”注释:“:%s!\s*//.*!!”。
去掉所有的“/* */”注释:“:%s!\s*/\*\_.\{-}\*/\s*! !g”。
If you want to change one word into another only a few locations, a quick method is to use the * command to find the next occurrence of the word and use cw to change the word. Then type n to find the next word and .(dot) to repeat the cw command.
CTRL-N can auto complete function and variable names. Vim looks up words in the file you are editing, and also in #include'd files.
Vim has a mechanism to record a macro. You type qa to start recording into register 'a'. Then you type your commands as usual and finally hit q again to stop recording. When you want to repeat the recorded commands you type @a.
- 寄存器
- 除了有一个无名寄存器外,Vim 还有一大堆有名的寄存器,可以通过“"”(参见“:help "”)或“Ctrl-R”(参见“:help i_CTRL-R”和“:help c_CTRL-R”)加寄存器名(字母、数字和某些特殊字符,参见“:help registers”;“无名”寄存器的名字是“"”)来访问。比如,你先使用“"ayy”复制了一行,然后使用“dd”删掉了一行,然后移动光标到要复制到的位置,就可以使用“"aP”把先前复制的内容粘贴上去了。
- 在使用 X Window 系统时,有两个特殊的寄存器是需要注意一下的:“"*”访问的寄存器是 X 的主选择区域(primary selection),“"+”访问的寄存器是 X 的剪贴板(clipboard)。
- 还有一个很特殊的“寄存器”:“=”。在插入模式或命令模式中,键入“Ctrl-R=”,Vim 会提示你输入一个表达式,普通的整数运算在此完全有效。
3.1.1.3. Fix it when it's wrong
It's normal to make errors while typing, this can be corrected with abbreviations: :abbr Lunix Linux.
The same mechanism can be used to type a long word with just a few characters: :abbr pn pinguin.
- To find errors in your text Vim has a clever hightlighting mechanism. This was actually meant to be used to do syntax hightlighting of programs, but it can catch and highlight errors as well.
Modeline for C coding style(see above): /* vim:shiftwidth=8:tabstop=8:filetype=c:norl: */
把光标移到要重新格式化的文本开头,使用gq命令后面跟一个光标移动命令确定重新格式化的范围。比如gq}(格式化一段),gq5j(格式化 5 行),gqG(格式化至文件末尾)。
:set list 查看所有不可见字符; :set nolist 关闭
- A more complex example: English text.
3.1.2. Edit more files
3.1.2.1. A file seldom comes alone
- Tag mechanism works for jumping between files.
- :tag 关键字(跳转到与“关键字”匹配的标记处)
- :tselect [关键字](显示与“关键字”匹配的标记列表,输入数字跳转到指定的标记)
- :tjump [关键字](类似于“:tselect”,但当匹配项只有一个时直接跳转至标记处而不再显示列表)
- :tn(跳转到下一个匹配的标记处)
- :tp(跳转到上一个匹配的标记处)
- Ctrl-](跳转到与光标下的关键字匹配的标记处;除“关键字”直接从光标位置自动获得外,功能与“:tags”相同)
- g](与“Ctrl-]”功能类似,但使用的命令是“:tselect”)
- g Ctrl-](与“Ctrl-]”功能类似,但使用的命令是“:tjump”)
- Ctrl-T(跳转回上次使用以上命令跳转前的位置)
Another powerful mechanism is to find all occurrences of a name in a group of files, using the :grep command. Vim makes a list of all matches, and jumps to the first one. The :cn command takes you to each next match.
Positions the cursor on the name of the function in your file and type [i: Vim will show a list of all matches for the function name in the included files.
- In vim you can split the text erea in several parts to edit different files.
:split 上下分屏; :vsplit 左右分屏。
- There are more uses of multiple window. The preview-tag mechanism is a very good example.
- 可以使用“gf”命令方便地跳转到光标下的文件名所代表的文件中。使用“Ctrl-O”(参见“:help CTRL-O”)可返回到原先的文件中。
3.1.2.2. Let's work together
Select some structured text in a list and sort it: !sort.
- 如果想把外部命令执行的结果插入到当前编辑的缓冲区中,可以考虑使用“:r!”。
当使用可视模式选中文本行后然后键入“:!”(命令行上将出现“:'<,'>!”,表示命令的范围是选定的文本),或者使用“:%!”(表示命令的范围是整个缓冲区中的文本),Vim 在执行后面的命令时,将把命令范围里的文本行作为后面执行的命令标准输入,并用命令执行后的标准输出替换当前缓冲区中的这些文本行。
- 要对所有的非空行进行编号,只需要“:%!nl”;要对包含空行的所有行进行编号?OK,“:%!nl -ba”。
3.1.2.3. Text is structured
One of the simpler things is to speed up the edit-compile-fix cycle. Vim has the :make command, which starts your compilations, catches the errors it produces and lets you jump to the error locations to fix the problems.
- :cn(显示下一个错误)
- :cp(显示上一个错误)
- :cl(列出所有的错误及其编号)
- :cc(跳转到指定编号的错误)
- :copen(打开快速修订窗口,在其中显示所有错误,可在错误上双击鼠标或按回车键跳转至该错误)
3.1.3. Sharpen the saw
3.1.3.1. Make it a habit
- You need to learn new commands and turn them into a habit.
3.1.4. Task
3.1.4.1. Editing in hex mode
- vim -b NSLU2?_v23R29.bin
- :%!xxd
- When completed, :%!xxd -r
3.2. CVS
先看 [http://www.chedong.com/tech/cvs_card.html CVS使用手册] by Chedong.
4. 反馈
欢迎大家对我说三道四哪