自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何成為一名優(yōu)秀的工程師(語義篇)

開發(fā) 后端
好的語義表達是團隊協(xié)作中高效迭代的潤滑劑,好的語義表達是線上未知代碼問題排查的指南針。

好的語義表達是團隊協(xié)作中高效迭代的潤滑劑,好的語義表達是線上未知代碼問題排查的指南針。

本篇文章巨長,如果你比較“懶”,來我講給你聽(直播中有更多細節(jié)) 回放地址

看完這個還不過癮?學習使你快樂?還想學習?快上車

不要讓其他人讀不懂你的代碼,其他人可能就是一周后的你。時刻以“如果你寫的這段代碼出現(xiàn)故障,一個陌生人接手你的代碼需要多久能處理完這個bug”來監(jiān)督自己。

日常中應(yīng)該多多刻意提升自己語義表達,百利而無一害。那么我們應(yīng)該從哪些細節(jié)去做好語義表達呢?  

如何成為一名優(yōu)秀的工程師(語義篇) 

以下代碼全為我的藝術(shù)創(chuàng)作,不屬于任何實際項目

命名

案例1 

  1. function getGoods($query, $shopId) 
  2.     $goodsId = Goods::add($query["uid"], $query["name"]); 
  3.     return Shop::add($goodsId, $shopId); 
  4.  
  5. class Goods 
  6.     public static function add($uid, $name
  7.     { 
  8.         $id = mt_rand(1, 100000); 
  9.         return $id; 
  10.     } 
  11.  
  12. class Shop 
  13.     public static function add($goodsId, $shopId) 
  14.     { 
  15.         $id = mt_rand(1, 100000); 
  16.         return $id; 
  17.     } 
  18. }  

如何成為一名優(yōu)秀的工程師(語義篇) 

案例2 

  1. function getUserInfo($teamId, $youId = []) 
  2. {  
  3. }  

如果僅僅有這個函數(shù)名和參數(shù)名,誰能猜到參數(shù)的意義呢? 

如何成為一名優(yōu)秀的工程師(語義篇) 

案例3

  1. class Db 
  2.     /** 
  3.      * @param string $table 數(shù)據(jù)庫表名 
  4.      * @param array  $data  新增數(shù)據(jù) 
  5.      * 
  6.      * @return int 新增主鍵 
  7.      */ 
  8.     public static function insert(string $table, array $data) 
  9.     { 
  10.         $id = mt_rand(1, 1000); 
  11.         return $id; 
  12.     } 
  13.  
  14. class ViewLogStore 
  15.     private $table = "view_log"
  16.  
  17.     function setHistory($data) 
  18.     { 
  19.         Db::insert($this->table, $data); 
  20.     } 
  21. }  

 

案例4

假如業(yè)務(wù)代碼里有這些類 

  1. class WechatUserModel{ 
  2. class WechatGroupModel{ 
  3. class WechatMessageModel{ 
  4. }  

而我們查詢數(shù)據(jù)庫發(fā)現(xiàn) 

 

這樣我們根據(jù)業(yè)務(wù)代碼就非常不方便找到對應(yīng)的表,而且其他人接手我們項目的時候,也會摸不著頭腦?;蛘哒f這可能是三個人三次迭代開發(fā)造成的,那么他們彼此都沒有去參考前面人的命名規(guī)則。

來自靈魂的拷問

 

注釋

說完命名,下面說下注釋。注釋里還有什么學問?Are you kidding me?

一個數(shù)組對象成員,你知道怎么寫嗎?

類的魔術(shù)方法調(diào)用的注釋,你知道怎么寫嗎?

對象數(shù)組 

  1. /** 
  2.  * @var Ads[] 
  3.  */ 
  4. public $adsList = [];  

 

  1. $blocks = [];/** @var $blocks Block[] **/  

如何成為一名優(yōu)秀的工程師(語義篇)

@method 的使用 

  1. /** 
  2.  * @link http://manual.phpdoc.org/HTMLframesConverter/default
  3.  * 
  4.  * @method static int search(string $query, $limit = 10, $offset = 0) 
  5.  */ 
  6. class SearchServiceProxy 
  7.     public static function __callStatic($method, $arguments) 
  8.     { 
  9.         if (!method_exists("SearchService", $method)) { 
  10.             throw new \LogicException(__CLASS__ . "::" . $method . " not found"); 
  11.         } 
  12.  
  13.         try { 
  14.             $data = call_user_func_array(["SearchService", $method], $arguments); 
  15.         } catch (\Exception $e) { 
  16.             error_log($e->getMessage()); 
  17.             return false
  18.         } 
  19.  
  20.         return $data; 
  21.     } 
  22. }  

 

 

 

@deprecated 使用  

  1. class SearchService 
  2.  
  3.     /** 
  4.      * @param string $query 
  5.      * @param int    $limit 
  6.      * @param int    $offset 
  7.      * 
  8.      * @return array 
  9.      * @deprecated 
  10.      */ 
  11.     public static function search(string $query, $limit = 10, $offset = 0) 
  12.     { 
  13.         return [ 
  14.             ["id" => 1, "aaa"], 
  15.             ["id" => 2, "bbb"], 
  16.         ]; 
  17.     } 
  18.  

 

 

 

注釋其他注意事項

注釋解釋張冠李戴,方法名更新,方法的功能業(yè)務(wù)注釋沒更新;復(fù)制別人的代碼把 @author 信息也復(fù)制過來了,錯誤了還要把鍋甩給別人。

注釋更多參考 http://manual.phpdoc.org/HTML...

函數(shù)、方法

案例1

先說明一句,不好的代碼不妨礙它成為一個優(yōu)秀的軟件。PHP MySQL 爛代碼多的去了。

找到一個開源軟件里面的代碼,功能非常搶到,但是這個方法內(nèi)容太多,一些不足點我標注出來了。 

 

 

 

案例2

拿上面我舉例子,還記得下面這種圖嗎? 

 

優(yōu)化方案1 

  1. class ArrayUtils{ 
  2.     public static function fetch($arr, $keys, $setNull = false
  3.     { 
  4.         $ret = array(); 
  5.         foreach($keys as $key
  6.         { 
  7.             if ($setNull) 
  8.             { 
  9.                 $ret[$key] = $arr[$key]; 
  10.             } 
  11.             else 
  12.             { 
  13.                 isset($arr[$key]) && $ret[$key] = $arr[$key]; 
  14.             } 
  15.         } 
  16.         return $ret; 
  17.     } 
  18.  
  19.  
  20. class ViewLogStore 
  21.     private $table = "view_log"
  22.  
  23.     function record($data) 
  24.     { 
  25.         $fields = array( 
  26.             'uid'
  27.             'url'
  28.             'referer'
  29.             'created_time' 
  30.         ); 
  31.         $data = ArrayUtils::fetch($data, $fields); 
  32.         Db::insert($this->table, $data); 
  33.     } 
  34.  

優(yōu)化方案2 

  1. class Db 
  2.     /** 
  3.      * @param string $table 數(shù)據(jù)庫表名 
  4.      * @param Entity $data  新增對象 
  5.      * 
  6.      * @return int 新增主鍵 
  7.      */ 
  8.     public static function insert(string $table, Entity $data) 
  9.     { 
  10.         $array = $data->toArray(); 
  11.         var_export($array); // test 
  12.  
  13.         $id = mt_rand(1, 1000); 
  14.         return $id; 
  15.     } 
  16.  
  17. class ArrayUtils 
  18.     /** 
  19.      * 針對成員都是私有屬性的對象 
  20.      * 
  21.      * @param      $obj 
  22.      * @param bool $removeNull 去掉空值 
  23.      * @param bool $camelCase 
  24.      * 
  25.      * @return array 
  26.      */ 
  27.     public static function Obj2Array($obj, $removeNull = true, $camelCase = true
  28.     { 
  29.         $reflect = new \ReflectionClass($obj); 
  30.         $props = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED); 
  31.  
  32.         $array = []; 
  33.         foreach ($props as $prop) { 
  34.             $prop->setAccessible(true); 
  35.             $key = $prop->getName(); 
  36.  
  37.             // 如果不是駝峰命名方式,就把對象里面的 createTime 轉(zhuǎn)成 create_time 
  38.             if (!$camelCase) { 
  39.                 $key = preg_replace_callback("/[A-Z]/"function ($matches) { 
  40.                     return "_" . strtolower($matches[0]); 
  41.                 }, $key); 
  42.                 $key = ltrim($key"_"); 
  43.             } 
  44.  
  45.             $value = $prop->getValue($obj); 
  46.  
  47.             if ($removeNull == true && $value === null) { 
  48.                 continue
  49.             } 
  50.  
  51.             if (is_object($value)) { 
  52.                 $value = self::Obj2Array($value); 
  53.             } 
  54.  
  55.             $array[$key] = $value; 
  56.         } 
  57.  
  58.         return $array; 
  59.     } 
  60.  
  61. class Entity 
  62.     public function toArray(){ 
  63.         return ArrayUtils::Obj2Array($this); 
  64.     } 
  65.  
  66. class ViewLogEntity extends Entity 
  67.     /** 
  68.      * @var int 
  69.      */ 
  70.     private $uid; 
  71.  
  72.     /** 
  73.      * @var string 
  74.      */ 
  75.     private $url; 
  76.  
  77.     /** 
  78.      * @var string 
  79.      */ 
  80.     private $referer; 
  81.  
  82.     /** 
  83.      * @var string 
  84.      */ 
  85.     private $createdTime; 
  86.  
  87.     /** 
  88.      * @param int $uid 
  89.      */ 
  90.     public function setUid(int $uid) 
  91.     { 
  92.         $this->uid = $uid; 
  93.     } 
  94.  
  95.     /** 
  96.      * @param string $url 
  97.      */ 
  98.     public function setUrl(string $url) 
  99.     { 
  100.         $this->url = $url; 
  101.     } 
  102.  
  103.     /** 
  104.      * @param string $referer 
  105.      */ 
  106.     public function setReferer(string $referer) 
  107.     { 
  108.         $this->referer = $referer; 
  109.     } 
  110.  
  111.     /** 
  112.      * @param string $createdTime 
  113.      */ 
  114.     public function setCreatedTime(string $createdTime) 
  115.     { 
  116.         $this->createdTime = $createdTime; 
  117.     } 
  118.  
  119.  
  120. class ViewLogStore 
  121.     private $table = "view_log"
  122.  
  123.     function record(ViewLogEntity $viewLogEntity) 
  124.     { 
  125.         Db::insert($this->table, $viewLogEntity); 
  126.     } 
  127.  
  128. // 測試 
  129.  
  130. $viewLogEntity = new ViewLogEntity(); 
  131. $viewLogEntity->setUid(1); 
  132. $viewLogEntity->setReferer("https://mengkang.net"); 
  133. $viewLogEntity->setUrl("https://segmentfault.com/l/1500000018225727"); 
  134. $viewLogEntity->setCreatedTime(date("Y-m-d H:i:s",time())); 
  135.  
  136. $viewLogStore = new ViewLogStore(); 
  137. $viewLogStore->record($viewLogEntity);  

案例3

這還是函數(shù)嗎?(不僅僅是語義,屬于錯誤) 

  1. /** 
  2.  * @method mixed fetchList(string $sql, array $argv); 
  3.  */ 
  4. class Model 
  5.  
  6.     public function __construct($table
  7.     { 
  8.  
  9.     } 
  10.  
  11. function getUserList($startId, $lastId, $limit = 100) 
  12.     if ($lastId > 0) { 
  13.         $startId = $lastId; 
  14.     } 
  15.  
  16.     $sql = "select * from `user` where id > ? order by id asc limit ?,?"
  17.  
  18.     $model = new Model('user'); 
  19.     return $model->fetchList($sql, [intval($startId), intval($limit)]); 
  20.  

$startId和$lastId兩個參數(shù)重復(fù)

案例4

盡量減少參數(shù)引用 

  1. function bad($input1, $input2, &$input3) 
  2.     //...logic 
  3.  
  4.     $input3 = "xxx"
  5.  
  6.     return true
  7.  

案例5

參數(shù)類型明確,返回值類型明確,不要出現(xiàn) mixed。這個我直接拿官方的函數(shù)來舉例,對權(quán)威也要有懷疑的眼光。純屬個人看法。 

 

 

 

案例6 

 

 

 

上面例子中你會發(fā)現(xiàn)這個addUser寫得不想一個函數(shù)(方法)而像一個遠程api接口。而且在右邊的代碼中需要每次使用的時候都要用is_array來判斷。這是非常不友好的語義表達。PHP Java 這樣的高級語言有異常,我們要善用異常。 

 

 

 

好的語義表達是團隊協(xié)作中高效迭代的潤滑劑,好的語義表達是線上未知代碼問題排查的指南針。這篇博客到這里就結(jié)束了,不知道你是否有一些收獲呢? 

 

 

責任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2017-04-14 10:37:21

2012-11-29 10:05:20

2016-02-25 11:42:19

2021-07-29 11:14:03

DevOpsLinux工程師

2009-02-10 15:39:59

軟件評測師軟考經(jīng)驗

2016-10-21 15:57:10

2016-06-27 10:40:12

軟件測試敏捷開發(fā)

2017-09-21 09:44:00

編程程序員軟件開發(fā)

2011-05-03 08:54:36

2020-06-29 14:54:19

網(wǎng)絡(luò)技術(shù)專家講座

2013-09-25 10:47:25

創(chuàng)新公司員工

2014-05-22 10:43:26

移動開發(fā)者優(yōu)秀

2014-12-23 09:40:41

CTO

2021-01-18 09:00:00

人工智能機器學習工程師

2016-01-28 11:18:09

卓越前端工程師

2013-01-07 09:41:48

2014-01-13 11:04:32

2021-05-25 09:51:42

架構(gòu)運維技術(shù)

2018-03-29 11:23:25

IT人員云計算工程師

2009-04-02 18:29:05

點贊
收藏

51CTO技術(shù)棧公眾號