Skip to main content

Repeat Yourself

The DRY principle — Don't Repeat Yourself — is a well-known rule in software development. We intentionally break that rule.

In traditional software, DRY reduces duplication and makes it easier to update multiple parts of a program by changing one section of code. But the source code of deployed smart contracts is small and doesn't change.

DRY can actually reduce clarity. Every internal function adds another indirection that developers must trace through, and those functions sometimes introduce extra logic for different cases.

Repetition can make smart contracts easier to read and reason about. Instead of using an internal function for a bit of code, just use the bit of code. Even if it is used in multiple places.

However, DRY still has its place. For example, when a large block of code performs a complete, self-contained action and is used identically in multiple locations, moving it into an internal function can improve readability. For example, Compose's ERC-721 implementation uses an internalTransferFrom function to eliminate duplication while keeping the code easy to read and understand.

Guideline: Repeat yourself when it makes your code easier to read and understand. Use DRY sparingly and only to make code more readable.