Skip to Content
Clean Code Cookbook
book

Clean Code Cookbook

by Maximiliano Contieri
May 2025
Intermediate to advanced
430 pages
4h 9m
Chinese
O'Reilly Media, Inc.
Content preview from Clean Code Cookbook

第 13 章 快速失败 快速失败

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

要知道哪些地方需要检查,并确保程序在出错时能迅速失效,是一门艺术。这种选择是简化艺术的一部分。

沃德-坎宁安

13.0 引言

快速失败的能力对于简洁的代码至关重要。一旦业务规则失败,您需要立即采取行动。每一次无声的失败都是一次错失的改进机会。要准确调试问题,您需要找到根本原因。而根本原因会给你一定的提示,让你追踪并解决故障。快速失败的系统比弱小的系统更稳健,弱小的系统会将失败一扫而过,即使失败影响了正确的结果,处理仍会继续进行。

13.1 重构 变量的重新分配

问题

你可以重复使用不同作用域的变量。

解决方案

不要重复使用变量名。这样做会破坏可读性和重构机会,而且一无所获;这是一种不成熟的优化,不会节省内存。尽可能缩小作用域。

讨论

如果重复使用变量并扩展其范围,自动重构工具可能会崩溃,虚拟机可能会错失优化机会。建议您在定义、使用和处置变量时缩短其生命周期。在这个示例中,有两个不相关的购买项目:

class Item:
  def taxesCharged(self):
    return 1;

lastPurchase = Item('Soda');
# Do something with the purchase

taxAmount = lastPurchase.taxesCharged();
# Lots of stuff related to the purchase
# You drink the soda

# You cannot extract method from below without passing
# useless lastPurchase as parameter

# a few hours later…
lastPurchase = Item('Whisky'); # You bought another drink

taxAmount += lastPurchase.taxesCharged();

下面是缩小范围后的结果:

class Item:
  def taxesCharged(self):
    return 1;

def buySupper():
  supperPurchase = Item('Soda');
  # Do something with the purchase

  # Lots of stuff related to the purchase
  # You drink the soda
  return supperPurchase;

def buyDrinks():
  # You can extract the method!

  # a few hours later..
  drinksPurchase = Item('Whisky');
  # I bought another drink

  return drinksPurchase;

taxAmount = buySupper().taxesCharged() + buyDrinks().taxesCharged();

重复使用变量是,也被称为 "非上下文复制粘贴 "提示。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Clean Code Cookbook

Clean Code Cookbook

Maximiliano Contieri
Terraform Cookbook

Terraform Cookbook

Kerim Satirli, Taylor Dolezal

Publisher Resources

ISBN: 9798341659438