{"id":83,"date":"2014-03-23T16:19:55","date_gmt":"2014-03-23T08:19:55","guid":{"rendered":"http:\/\/blog.turn.tw\/?p=83"},"modified":"2016-07-13T14:15:31","modified_gmt":"2016-07-13T06:15:31","slug":"%e5%a6%82%e4%bd%95%e5%af%ab%e5%a5%bdcode%ef%bc%9a%e7%94%a8guard-clauses%e8%88%87eafp%e6%89%93%e9%80%a0%e4%bd%a0%e7%9a%84%e7%a9%a9%e5%9b%ba%e5%9f%8e%e5%a0%a1%e3%80%82","status":"publish","type":"post","link":"https:\/\/blog.turn.tw\/?p=83","title":{"rendered":"\u5982\u4f55\u5beb\u597dcode\uff1a\u7528Guard Clauses\u8207EAFP\u6253\u9020\u4f60\u7684\u57ce\u5821\u3002"},"content":{"rendered":"<p>\u4ee5\u524d\u5bebproject\u7684\u6642\u5019\uff0c\u5e38\u5e38\u8d8a\u5beb\u8d8a\u4e0d\u5b89\u3002<\/p>\n<p>\u5fc3\u88e1\u982d\u7e3d\u662f\u6709\u5169\u500b\u8072\u97f3\uff1a<\/p>\n<ol>\n<li>\u70ba\u4e86\u66f4\u80fd\u61c9\u4ed8\u4e0d\u540c\u72c0\u6cc1\uff0c\u7a0b\u5f0f\u78bc\u8981\u8655\u7406\u7684\u60c5\u6cc1\u8d8a\u4f86\u8d8a\u591a\u3001\u8d8a\u4f86\u8d8a\u8907\u96dc\u3002if\/else\u8ddfswitch\/case\u8d8a\u5beb\u8d8a\u591a\u5c64\u3001\u4e00\u5c64\u53c8\u5728\u4e00\u5c64\u53c8\u5728\u4e00\u5c64\u88e1\u9762\uff0c\u8d8a\u5beb\u8d8a\u50cf\u5728\u6311\u6230\u81ea\u5df1\u667a\u5546\u7684\u6975\u9650\u3002<\/li>\n<li>\u9019\u500bproject\u518d\u7e7c\u7e8c\u7dad\u8b77\uff08\u64f4\u5145\u529f\u80fd\u3001\u4feebug\uff09\u4e09\u500b\u6708\u6c92\u554f\u984c\u3001\u534a\u5e74\u9084\u6490\u5f97\u4f4f\uff0c\u4e4b\u5f8c\u4e00\u5b9a\u8d8a\u5beb\u8d8a\u6162\u3002\u4e0d\u5230\u4e00\u5e74\uff0c\u7a0b\u5f0f\u78bc\u5c07\u9f90\u96dc\u5230\u96e3\u4ee5\u633d\u6551\u3002<\/li>\n<\/ol>\n<p>\u7e3d\u89ba\u5f97\u54ea\u88e1\u51fa\u4e86\u554f\u984c\uff1a\u5982\u679c\u4e4b\u5f8c\u5beb\u5546\u7528\u8edf\u9ad4\u3001\u6709\u6301\u7e8c\u64f4\u5145\u7684\u91ce\u5fc3\uff0c\u9019\u6a23\u5bebcode\u4e0b\u53bb\u4e00\u5b9a\u6703\u9047\u5230\u5927\u9ebb\u7169\u3002\u83ab\u975e\u8981\u8ddf\u5ba2\u6236\u6216\u662f\u8001\u95c6\u8aaa\uff1a\u9019project\u6211\u7121\u6cd5\u7e7c\u7e8c\u7dad\u8b77\u4e86\uff0c\u56e0\u70ba\u6211\u80fd\u529b\u4e0d\u5920\u3001\u60a8\u53ef\u80fd\u8981\u53e6\u8acb\u9ad8\u660e\u3002<\/p>\n<p>\u96e3\u9053\u6211\u771f\u7684\u667a\u5546\u4e0d\u8db3\uff0c\u6c92\u80fd\u529b\u5beb\u51fa\u898f\u6a21\u6bd4\u8f03\u5927\u7684\u8edf\u9ad4\uff1f<\/p>\n<p>\u7d42\u65bc\uff0c\u6709\u4e00\u5929\uff0c\u6211\u5b78\u5230\u4e86\u5169\u62db\uff0c\u5f9e\u6b64\u4e0d\u518d\u6709\u9019\u7a2e\u64d4\u6182\u3002<\/p>\n<p>\u4eca\u5929\uff0c\u5c31\u4f86\u8ddf\u5404\u4f4d\u5206\u4eab\u6211\u7684\u7955\u5bc6\u3002<\/p>\n<p>\uff08\u5929\u963f\uff0c\u53e3\u6c23\u771f\u8a87\u5f35\uff0c\u597d\u50cf\u8981\u63a8\u92b7\u7522\u54c1\u9084\u662f\u8ce3\u4fdd\u96aa\uff0c\u54c8\uff09<\/p>\n<p>\u9019\u5169\u62db\u662fGuard Clauses\u8207EAFP\u539f\u5247\uff08It&#8217;s Easier to Ask Forgiveness than Permission\uff09\u3002\u4e0b\u9762\u8ddf\u5404\u4f4d\u5206\u4eab\uff0c\u6709\u5beb\u932f\u7684\u90e8\u4efd\u4e5f\u671f\u5f85\u5404\u4f4d\u524d\u8f29\u6307\u6559<\/p>\n<h1>Guard Clauses<\/h1>\n<p>\u8207Guard Clauses\u76f8\u5c0d\u7684\u662fNested Structure\u3002<\/p>\n<p>\u4e0b\u9762\u9019\u7a2ecode\u662f\u5178\u578b\u7684Nested Structure\uff0c\u76f8\u4fe1\u4e5f\u66fe\u51fa\u73fe\u5728\u4f60\u7684project\u3002\u707d\u96e3\u4e00\u5834\u3002<\/p>\n<blockquote>\n<pre>function the_method($some_variable){\r\n    if (some_expression){\r\n        \/\/ some code\r\n        \/\/ ...\r\n        if (another_expression){\r\n            \/\/ some more code\r\n            \/\/ ...\r\n            if (another_expression){\r\n                \/\/ \u9019\u88e1\u662f\u7406\u60f3\u60c5\u6cc1\r\n                \/\/ \u7d42\u65bc\u53ef\u4ee5\u5728\u9019\u88e1\u505a\u60f3\u505a\u7684\u4e8b\u4e86\r\n                \/\/ \u4e0d\u904e\u6211\u76f8\u4fe1\u9019\u4e32code\u904e\u4e86\u4e00\u500b\u6708\u9023\u4f60\u81ea\u5df1\u90fd\u770b\u4e0d\u61c2\r\n            }\r\n        }\r\n        \/\/ ... maybe some 'else' block to handle here\r\n    }\r\n    \/\/ ... and here\r\n}<\/pre>\n<\/blockquote>\n<p>\u9047\u5230\u9019\u7a2e\u60c5\u6cc1\uff0c\u8981\u600e\u9ebc\u61c9\u7528guard clauses\uff1f\u8acb\u770b\u4e0b\u9762\u7684\u7bc4\u4f8b\u3002<\/p>\n<p>\u4ee5\u96fb\u5b50\u5546\u52d9\u7db2\u7ad9\u70ba\u4f8b\uff0c\u4e0b\u9762\u662f\u67d0class\u5efa\u7acb\u4e00\u5f35\u8cfc\u7269\u8a02\u55ae\u7684method\u3002\u8981\u8f38\u5165\u55ae\u54c1\u7684id\u9663\u5217\u3001\u8cb7\u5bb6id\u3001\u4ed8\u6b3e\u65b9\u5f0f\u8207\u6298\u6263\u91d1\u984d\uff1a<\/p>\n<blockquote>\n<pre>function create_one($ids, $user_id, $payment_method, \r\n  $deduction_amount)\r\n{\r\n    \/\/ \u62b5\u6263\u91d1\u984d\u4e0d\u662f\u6574\u6578\r\n    \/\/ \u547c\u53eb\u9019\u500bmethod\u7684\u5de5\u7a0b\u5e2b\u4e00\u5b9a\u54ea\u908a\u641e\u932f\u4e86\u5427\r\n    if ( !is_integer( $deduction_amount) ){\r\n        throw new Exception(\"invalid 'deduction_amount': $deduction_amount. it's not integer.\");\r\n    }\r\n\r\n    \/\/ \u4e4b\u524d\u6c7a\u5b9a\u8cc7\u6599\u5eab\u5167\u7528\u8ca0\u6578\u4ee3\u8868\u6298\u6263\u91d1\u984d\r\n    \/\/ \u50b3\u6b63\u6578\u9032\u4f86\u7684\u5de5\u7a0b\u5e2b\u4f60\u6628\u5929\u5f88\u665a\u7761\u543c\r\n    if ($deduction_amount > 0){\r\n        throw new Exception(\"invalid deduction amount: $deduction_amount. it can't be position\");\r\n    }\r\n\r\n    \/\/ \u8cc7\u6599\u5eab\u5167\u627e\u4e0d\u5230\u9019\u500b$user_id\r\n    \/\/ \u547c\u53eb\u9019\u500bmethod\u7684\u5de5\u7a0b\u5e2b\u4f60\u8981\u4e0d\u8981\u518d\u6aa2\u67e5\u4e00\u4e0b\u7a0b\u5f0f\u78bc\r\n    $query = $this->db\r\n                               ->where('id',$user_id)\r\n                               ->get('users');\r\n    if ( $query->num_rows() == 0 ){\r\n        throw new Exception(\"invalid user id: $user_id. the user doesn't exist\");\r\n    }\r\n\r\n    \/\/ \u516c\u53f8\u53ea\u6709\u63a5\u53d7\u4e09\u7a2e\u4ed8\u6b3e\u65b9\u5f0f\r\n    $acceptable_payment_method = array( 'ATM', 'at_home', 'store');\r\n    if ( !in_array( $payment_method, $acceptable_payment_method ) )\r\n    {\r\n        throw new Exception(\"invalid payment_method: $payment_method .\");\r\n    }\r\n\r\n    \/\/ \u7e7c\u7e8c\u4e00\u4e32\u8981\u6aa2\u67e5\u7684\u9805\u76ee\r\n\r\n    \/\/ \u9019\u908a\u7d42\u65bc\u53ef\u4ee5\u628a\u8cc7\u6599\u6574\u7406\u4e00\u4e0b\u7136\u5f8c\u5b58\u9032\u8cc7\u6599\u5eab\u4e86\r\n    \/\/ \u53ef\u80fd\u9084\u662f\u6703\u5728\u9019\u88e1\u5674\u51fa\u4e0d\u77e5\u9053\u4ec0\u9ebc\u9b3c\u7684exception\r\n    \/\/ \u4f46\u6574\u6bb5code\u771f\u7684\u597d\u8b80\u3001\u597d\u7dad\u8b77\u3001\u53c8robust\u5f88\u591a\u4e86\r\n\r\n    return true;\r\n\r\n}\/\/ end function create_one<\/pre>\n<\/blockquote>\n<p>\u5f8c\u8005\u662f\u4e0d\u662f\u6e05\u695a\u975e\u5e38\u591a\uff1f<\/p>\n<h1>EAFP\u539f\u5247\uff08It&#8217;s Easier to Ask Forgiveness than Permission\uff09<\/h1>\n<p>*\u8a3b1<\/p>\n<p>\u8207EAFP\u76f8\u5c0d\u7684\u662fLBYL\u539f\u5247\uff08Look Before You Leap\uff09<\/p>\n<p>\u60f3\u50cf\u4e0b\u9762\u9019\u500b\u7528LBYL\u5beb\u7684\u9664\u6cd5\u51fd\u5f0f\uff1a<\/p>\n<blockquote>\n<pre>function safe_divide_1($x, $y){\r\n    if ($y == 0){\r\n        echo \"\u5206\u6bcd\u70ba\u96f6\";\r\n        return null;\r\n    }\r\n    else{\r\n        return $x\/$y;\r\n    }\r\n}<\/pre>\n<\/blockquote>\n<p>\u63a5\u8457\u662f\u7528EAFP\u5beb\u7684\u9664\u6cd5\u51fd\u5f0f\uff1a<\/p>\n<blockquote>\n<pre>function safe_divide_2($x, $y){\r\n    try{\r\n        return x\/y\r\n    }\r\n    \/\/ \u5982\u679c\u4f60\u6709\u5b9a\u7fa9\u9019\u6a23\u4e00\u500bZeroDivisionException\u985e\u5225\u7684\u8a71\u624d\u884c\r\n    catch (ZeroDivisionException $e){ \r\n        echo \"\u5206\u6bcd\u70ba\u96f6\";\r\n        return null; \/\/ \u6216\u662f\u518dthrow $e\u7d66\u4e0a\u9762\u7684stack\u8655\u7406\u4e5f\u884c\r\n    }\r\n     \/\/ \u5c31\u7b97\u4f60\u4e0a\u9762\u6c92\u5b9a\u7fa9ZeroDivisionException\r\n    \/\/ \u9019\u908a\u9084\u662f\u6703\u6293\u5f97\u5230\u932f\u8aa4\u7136\u5f8c\u986f\u793a\u8a0a\u606f\u5566\r\n    catch (Exception $e){ \r\n        echo ($e->getMessage());\r\n        return null;\r\n    }\r\n}<\/pre>\n<\/blockquote>\n<p>\u5f8c\u8005\u6709\u6c92\u6709\u6bd4\u8f03\u4e7e\u6de8\u4fd0\u843d\uff1f<\/p>\n<p>\uff08\u597d\u5427\uff0c\u9019\u908asample code\u770b\u8d77\u4f86\u597d\u50cf\u6c92\u6709\uff0c\u54c8\u54c8\uff09<\/p>\n<p>LBYL\u4ee3\u8868\u4f60\u5c0f\u5fc3\u8655\u7406\u5404\u7a2e\u60c5\u6cc1\uff1bEAFP\u4ee3\u8868\u4f60\u76f4\u63a5\u57f7\u884c\u6700\u91cd\u8981\u7684\u7a0b\u5f0f\u78bc\uff0c\u7136\u5f8c\u78b0\u5230\u554f\u984c\uff08\u51fa\u73feexception\uff09\u518d\u9762\u5c0d\u3002<\/p>\n<p>\u5728MVC\u00a0web\u958b\u767c\u9818\u57df\uff0c\u901a\u5e38\u5c31\u662fcontroller\u5bebtry\/catch\u53bb\u57f7\u884cmodel\u7684\u67d0\u4e9bmethod\uff08EAFP\uff09\uff0c\u800c\u4e0d\u8981\u5728controller\u5167\u81ea\u5df1\u5beb\u4e00\u5927\u5806\u5224\u65b7\u5f0f\u6aa2\u67e5\uff08LBYL\uff09\u3002<br \/>\n\u5c0d\u61c9\u5230\u525b\u525b\u96fb\u5b50\u5546\u52d9\u8a02\u55ae\u7684\u4f8b\u5b50\uff0c\u67d0\u500b\u6210\u7acb\u8a02\u55ae\u7684controller\u6839\u64daEAFP\u6703\u5beb\u51fa\u9019\u6a23\u7684code\uff1a<\/p>\n<blockquote>\n<pre>try{\r\n    $this->Order_model->create_one($ids, $user_id, $payment_method, $deduction_amount);\r\n} catch (Exception $e){\r\n    \/\/ \u9019\u6a23\u5beb\u5176\u5be6\u5f88\u6df7\uff0c\u4f7f\u7528\u8005\u6703\u770b\u5230\u4e00\u500b\u5f88\u919c\u7684error\u9801\u9762\r\n    \/\/ \u4f46\u662fwork\u5c31\u662f\u4e86\r\n    exit($e->getMessage());\r\n}\r\n\/\/ some code ...\r\n\/\/ \u986f\u793a\u606d\u559c\u4f7f\u7528\u8005\u7684\u9801\u9762\/\u8acb\u4ed6\u62ff\u9322\u5305\u6e96\u5099\u4ed8\u6b3e\u7684\u9801\u9762\u4e4b\u985e\u7684<\/pre>\n<\/blockquote>\n<p>\u82e5\u662f\u6839\u64daLBYL\u539f\u5247\u53bb\u5bebcontroller\uff0c\u5247\u6703\u5beb\u51fa\u4e00\u5927\u4e32if\/else if\/else\u53bb\u6aa2\u67e5$ids, $user_id, $payment_method, $deduction_amount\u9019\u5e7e\u500b\u8b8a\u6578\u3001\u5c0f\u5fc3\u8655\u7406\u5b8c\u5404\u7a2e\u60c5\u6cc1\u624d\u53bb\u547c\u53ebcreate_one\u9019\u500bmethod\u3002\u7a0b\u5f0f\u78bc\u6703\u53c8\u9577\u53c8\u919c\uff0c\u770b\u4e0d\u61c2\u6574\u6bb5code\u91cd\u9ede\u5728\u54ea\u3002<\/p>\n<p>\u4ee5\u4e0a\u662f\u500b\u4eba\u5b78\u7fd2\u5bebcode\u5b78\u5230\u6700\u91cd\u8981\u7684\u5176\u4e2d\u5169\u62db\u3002\u5176\u4e2d\u6709\u5beb\u932f\u7684\u90e8\u4efd\u4e5f\u671f\u5f85\u5404\u4f4d\u524d\u8f29\u6307\u6559\u3002<\/p>\n<h1>Q&amp;A<\/h1>\n<p><em>Q1: guard clauses\u770b\u8d77\u4f86\u8d85\u76f4\u89ba\u7684\u554a\uff0c\u70ba\u4ec0\u9ebc\u6211\u5230\u4eca\u5929\u624d\u77e5\u9053\u53ef\u4ee5\u9019\u6a23\u5bebcode\uff1f<\/em><\/p>\n<p>\u9019\u8ddf\u5b78\u6821\u6559\u80b2\u6709\u95dc\u3002\u6b63\u7d71\u6559\u80b2\u6709\u500b\u540d\u8a5e\u53eb\u505asingle entry\/exit point\uff0c\u8aaa\u662f\u70ba\u4e86\u7dad\u6301code\u7684\u9ad8\u54c1\u8cea\uff0c\u53eb\u4f60\u61c9\u8a72\u9019\u9ebc\u505a\u3002<\/p>\n<p><em>Q2: \u6709\u6642\u5019\u6211\u9084\u662f\u89ba\u5f97\u5beb\u4e00\u4e32if\/else\u6bd4guard clause\u76f4\u89ba\u554a\uff1f<\/em><\/p>\n<p>\u7a0b\u5f0f\u78bc\u7684\u5916\u89c0\u5176\u5be6\u76f4\u63a5\u8aaa\u660e\u4e86\u67d0\u4e9b\u4e8b\u60c5\u3002guard clauses\u7684code\u770b\u8d77\u4f86\u5c31\u50cf\u5728\u8aaa\uff1a\u5982\u679c\u767c\u751f\u9019\u4ef6\u4e8b\uff0c\u8d95\u5feb\u8655\u7406\u5b8c\u3001\u7136\u5f8c\u6efe\u5427\u3002\u4e00\u4e32if\/else if\/else\u50cf\u662f\u5728\u8aaa\uff1a\u4f60\u770b\uff0c\u5404\u500b\u60c5\u6cc1\u90fd\u4e00\u6a23\u91cd\u8981\u3001\u6240\u4ee5\u5927\u5bb6\u90fd\u5728\u4e00\u6a23\u6df1\u5c64\u7684\u5de2\u72c0\u7d50\u69cb\u88e1\u9762\uff0c\u771f\u6b63\u91cd\u8981\u7684code\u8ddf\u67d0\u4e9b\u72d7\u5c41\u60c5\u6cc1\u4e00\u6a23\u91cd\u8981\uff0c\u6240\u4ee5\u4ed6\u5011\u4e00\u6a23\u5728\u9019\u6050\u6016\u7a0b\u5f0f\u78bc\u7684\u5167\u90e8\u7b2cN\u5c64\uff01<\/p>\n<p>\u5230\u5e95\u8a72\u600e\u9ebc\u5beb\u9084\u662f\u770b\u5be6\u969b\u60c5\u6cc1\u3002\u4e0d\u59a8\u90fd\u8a66\u8a66\uff0c\u770b\u7576\u4e0b\u54ea\u500b\u9069\u5408\uff1f<\/p>\n<p><em>Q3: \u6211\u5c31\u8001\u5be6\u544a\u8a34\u4f60\u5427\uff01\u6211\u6839\u672c\u4e0d\u60f3\u5b78exception\u6216\u662ftry\/catch\u662f\u4ec0\u9ebc\uff01\u56e0\u70ba\u6211\u7684code\u7528if\/else\u6216\u662fswitch\/case\/default\u5c31\u628a\u5168\u90e8\u60c5\u6cc1\u90fd\u8655\u7406\u597d\u4e86\uff01<\/em><\/p>\n<p>\u6703\u9019\u6a23\u60f3\u662f\u56e0\u70ba\u4f60\u4ee5\u524d\u5b78C\u8a9e\u8a00\u5c0d\u55ce\uff1fC\u8a9e\u8a00\u6c92\u6709exception\u6a5f\u5236\uff0c\u5de5\u7a0b\u5e2b\u5fc5\u9808\u5beb\u51fa\u5b8c\u6574\u8655\u7406\u5404\u7a2e\u60c5\u6cc1\u7684\u7a0b\u5f0f\u78bc\u3002\u7562\u7adfLBYL\uff08Look Before You Leap \uff09\u4e5f\u662f\u4e00\u7a2e\u64b0\u5beb\u98a8\u683c\u3002\u90a3\u5c31\u5beb\u5230\u4f60\u89ba\u5f97\u75db\u82e6\u7684\u90a3\u5929\u518d\u56de\u4f86\u5b78\u9019\u5e7e\u62db\u5427\u3002<br \/>\n\u4e1fexception\u4e0d\u4f46code\u8f03\u6613\u8b80\uff0c\u9084\u80fd\u5728\u539f\u672cfunction\u4e4b\u5916\u53bb\u8655\u7406\u76f8\u95dc\u932f\u8aa4\uff08\u628a\u554f\u984c\u4e1f\u7d66\u300c\u5176\u4ed6\u5c64\u7d1a\u300d\u53bb\u8655\u7406\uff0c\u50cf\u662fmodel\u4e1f\u7d66\u5176\u4ed6model\u3001model\u518d\u4e1f\u7d66controller\u4e4b\u985e\u7684\uff09\u3001\u8ffd\u8e64exception\u5728\u5404stack\u5167\u7684\u6d41\u52d5\u3002<\/p>\n<p><em>Q4: \u597d\u5427\uff0c\u6211\u525b\u662f\u9a19\u4f60\u7684\u3002\u5176\u5be6\u6211\u5c0d\u6b63\u5728\u7528\u7684\u8a9e\u8a00\u7684exception\u6a5f\u5236\u4e0d\u719f\u3002\u6211\u9084\u80fd\u7528guard clauses\u55ce\uff1f<\/em><\/p>\n<p>\u53ef\u4ee5\uff01\u4e0a\u9762sample code\u7684throw new Exception\u5168\u90e8\u5beb\u6210return false\u5373\u53ef\u3002\u7136\u5f8cEAFP\u5c31\u4e0d\u6703\u5beb\u6210try\/catch\u4e86\u3002<\/p>\n<p>\u4ee5\u540c\u4e00\u500b\u5efa\u7acb\u8a02\u55ae\u7684\u4f8b\u5b50\u4f86\u8aaa\uff0c\u5982\u679c\u4f60\u89ba\u5f97\u6210\u529f\u8ddf\u5931\u6557\u90fd\u662f\u4eba\u4e4b\u5e38\u60c5\uff0c\u8acb\u5beb\u6210\u9019\u6a23\uff1a<\/p>\n<blockquote>\n<pre>$success =$this->Order_model->create_one($ids, $user_id, $payment_method, $deduction_amount);\r\nif ($success){\r\n    \/\/ some code ...\r\n    \/\/ \u986f\u793a\u606d\u559c\u4f7f\u7528\u8005\u7684\u9801\u9762\/\u8acb\u4ed6\u62ff\u9322\u5305\u6e96\u5099\u4ed8\u6b3e\u7684\u9801\u9762\u4e4b\u985e\u7684\r\n}\r\nelse{\r\n    echo \"\u89aa\u611b\u7684\u4f7f\u7528\u8005\uff0c\u56e0\u70ba\u6211\u9084\u6c92\u5b78exception\u6240\u4ee5\u6211\u4e0d\u80fd\u544a\u8a34\u4f60\u54ea\u908a\u51fa\u932f\uff0c\u53cd\u6b63\u51fa\u932f\u4e86\u3002\"\r\n}<\/pre>\n<\/blockquote>\n<p>\u5982\u679c\u4f60\u89ba\u5f97\u5728\u4f60\u7cfb\u7d71\u5167\u61c9\u8a72\u4e0d\u592a\u6703\u5931\u6557\u624d\u5c0d\uff0c\u8acb\u5beb\u6210\u9019\u6a23\uff1a<\/p>\n<blockquote>\n<pre>$success =$this->Order_model->create_one($ids, $user_id, $payment_method, $deduction_amount);\r\nif ( !$success){\r\n    echo \"\u89aa\u611b\u7684\u4f7f\u7528\u8005\uff0c\u56e0\u70ba\u6211\u9084\u6c92\u5b78exception\u6240\u4ee5\u6211\u4e0d\u80fd\u544a\u8a34\u4f60\u54ea\u908a\u51fa\u932f\uff0c\u53cd\u6b63\u51fa\u932f\u4e86\u3002\"\r\n}\r\n\/\/ some code ...\r\n\/\/ \u986f\u793a\u606d\u559c\u4f7f\u7528\u8005\u7684\u9801\u9762\/\u8acb\u4ed6\u62ff\u9322\u5305\u6e96\u5099\u4ed8\u6b3e\u7684\u9801\u9762\u4e4b\u985e\u7684<\/pre>\n<\/blockquote>\n<p><em>Q5: \u4f60Q2\u3001Q3\u597d\u50cf\u5728\u8aaa\u6709\u6642\u5019\u7684\u78ba\u9700\u8981\u5beb\u4e00\u4e32\u919c\u919cif\/else if\uff1f\u53ef\u4ee5\u518d\u505a\u4e00\u9ede\u8aaa\u660e\uff1f<\/em><\/p>\n<p>if\/else if \u53ef\u4ee5\u60f3\u6210\u5728\u8655\u7406\u300c\u5408\u7406\u7684\u5404\u7a2e\u60c5\u6cc1\u300d\u3002\u5b83\u5011\u90fd\u4e00\u6a23\u91cd\u8981\uff0c\u6240\u4ee5code\u5728\u540c\u5c64\u7d1a\u5404\u4f54\u4e86\u4e00\u6574\u4e32\u3002<br \/>\nthrow exception\u5247\u662f\u5728\u8655\u7406\u300c\u4e0d\u5408\u7406\u4f46\u78ba\u5be6\u53ef\u80fd\u9762\u5c0d\u7684\u60c5\u6cc1\u300d\u3002\u9019\u7a2e\u60c5\u6cc1\u96a8\u4fbf\u4e1f\u500bexception\u9084\u662f\u79c0error\u4e4b\u985e\u7684\uff0c\u53eb\u5b83\u5feb\u6efe\u5c31\u597d\u4e86\u3002<br \/>\n\u5982\u679c\u4f60\u5f88\u5728\u610fuser experience\uff0c\u90a3\u5c31\u53bb\u5ba2\u88fd\u5316\uff08\u5ba3\u544aclass\u53bb\u7e7c\u627f\u539f\u751fexception\uff09\u5404\u500bexception\u3001\u5e6b\u52a9\u5176\u4ed6developer\uff08\u901a\u5e38\u5c31\u662f\u4f60\u81ea\u5df1\uff09\u5728\u5404\u7a2eexception\u4e4b\u4e0b\u8655\u7406\u5404\u7a2e\u60c5\u6cc1\u5427\u3002<br \/>\n\u4e0d\u904e\uff0c\u5728Web\u958b\u767c\u9818\u57df\uff0c\u5674exception\u5e38\u662f\u5728controller\u547c\u53ebmodel\u51fd\u5f0f\u4e4b\u6642\u767c\u751f\u3002\u6211\u901a\u5e38\u76f4\u63a5\u5728view\u5167\u7528JavaScript\u63d0\u793a\u4f7f\u7528\u8005\u3002\u4ed6\u82e5\u662f\u9032\u884c\u4ec0\u9ebc\u8a6d\u7570\u64cd\u4f5c\u8df3\u904e\u4e86\u6211\u7684JavaScript\u5354\u52a9\uff0c\u90a3\u6211\u5c31\u7528\u700f\u89bd\u5668\u5927\u65b9\u7684\u5674\u500b\u8d85\u919c\u7684error message\u9801\u9762\u7d66\u4ed6\u3002<\/p>\n<p><em>Q6: \u70ba\u4ec0\u9ebcQ4\u4f60\u8981\u793a\u7bc4\u5169\u7a2e\u5beb\u6cd5\uff1f\u4f60\u8b93\u6211\u597d\u56f0\u64fe\u3001\u6211\u5230\u5e95\u8981\u6311\u54ea\u7a2e\uff1f<\/em><\/p>\n<p>\u628aQ2\u518d\u770b\u4e00\u904d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>\u8a3b1\uff1aEAFP\u8207LBYL\u9019\u5169\u500b\u8a5e\u6211\u662f\u5728Python\u793e\u7fa4\u770b\u5230\u7684\u3002\u9019\u7bc7\u7528\u5b57\u9063\u8a5e\u53ef\u80fd\u4e0d\u751a\u7cbe\u78ba\u3002<\/p>\n<p>\u8a3b2\uff1a\u6587\u7ae0\u88e1\u5efa\u8b70\u7684\u67d0\u4e9b\u8655\u7406\u65b9\u5f0f\u975e\u5e38\u96a8\u4fbf\u3002\u4ee5\u4e0a\u50c5\u662f\u6211\u76ee\u524d\u7684\u7406\u89e3\u3002\u8acb\u4e0d\u65b7\u8ffd\u5c0b\u81ea\u5df1\u6700\u559c\u6b61\u7684best practice\u3002<\/p>\n<p>(Photo via\u00a0<a href=\"https:\/\/www.flickr.com\/photos\/elescir\/\">Elescir<\/a>, CC License)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u5bebproject\u7684\u6642\u5019\uff0c\u5e38\u5e38\u8d8a\u5beb\u8d8a\u4e0d\u5b89\u3002 \u5fc3\u88e1\u982d\u7e3d\u662f\u6709\u5169\u500b\u8072\u97f3\uff1a \u70ba\u4e86\u66f4\u80fd\u61c9\u4ed8\u4e0d\u540c\u72c0\u6cc1\uff0c\u7a0b\u5f0f\u78bc\u8981\u8655\u7406\u7684\u60c5\u6cc1 &hellip; <a href=\"https:\/\/blog.turn.tw\/?p=83\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">\u5982\u4f55\u5beb\u597dcode\uff1a\u7528Guard Clauses\u8207EAFP\u6253\u9020\u4f60\u7684\u57ce\u5821\u3002<\/span> <span class=\"meta-nav\">&rarr;<\/span> <\/a><\/p>\n","protected":false},"author":1,"featured_media":287,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/posts\/83"}],"collection":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=83"}],"version-history":[{"count":63,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions"}],"predecessor-version":[{"id":3321,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/posts\/83\/revisions\/3321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/media\/287"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}