Original: https://zhuanlan.zhihu.com/p/30905033. The author is himself.
This series will be a comparison of the same code in Chinese and two versions of the English name. The purpose of this paper is to demonstrate Chinese naming, discover the problems that may be encountered in naming, and explore code styles (Chinese named ' routines ') and so on. The naming style in the example is based only on a very limited number of personal practices, and I want to be inspired.
Not within the scope of the discussion is:
- The readability of the code in Chinese and English is higher or lower. Personally believe in both English and Chinese can write good readability code. The difference is that the native language developers are more sensitive to which version, and read and write maintenance is more labor-saving.
- optimization/style problems of the original code itself. If you think there is more representative code, please do not hesitate to recommend.
- The convenience of code entry. Before the question and response to the use of Chinese naming in code "Chinese input is too slow, reduce development efficiency" section is involved in the plan to open another topic to explore.
The following code is selected from the Clean code:a Handbook of Agile Software craftsmanship, page 19. has been improved by the author's naming. As I have not read the book, if this code in the post of a section still has a naming improvement, please trouble to point out.
PublicList<Cell>Getflaggedcells () {list<cell > flaggedcells = new ArrayList< Span class= "o" ><cell> (); for (cell cell : Gameboard) if (cell. Isflagged ()) flaggedcells. (cellreturn flaggedcells /span>
Chinese named version:
PublicList<Lattice class> () {list< lattice class > subject lattice = new < lattice > (); for ( class a : mined area Span class= "O" >) if ( some . Is the marked ()) . ( a return /span>
Several typical Chinese naming problems have been touched. First of all, there is no case classification and variable name (cell cell). The individual uses the suffix ' class ' in the class name. For the time being, there is a case where the class name itself is terminated with a ' class ' (that would appear to be XXX类类
named), and even so, it should be avoided by changing 类型
to the end XXX类型类
. The variable name is also consistent with the class name, where all 格类
variables are xx格
named Same as the English naming method.
Below, is the difference between singular and plural. flaggedCells
is a list, which is named after the plural, which means "may contain more than one lattice". In fact, this variable may contain only a single lattice. Nouns in Chinese do not divide the plural, just in line with the semantics that "this variable may contain single or multiple elements". So the name is feasible for the 被标的格
senses. If the semantics need to be emphasized in more than one (cannot be single or not), consider adding modifiers such as 多个
prefixes and so on.
In contrast, how to represent the singular in a for loop is somewhat troublesome. The semantics of this for loop are probably: for one/each cell in the gameboard, if the cell is flagged, add the cell to...
no distinction is made in the original code one/each cell
. The the cell
corresponding Chinese is about. It is 对于雷区中的每一格, 如果这一格是被标的, 就把这一格加到...
also difficult to use a word in the code to represent the 每一格
这一格
two distinct semantics. In addition the possible names have 一格
, 每格
, 此格
without the need to highlight the singular of the case directly and 格
so on.
The method name of the get prefix, the Chinese adopted the corresponding 取
, with the verb phrase can be distinguished from the variable name, which is similar to the English name. There is a potential technical problem, which is the special rule of get in the bean. The individual thinks that in this case can be used mixed, that is get被标的格
. this spring This is true in the boot demo program.
The method name of the is prefix, which is generally considered to be a method of returning a Boolean value. The naming used here 是被标的
is a straightforward one. It retains both the 是
return Boolean value as a representation and the passive voice. The individual feels slightly cumbersome, but is consistent with other names (taking ' labeled ' ' Lattice ', ' marked '), and easy to use in other situations, such as isRunning
是运行中的
, isTerriblyDamaged
and 是被严重损坏的
so on. It's a workable style. It was originally used 是被标记的
, but the feeling of removing one word was acceptable.
In addition, gameBoard
there is no literal translation, but it 雷区
is used because the code is supposed to be selected from a mine Sweeper Game (minesweeper) in the original, so it feels more appropriate. Of course, the original code gameBoard
can also be used mineField
Make this section of code separate out more contextual. However, as stated at the beginning, this article does not compare the readability between Chinese and English code. There is no way to find gameBoard
the corresponding Chinese.
Again welcome all sorts of scrutiny.
A case of Java in the code comparison series of Chinese and English