What is the incorrect answer?
Recently, enthusiastic readers have suggested whether to find a poorly-written engineering example and analyzeCodeErrors, so that we can more intuitively understand the bad code writing. I have been dealing with a lot of bad code for so many years, but it is hard to make it public. However, later I came into contact with openwnn, which is a good project. Its code is almost filled with various bad habits of writing code. Just like the incorrect topic book of middle school students, opening and reading all kinds of errors can be seen. It is enough to watch off and stop making the same mistake.
In this chapter, we will analyze the code problems of openwnn, and describe them in terms of naming, commenting, structure, and style.
What is openwnn?
I mentioned this project at least twice in my previous blog. This is an open-source Japanese Input Method for Android, developed by OMRON software co., Ltd. Make public from Android cupcake. It is said that it supports Chinese, English, and Korean (I have only tried Japanese and English, but not Chinese or Korean ).
How to obtain openwnnSource codeWhat about it?
You can follow the android source code download guide or search for it on the search engine.
The following is an available address:
Http://gitorious.org/sourcecode/google_android_2_2/trees/e850cee/jp/co/omronsoft/openwnn
Why openwnn?
The first reason is that the overall feeling of this Code is that there is little experience.ProgramFans are sitting and writing together.
The second reason is that he is the only publicly available source code in the poor code I have been exposed.
Before learning why it is terrible, download its code and run it on the android simulator or device.
Then, I have some knowledge about the basic concepts of the android input method. Then read the following content. Otherwise, there may be no clue in some places.
The purpose of this article is to discuss coding issues, not usability or software quality issues.
--------------------- Split ---------------------
I. naming issues
1. Negative naming
Severity: high
Negative naming allows readers to understand it only after a pause. For example, if (! Mnoinput) is not as easy to understand as if (hasinput.
There are several negative naming rules in openwnn.
Defasoftsoftkeyboard. mnoinput indicates no input
Defasoftsoftkeyboard. mdisablekeyinput indicates that keyboard input is disabled.
Defasoftsoftkeyboard. mhardkeyboardhidden indicates hard keyboard hiding
Isnotcomposing in openwnnjajp. onupdateselection indicates that composingtext is not selected.
However, this does not mean that the negative naming must be poor, such as invalid_keymode.
2. Sequential name
Severity: high
Sequential naming is in the form of var1, var2. .. Varn ~ N to obtain valuable information.
Sequential name in openwnn
Composingtext. layer0, layer1, and Layer2 indicate English-> Kana-> three-layer conversion of Chinese characters.
In fact, it is better to name layer_alphabet, layer_kana, and layer_japanese.
3. Improper use of words
Severity: low
Improper use of words means that a name uses a word that is close to the actual meaning, but it is not appropriate. A better word can indicate a more appropriate meaning.
Defasoftsoftkeyboardjajp. mlimitedkeymode indicates an array of restricted keyboard types that can be switched. In fact, constraint is more appropriate to indicate this meaning than limited.
Openwnn. limit_input_number indicates the maximum allowed input length. In fact, it can be named max_input_length because limit and number have other meanings.
4. Unsatisfactory words
Severity: Medium
Words used cannot accurately express the true meaning. They are classified as unsatisfactory words. Bad words may lead to misunderstandings.
For example: openwnnjajp.UpdatepredictionCandidates, the first variable in the method, is named as an array, list, and other variables. However, it is an int variable used to store the number of candidates. The correct name is candidatescount.
5. Different words
Severity: high
The statement does not mean that a method or class executes a job other than the function of its declaration. This problem is very serious. If someone else (including yourself a few months later) takes over the code, be very careful when planning to reuse the code by name, however, this kind of caution is caused by human errors. If the name is the same as the actual name, you do not have to worry too much about the code to be reused.
Deafultsoftkeyboardjajp.Toggleshiftlock(). The changekeyboard () method is also called.
Changekeyboard will update a lot of underlying data... this also involves structure issues. The structure analysis will be detailed.
6. Incorrect prefix or suffix
Severity: low
Incorrect prefix and suffix may cause some obstacles to reading (Reading is a bit strange), but it does not affect overall reading.
Has ~ /Is ~ /, That is, the prefix used for methods should not be used for variables.
Openwnnjajp.Mhascontinuedprediction wants to express whether there are any future predictions, but if this variable is named mpredictedfurther, it can reach 1) Shorten the name, 2) Avoid improper use of words. Similarly, textcandidateviewmanager has several similar names: misfullview and misscaleup.
~ If the current prefix appears in parallel with Prev and next, it makes no sense to separate the current prefix.
Current softsoftkeyboard. mcurrentkeyboard current does not make sense.
~ List ,~ Map should not appear in the suffix, which may be affected by the Hungarian naming method, but it is unnecessary to attach list to the name.
For example:Keyboardlist can be written as keyboards.
7. Long-lived name
Severity: Medium
Long-lived names cause hard reading. This is a recognized issue.
Openwnnjajp.Processkeyeventnoinputcandidateshown(Keyevent eV) Method
The reason for this name length is: it contains certain restrictions: no input + candidate shown (refer to the called code to know that these are two restrictions ). Therefore, this name is actually a structural issue. This will be discussed in detail in the structure issue. Here we will briefly discuss how to solve this problem.
In fact, this code is intended to solve the problem of non-input buttons other than the left and right keys during candidate display. Then, this should be handled by openwnn. onkeydown. This method should classify various buttons and then hand them to different categories for processing. This process may belong to functionalkey. onkeydown and arrowkey. onkeydown.
8. Local naming
Severity: low
Local languages use words that can only be understood by local people for naming. Openwnn has many naming cases in Japanese. Besides the necessary names such as hiragana and katakan, some of them should not be named in local languages. For example, isrenbun (this spelling is still wrong. In fact, I want to write isrenban) to indicate continuous numbers (continuousnumbers)
Defasoftsoftkeyboard. keycode_qwerty_zen_hira
Defasoftsoftkeyboard. keycode_qwerty_han_num
Here, Han and Zen indicate halfwidth and fullwidth. If they are changed to half and full, they are much easier to understand.
Others include eisu_kana, convhansuuji, convhaneiji, convzeneiji, moji, and inputromaji.
Because this is a developed input method, the severity of this problem is as follows: low. In fact, this is a big reading problem for other readers.
9. Similar naming
Severity: Medium
The name is similar, but different meanings may cause reading and calling problems.
What are the differences between openwnn. mhardshift, openwnn. mshiftpressing, and openwnnjajp. mshifton?
10. Name in upper case
Severity: Medium
Naming with uppercase letters can cause many problems, including rule exceptions when you set the upper Letter of the first letter in the future. The jajp name and EN name of the package in openwnn belong to this situation. This is also the case with the wnnpos class.
11. Meaningless naming
Severity: high
In the face of meaningless naming, the reader cannot obtain valid information from it, and does not know how to use such objects (methods and variables ).
Mstringbuff in kanaconverter is such a variable.