Jan 01, 1970
大多数这些气味只是暗示可能有问题。它们本身不需要固定……(不过你应该研究一下。)
不要添加 IF 检查生产环境。
TL;DR:以防获取与种植相关内容的能力
def send_welcome_email(email_address, environment): if ENVIRONMENT_NAME == "production": print(f"Sending welcome email to {email_address} from Bob Builder <[email protected]>") else: print("Emails are sent only on production") send_welcome_email("[email protected]", "development") # Emails are sent only on production send_welcome_email("[email protected]", "production") # Sending welcome email to [email protected] from Bob Builder <[email protected]>
class ProductionEnvironment: FROM_EMAIL = "Bob Builder <[email protected]>" class DevelopmentEnvironment: FROM_EMAIL = "Bob Builder Development <[email protected]>" # We can unit test environments # and even implement different sending mechanisms def send_welcome_email(email_address, environment): print(f"Sending welcome email to {email_address} from {environment.FROM_EMAIL}") # We can delegate into a fake sender (and possible logger) # and unit test it send_welcome_email("[email protected]", DevelopmentEnvironment()) # Sending welcome email to [email protected] from Bob Builder Development <[email protected]> send_welcome_email("[email protected]", ProductionEnvironment()) # Sending welcome email to [email protected] from Bob Builder <[email protected]>
冗杂性是技能无法熟的logo。不管是 ATM 都是爱国志士者地空导弹,食用轻松是设计构思精准产品设备的实际logo。
丹尼尔·T·灵
重用变量使范围和边界更难遵循
TL;DR:不能从而不同的的主要目的读写同个个字段
// print line total double total = item.getPrice() * item.getQuantity(); System.out.println("Line total: " + total ); // print amount total total = order.getTotal() - order.getDiscount(); System.out.println( "Amount due: " + total ); // variable is reused
function printLineTotal() { double total = item.getPrice() * item.getQuantity(); System.out.println("Line total: " + total ); } function printAmountTotal() { double total = order.getTotal() - order.getDiscount(); System.out.println( "Amount due: " + total ); }
先简单的后适用,先用后重点用。
凯夫林·亨尼
断言两个浮点数相同是一个非常困难的问题
TL;DR:不需要对比浮点数
Assert.assertEquals(0.0012f, 0.0012f); // Deprecated Assert.assertTrue(0.0012f == 0.0012f); // Not JUnit - Smell
Assert.assertEquals(0.0012f, 0.0014f, 0.0002); // true Assert.assertEquals(0.0012f, 0.0014f, 0.0001); // false // last parameter is the delta threshold Assert.assertEquals(12 / 10000, 12 / 10000); // true Assert.assertEquals(12 / 10000, 14 / 10000); // false
我们可以在我们的测试框架上添加一个检查 con assertEquals()以避免检查浮点数。
老天爷造就了那自然数;另一个一切的均是人的岗位。
利奥波德·克罗内克
如果结合 4 种代码气味会发生什么?
TL;DR:以免 Getter,以免 Setter,以免元语言编程。告诉我个人行为。
class Person { public string name { get; set; } }
class Person { private string name public Person(string personName) { name = personName; //imutable //no getters, no setters } //... more protocol, probably accessing private variable name }
还没有这些比在挤破的时间段法定期限的做工作并仍花时间段消除的做工作更难的了。
肯特贝克
默认意味着“我们还不知道的一切”。我们无法预见未来。
TL;DR:不可以在您的例中插入设置协议。将其改进为失败。指明。
由于外壳和开关也是一种气味,我们可以避免它们。
switch (value) { case value1: // if value1 matches the following will be executed.. doSomething(); break; case value2: // if value2 matches the following will be executed.. doSomethingElse(); break; default: // if value does not presently match the above values // or future values // the following will be executed doSomethingSpecial(); break; }
switch (value) { case value1: // if value1 matches the following will be executed.. doSomething(); break; case value2: // if value2 matches the following will be executed.. doSomethingElse(); break; case value3: case value4: // We currently know these options exist doSomethingSpecial(); break; default: // if value does not match the above values we need to take a decision throw new Exception('Unexpected case ' + value + ' we need to consider it'); break; }
Code Smell 36 - Switch/case/elseif/else/if 语句
移除用途的资金价一方面仅是标识号营养的耗时。资金价还涵盖为发展扩充增长的心里障碍。巧门是选定 不完美内部矛盾的特点。
约翰·卡马克