Teach you how to create a keyword matching project (search engine) ---- 18th days, teach you how to do 18th days
18th days
Guest string: hacker form artifact
Those things we 've gone through: Database things
Start Point: Teach you how to create a keyword matching project (search engine) ---- Day 1
Review: Experts teach you how to perform keyword matching projects (search engines)-17th days
When talking about the code handed over by Shuai to the boss, he experienced a large number of Shuai and he really felt wronged.
From the perspective of the Technical Director, do the technical director really need to read the supervision code?
I remember that many companies advocate benefits, and the benefits serve as a measure of the value of a technical director.
Is the technical director wrong? Is it out of his duty range?
In fact, the boss saw that the exec method in the LinklistCharListHandle class had three levels of foreach, so he gave the handsome guy a batch.
Better than the boss, or the boss has higher expectations for the handsome boy.
He tried to extract the foreach as much as possible:
class LinklistCharListHandle extends CharListHandle { public function exec(){ $sql = "select word from category_linklist where cid='$this->selectorItem->cid'"; $linklist = DB::makeArray($sql); foreach($linklist as $strWords){ $words = explode(",",$strWords); $this->propertiesTransferCharlist($words); } } public function propertiesTransferCharlist($linkWords){ $properties = $this->selectorItem->getProperties(); foreach($properties as $property){ $this->charlist->addCore($property->value); if(in_array($property->value,$linkWords)){ $this->addCores($linkWords); } } } public function addCores($words){ foreach($words as $char){ $this->charlist->addCore($char); } }}
Shuai extracted two methods to make the program more understandable.
In fact, Shuai's approach is to use refactoring-one of the skills to improve the design of existing code, Extract Method to Extract functions.
Extract Method: Put This code into an independent function and let the function name parse the function's purpose.
Shuai has a sense of accomplishment. When he continued to give the code to the boss, he mentioned two points.
1. Why does propertiesTransferCharlist accept parameters.
2. addCores is the responsibility scope of that class.
He knows how to do the 2nd types of data. He moves the method to the Charlist class and the code is as follows:
<?phpclass CharList { private $core = array(); private $blacklist = array(); public function addCore($char){ if(!in_array($char,$this->core)) $this->core[] = $char; } public function addCores($words){ foreach($words as $char){ $this->addCore($char); } } public function getCore(){ return $this->core; } public function addBlacklist($char){ if(!in_array($char,$this->blacklist)) $this->blacklist[] = $char; } public function getBlacklist(){ return $this->blacklist; }}
In fact, Shuai's practice is to use refactoring-one of the skills to improve the design of existing code, the Move Method moving function.
Move Method: Create a new function with similar behavior in the class most commonly referenced by this function. Convert the old function into a pure delegate function, or remove the old function completely.
I really don't know what to do with 1st of them. I went to consult with the boss. The boss gave the code to Shuai, and the boss's code was:
<?phpclass LinklistCharListHandle extends CharListHandle { private static $linklist; public function exec(){ $this->propertiesTransferCharlist(); } public static function linklist($cid){ if(!isset(self::$linklist) || !isset(self::$linklist[$cid]) || self::$linklist[$cid] == null){ $sql = "select word from category_linklist where cid='$cid'"; self::$linklist[$cid] = DB::makeArray($sql); } return self::$linklist[$cid]; } public function propertiesTransferCharlist(){ $properties = $this->selectorItem->getProperties(); foreach($properties as $property){ $this->charlist->addCore($property->value); $this->extendCharlist($property->value); } } public function extendCharlist($char){ $linklist = self::linklist($this->selectorItem->cid); foreach($linklist as $strWords){ $words = explode(",",$strWords); if(in_array($char,$words)){ $this->charlist->addCores($words); } } } }
Shuai looked at it and made a sigh. The original Code was changed at will, and the difference was so big that it never felt like this before.
Shuai really hopes that he can find the boss on his own without having to go to the boss every day.