什麼是優雅編程?什麼是優雅代碼?

來源:互聯網
上載者:User
個人理解:
所謂優雅編程就是喝著咖啡,摸著鍵盤,想著業務;
優雅代碼的定義是:
1、儘可能的少單雙引號;
2、不要嵌套;
3、命名全是最基本的英語單詞;
4、代碼像英文一樣,是一段一段的;
5、一個應用程式碼群組織調理清晰,最好是樹關係,網關係少用。

下邊是一個sql查詢的連貫操作實現方法,請大家指導指導!
  1. /**
  2. * DB連貫操作、sql條件構造
  3. * From: EQPHP FrameWork
  4. * Author: art_youth
  5. * E-mail: 258122391@qq.com
  6. * Pub data: 2012-11-09
  7. */
  8. class query{
  9. public $sql='';
  10. public $option=array();
  11. static $keyword=array('select','from','where','group','having','order','limit');
  12. //初始化查詢參數
  13. function __construct($table,$prefix=''){
  14. $this->option['from']=$prefix.$table;
  15. }
  16. //構造參數
  17. function __call($method,$param){
  18. if (in_array($method,self::$keyword)) {
  19. $this->option[$method]=$param[0];
  20. return $this;
  21. }
  22. }
  23. //輸出查詢結果
  24. function out($mode='sql',$rs_count=0,$now_page=1,$page_size=20){
  25. $this->sql='';
  26. foreach (self::$keyword as $key) {
  27. $value=($key == 'group' || $key == 'order') ? $key.' by' : $key;
  28. if ($key === 'where' && is_array($this->option['where'])) {
  29. $this->option['where']=self::condition($this->option['where']);
  30. }
  31. if (isset($this->option[$key]) && trim($this->option[$key])) {
  32. $this->sql.=' '.$value.' '.trim($this->option[$key]);
  33. }
  34. unset($this->option[$key]);
  35. }
  36. $this->sql=trim($this->sql);
  37. switch($mode){
  38. case 'rs':
  39. return db::rs($this->sql);
  40. case 'list':
  41. return db::rs_list($this->sql);
  42. case 'page':
  43. return db::page_list($this->sql,$rs_count,$now_page,$page_size);
  44. default:
  45. return $this->sql;
  46. }
  47. }
  48. //構造sql查詢條件
  49. static function condition($data){
  50. //處理邏輯串連符
  51. $logic=' and ';
  52. if (isset($data['logic'])) {
  53. $logic=' '.$data['logic'].' ';
  54. unset($data['logic']);
  55. }
  56. //處理字串(本生sql)
  57. if (isset($data['query'])) {
  58. $condition[]='('.$data['query'].')';
  59. unset($data['query']);
  60. }
  61. //處理條件資料
  62. foreach ($data as $key=>$value) {
  63. $condition[]='('.self::parse_expression($key,$value).')';
  64. }
  65. return implode($logic,$condition);
  66. }
  67. //解析運算式
  68. private static function parse_expression($key,$value){
  69. if (is_numeric($value)) return $key.'='.$value;
  70. if (is_string($value)) return $key.'="'.$value.'"';
  71. if (is_array($value)) {
  72. //基本條件查詢
  73. if (preg_match('/^(eq|neq|gt|egt|lt|elt)$/i',$value[0])) {
  74. is_string($value[1]) && $value[1]='"'.$value[1].'"';
  75. $operator=array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=',);
  76. return $key.$operator[$value[0]].$value[1];
  77. }
  78. //in範圍尋找
  79. if (in_array($value[0],array('in','not in'))) {
  80. is_array($value[1]) && $value[1]=implode(',',$value[1]);
  81. return $key.' '.$value[0].'('.$value[1].')';
  82. }
  83. //between區間尋找
  84. if (in_array($value[0],array('between','not between'))) {
  85. $param=is_string($value[1]) ? explode(',',$value[1]) : $value[1];
  86. return $key.' '.$value[0].' '.$param[0].' and '.$param[1];
  87. }
  88. //like模糊比對
  89. if (in_array($value[0],array('like','not like'))) {
  90. if (is_array($value[1])) {
  91. $buffer=array();
  92. foreach ($value[1] as $param) {
  93. $buffer[]=$key.' '.$value[0].' "'.$param.'"';
  94. }
  95. $logic=isset($value[2]) ? ' '.$value[2].' ' : ' or ';
  96. return implode($logic,$buffer);
  97. }
  98. if (strpos($key,'|') !== false) {
  99. $buffer=array();
  100. foreach (explode('|',$key) as $field) {
  101. $buffer[]='('.$field.' '.$value[0].' "'.$value[1].'")';
  102. }
  103. return implode(' or ',$buffer);
  104. }
  105. if (strpos($key,'&') !== false) {
  106. $buffer=array();
  107. foreach (explode('&',$key) as $field) {
  108. $buffer[]='('.$field.' '.$value[0].' "'.$value[1].'")';
  109. }
  110. return implode(' and ',$buffer);
  111. }
  112. return $key.' '.$value[0].' "'.$value[1].'"';
  113. }
  114. //數學區間查詢(1,9)/[2,3)
  115. if ($value[0] === 'extent') {
  116. $logic=isset($value[2]) ? ' '.$value[2].' ' : ' && ';
  117. $operator=array('('=>'>','['=>'>=',')'=>'<',']'=>'<=');
  118. preg_match('/^(\(|\[)(.*),(.*)(\)|\])$/',$value[1],$param);
  119. $result='';
  120. isset($param[2]) && $result.=$key.$operator[$param[1]].$param[2];
  121. isset($param[4]) && $result.=$logic.$key.$operator[$param[4]].$param[3];
  122. return $result;
  123. }
  124. return '';
  125. }
  126. }
  127. //資源回收
  128. function __destruct(){
  129. unset($this->option,$this->sql);
  130. }
  131. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.