software-design/functions-why-you-should-stop-copy-pasting-your-code.md

Functions: Why You Should Stop Copy-Pasting Your Code

Building software · 9 min read ·

Copy-pasting code works fine on the day you do it. It fails the day you need to change it, and have to find and fix every copy without missing one.

A function fixes that: write the code once, give it a name, and call it as often as you like. This page shows how values go in (parameters and arguments), how they come out (return, and why print is not the same thing), and why programmers live by DRY: Don't Repeat Yourself.

$ ls software-design/

see all →
software-design/feature-flags-how-to-ship-code-switched-off-and-turn-it-on-for-10-of-u.md Feature flags: how to ship code switched off and turn it on for 10% of users A feature flag is a yes/no setting your code checks before choosing between old and new behaviour, so you can ship code switched off and… 5 min read · 2 Nov software-design/composition-vs-inheritance-why-a-toy-duck-shouldn-t-learn-to-fly.md Composition vs inheritance: why a toy duck shouldn't learn to fly A rubber duck that learns to fly is a classic sign that inheritance, the "is a" way of sharing code, has been stretched too far. Every… 5 min read · 30 Oct software-design/the-observer-pattern-how-a-shop-tells-many-functions-about-one-order-w.md The observer pattern: how a shop tells many functions about one order without knowing them A shop that must email a receipt, update stock and add loyalty points ends up with one function that knows about everything. Every new… 5 min read · 27 Oct software-design/refactoring-how-to-tidy-working-code-without-breaking-it.md Refactoring: how to tidy working code without breaking it Refactoring means changing how code is built on the inside without changing what it does. It sounds risky, but there is a simple routine… 6 min read · 24 Oct software-design/unit-tests-why-four-passing-tests-don-t-prove-a-function-works.md Unit tests: why four passing tests don't prove a function works Your function gave the right answer four times. Is it correct? Not necessarily: passing tests only show the bug isn't where you looked… 4 min read · 21 Oct software-design/idempotency-why-clicking-pay-twice-should-charge-you-once.md Idempotency: why clicking Pay twice should charge you once You tap Pay, the screen shows an error, you tap again, and your card is charged twice. Networks drop messages, so a phone often cannot tell… 6 min read · 18 Oct