Bad taste of code Reconstruction

Source: Internet
Author: User
Document directory
  • I. Duplicated code (repeated code)
  • Ii. Long method (long function)

It doesn't matter if you write code with bad taste. The painful thing is that the Code is full of bad taste. Every time you see such code, there is an impulse to refactor. When the Refactoring is also very painful, you don't have to worry about it. rewrite it.

Is your code bad? You will find it.

I. Duplicated code (repeated code)

If you see the same code in more than one place, congratulations, duplicated code is coming. These codes are probably produced by Control + C and Control + V. Some people think this is much faster than writing a method.

See the following two methods:

Public Function changeaction (roleid: int, resarray: array, actionid: INT): void {If (_ roledictionary [roleid]! = NULL) {// assign var body: Int = resarray [0] to the part ID; var clothes: Int = resarray [1]; var hair: Int = resarray [2]; vaR weapon: Int = resarray [3]; If (INT (body) <-2 | int (clothes) <-2 | int (hair) <-2 | int (weapon) <-2) {return;} var bodyarray: array = getaction (body, actionid); var clothesarray: array = getaction (clothes, actionid); var hairarray: array = getaction (hair, actionid); var weapo Narray: array = getaction (weapon, actionid); var mymovieclip: movi1_statemachine = _ roledictionary [roleid]; mymovieclip. changeaction (actionid, [bodyarray, clothesarray, hairarray, weaponarray]);} public function changeweapon (roleid: int, resarray: array, actionid: INT ): void {If (_ roledictionary [roleid]! = NULL) {// assign var body: Int = resarray [0] to the part ID; var clothes: Int = resarray [1]; var hair: Int = resarray [2]; vaR weapon: Int = resarray [3]; If (INT (body) <-2 | int (clothes) <-2 | int (hair) <-2 | int (weapon) <-2) {return;} var bodyarray: array = getaction (body, actionid); var clothesarray: array = getaction (clothes, actionid); var hairarray: array = getaction (hair, actionid); var weaponarray: array = getaction (weapon, actionid); var mymovieclip: Rule = _ roledictionary [roleid]; mymovieclip. changeall ([int (body), INT (clothes), INT (hair), INT (weapon)], [bodyarray, clothesarray, hairarray, weaponarray]);}

A simple solution is to use the extract mathod method to extract duplicate code and call it:

public function changeAction(roleId : int, resArray : Array, actionId : int) : void{if (_roleDictionary[roleId] != null){var myMovieClip : MovieClipStateMachine = _roleDictionary[roleId];myMovieClip.changeAction(actionId, getRoleResList(actionId,resArray));}}public function changeWeapon(roleId : int, resArray : Array, actionId : int) : void{if (_roleDictionary[roleId] != null){var myMovieClip : MovieClipStateMachine = _roleDictionary[roleId];myMovieClip.changeAll([int(resArray[0]), int(resArray[1]), int(resArray[2]), int(resArray[3])],                     getRoleResList(actionId,resArray));}}private function getRoleResList(actionId:int,resArray:Array):Array{if(resArray==null || resArray.length!=4) return null;var body:int=int(resArray[0]);var clothes:int=int(resArray[1]);var hair:int=int(resArray[2]);var weapon:int=int(resArray[3]);if(body<-2 || clothes<-2 || weapon<-2 || weapon<-2) return;var data:Array=new Array();data[0]=getAction(body,actionId);data[1]=getAction(clothes,actionId);data[2]=getAction(hair,actionId);data[3]=getAction(weapon,actionId);return data;}

The Code repeats in the following situations and solutions:

  • Multiple Functions contain the same code: extract mathod to extract duplicate code
  • Multiple sibling classes contain the same method: first use the extract mathod method, and then use the pull up method to put the extracted code into the superclass.
  • Multiple irrelevant classes contain the same code: one is put into a class, the other is called, and the other is extracted from an independent class using the extract class method, all classes call the interfaces of this independent class, etc.
  • The code in multiple classes is similar: Use the extract mathod method to separate similarity and difference, and use form template method to obtain a template method
  • Multiple Functions use different algorithms to do the same thing: Use the substitute method to select a better one and kill other functions. What do you think of the harem?
Ii. Long method (long function)

The function is too long. If the code is not clearly written, it is no different from reading tianshu. You have to add comments to inform you of your intention. If you want to use comments to tell you the implementation method, it is really necessary to break down this function. Remember, use code to explain the code. The code is used to briefly describe the purpose and usage of a function, rather than to describe how to implement a function.

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.