Skip to Content
Clean Code Cookbook
book

Clean Code Cookbook

by Maximiliano Contieri
September 2023
Intermediate to advanced
430 pages
8h 6m
English
O'Reilly Media, Inc.
Content preview from Clean Code Cookbook

Chapter 13. Fail Fast

There is an art to knowing where things should be checked and making sure that the program fails fast if you make a mistake. That kind of choosing is part of the art of simplification.

Ward Cunningham

13.0 Introduction

The ability to fail fast is critical for clean code. You need to take action as soon as a business rule fails. Every silent failure is a missed improvement opportunity. To accurately debug a problem you need to find the root cause. And the root cause will give you a certain hint to trace and solve the failure. Fail fast systems are more robust than weaker systems where failures are swept under the rug and processing keeps going forward, even after the failure affected the correct result.

13.1 Refactoring Reassignment of Variables

Problem

You reuse variables with different scopes.

Solution

Don’t reuse variable names. You break readability and refactor chances and gain nothing; this is a premature optimization that does not save memory. Narrow the scopes as much as possible.

Discussion

If you reuse a variable and extend its scope, automatic refactoring tools may break and your virtual machine might miss the chance to make optimizations. It is recommended that you define, utilize, and dispose of variables while keeping their lifecycle short. In this example, there are two unrelated purchases:

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

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

taxAmount = lastPurchase.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

GitOps Cookbook

GitOps Cookbook

Natale Vinto, Alex Soto Bueno
TypeScript Cookbook

TypeScript Cookbook

Stefan Baumgartner
JavaScript Cookbook, 3rd Edition

JavaScript Cookbook, 3rd Edition

Adam D. Scott, Matthew MacDonald, Shelley Powers

Publisher Resources

ISBN: 9781098144715Errata Page