Causes of poor code generation (2) Insufficient programming language knowledge (Continued 3)

Source: Internet
Author: User

6. Empty Object Model

When you want to access an object that may be empty, you need to perform null verification.

For example, the following example.

 
1 Public ClassUser {2IntID;3 String name;4Public VoidSave (){5System. Out. println ("Save User");6 }7}

 

  1   Public   void  saveuser ( int   ID, string name) {  2  User user =  finduserbyid (ID);   3   If  (user =  null  ) {  4   return  ;   5  }  6  User. name =  name;   7   User. save ();   8 }

However, when such an empty VerificationCodeWhen it increases, it reduces the speed of understanding the code.

 
1 Public ClassNulluserExtendsUser {2Public VoidSave (){3//Do nothing4 }5}
1 Public VoidSaveuser (IntID, string name ){2User user =Finduserbyid (ID );3User. Name =Name;4 User. Save ();5}

* In the above Code, finduserbyid needs to be transformed to ensure that when null is returned, nulluser is now returned. In this way, the called code becomes very concise.
In this way, the null Determination of multiple calls can be combined at the underlying processing level, and the upper layer does not need to worry about whether the object is empty or not.

7. Several Forms of constants

Constants are inevitable for software development. Common constants are:

1 Public Static Final IntVersion = 1;

But constants are not in this form.
A. The constant type can be not only the basic type

 
1 Public static FinalKeyboard soft_keyboard =NewSoftkeyboard ();2 Public Static FinalKeyboard hard_keyboard =NewHardkeyboard ();

B. constants can also be enumerated.

1 Public EnumKeyboardstate {2 Invisible, visible, unavailable;3}

8. Functions of Map

Map is a data structure that stores data in the form of key-value pairs. MAP is widely used in writing code, but in addition to simple data storage, MAP also has some wonderful usage methods.

A. remove duplicates.

For example, remove the duplicate address when sending an email.

 1   Public List <string> removeduplicatedmails (list <string> Addresses ){  2 Map <string, string> instinctmails =New Hashmap <string, string> ();  3       For  (String a: addresses ){  4           If (! Instinctmails. containskey ()){  5   Instinctmails. Put (A, );  6   }  7   }  8  9 List <string> instinctmaillist = New Arraylist <string> ();  10 Set keys = Instinctmails. keyset ();  11 Iterator I = Keys. iterator ();  12       While  (I. hasnext ()){  13   Instinctmaillist. Add (string) I. Next ());  14  }  15       Return  Instinctmaillist;  16 }

B. Statistics by group
For example, there is a set of data:

Store, sales, month
Store, sales, month

You want to merge the data by store to calculate the total sales.

 1   Public Map <integer, float> makesalesreport (list <salesinfo> Sales ){  2 Map <integer, float> report = New Hashmap <integer, float>();  3       For  (Salesinfo Si: Sales ){  4           If  (Report. containskey (Si. Shop )){  5 Report. Put (Si. Shop, repot. Get (Si. Shop) + Si. Price );  6 } Else  {  7   Report. Put (Si. Shop, Si. Price );  8  }  9   }  10       Return  Report;  11 }

ThisAlgorithmThe structure is much simpler than finding the key through several nested loops and then comparing and adding the key.

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.