Self-expressionCodeFeatures
1. the return value of the Code has clear meanings.
Check a registrationProgramThis is written in non-self-expression mode.
1 Public String registerlesson ( Int Userid, Int Lessonid ){ 2 Int Code = Checkuserid (userid ); 3 If (Code! = 0 ){ 4 Return "User does not exist ." ; 5 } 6 7 Code = Checklessonid (lessonid ); 8 If (Code! = 0 ){ 9 Return "Lesson does not exist ." ; 10 } 11 12 Code = Checklessonexpired (lessonid ); 13 If (Code! = 0 ){ 14 Return "Lesson is expired ." ; 15 } 16 17 Code = Checklessonvacance (lessonid ); 18 If (Code! = 0 ){ 19 Return "Lesson has no vacance ." ; 20 } 21 22 Code = Registerlessonremote (userid, lessonid ); 23 If (Code! = 0 ){ 24 Return "Failed to register lesson ." ; 25 } 26 Return "" ; 27 }
The above Code has the following reading problems:
1. The meaning of the Code variable is constantly changing.
2. It is difficult to use the string type of the returned value in the upper layer.
This code is written as follows in self-expression mode:
Public VoidRegisterlesson (IntUserid,IntLessonid)ThrowsBusinessexception {checkuserid (userid); checklessonid (lessonid); role (lessonid); checklessonvacance (lessonid); registerlessonremote (userid, lessonid );}
The two code writing methods differ significantly. The self-expression method has the following advantages:
1. No local variables
2. No return value is required for the code.
3. Method signature exceptions are convenient for upper-layer calls.
2. There is basically no need to comment out
The reason why the code needs to be commented out is that the name and parameters of the Code are not easy to understand. For example, (a + B)> 1. It is not easy to understand.
Self-expressing code claims the writing of code itself is your own meaning. This is the origin of Self-expressing code.
The self-expression code is like reading a novel. For example:
1 Public Abstract ClassPlant {2PublicWeapon weapon;3PublicHealth health;4 5Public VoidFire (){6 Weapon. Fire ();7 }8}
The definition of weapons:
1 Public Abstract ClassWeapon {2PublicBullet bullet;3Public VoidFire (){4 Gameworld. getinstance. append (bullet );5 }6}
In this way, the code is easy to understand without comments.
3. Clear code structure
For example, when writing an android input method, you must determine the language type of the Input Method: Hiragana, full-Katakana, half-Katakana, full-alphabet, half-alphabet, and half-number. In addition, you need to consider the input keyboard type, QWERTY keyboard, Kana keyboard, and 12-key keyboard.
When determining an input, the code will be written as follows:
1 Public Boolean Onkey ( Int Primarycode ){ 2 String keychar = "" ; 3 If (Keymode = Qwerty ){ 4 Keychar =Getqwertchar (primarycode ); 5 } Else If (Keymode = Japanese_50 ){ 6 Keychar = Getjapanese50char (primarycode ); 7 } Else If (Keymode = Number ){ 8 Keychar = Getnumberchar (primarycode ); 9 } 10 Appendcomposingchar (keychar ); 11 }
Each method in the above Code must determine the current language type, for example:
1 Public String getjapanese50char ( Int Primarycode ){ 2 Switch (Language ){ 3 Case Hiragana: 4 Return Hiragana_table [primaryode]; 5 Case Full_katakana: 6 Return Full_katakana [primarycode]; 7 Case Half_katakana: 8 Return Half_katakana [primarycode]; 9 Case Full_alphabet: 10 Return Full_alphabet [primarycode]; 11 Case Half_alphabet: 12 Return Half_alphabet [primarycode]; 13 Case Half_number: 14 Return Half_number [primarycode]; 15 } 16 }
Similarly, other methods should be written like this.
However, when the key on the keyboard is pressed, it is not a case of assembling candidate text, and the code needs to be extended. There will be duplicates and similarities everywhere.
A simple rewrite is as follows:
1 Public InterfaceLanguage {2String getkey (IntPrimarycode );3}
1 PublicHiraganaImplementsLanguage {2PublicString getkey (IntPrimarycode ){3ReturnHiragana [primarycode];4 }5}
This gives the Code better scalability.
The layers are clear to make the code easier to understand.