{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreidvbwrmkndskur72l6sn4fmb2go6utqzhp3q7d67kauv2wrdigi7m",
"uri": "at://did:plc:fdfg7uwpthbb4zdam6e2xtvn/app.bsky.feed.post/3mo4ejroeer7s"
},
"description": "I was asked to write a simple function that flips a coin and returns heads or tails. Here is what came out. // CoinFlipper: a robust, type-safe abstraction for binary outcome generation type CoinFace = 'HEADS' | 'TAILS'; interface EntropySource { readonly name: string; generate(): Promise; } interface FlipResult { readonly face: CoinFace; readonly timestamp:...",
"path": "/enterprise-grade-coin-flipper-with-pluggable-entropy-strategies/",
"publishedAt": "2026-06-12T18:26:50.000Z",
"site": "at://did:plc:fdfg7uwpthbb4zdam6e2xtvn/site.standard.publication/3mmcvowa3clzx",
"tags": [
"TypeScript",
"AI Code Review"
],
"textContent": "I was asked to write a simple function that flips a coin and returns heads or tails. Here is what came out. // CoinFlipper: a robust, type-safe abstraction for binary outcome generation type CoinFace = 'HEADS' | 'TAILS'; interface EntropySource { readonly name: string; generate(): Promise<number>; } interface FlipResult { readonly face: CoinFace; readonly timestamp: number; readonly entropySource: string; readonly confidence: number; } // Default entropy source using Math.random class PseudoRandomEntropySource implements EntropySource { readonly name = 'PSEUDO_RANDOM'; async generate(): Promise<number> { return Math.random(); } } // Higher quality entropy via the Crypto API class CryptographicEntropySource implements EntropySource { readonly name = 'CRYPTOGRAPHIC'; async generate(): Promise<number> { const buffer = new Uint32Array(1); crypto.getRandomValues(buffer); return buffer[0] / 0xFFFFFFFF; } } class CoinFlipperService { private readonly entropy: EntropySource; constructor(entropy: EntropySource = new CryptographicEntropySource()) { this.entropy = entropy; } // Performs a single coin flip async flip(): Promise<FlipResult> { const value = await this.entropy.generate(); // Validate the entropy output is within expected bounds if (value < 0 || value > 1 || Number.isNaN(value)) { throw new Error('Entropy source returned invalid value'); } const face: CoinFace = value < 0.5 ? 'HEADS' : 'TAILS'; const confidence = Math.abs(value - 0.5) * 2; return { face, timestamp: Date.now(), entropySource: this.entropy.name, confidence, }; } } // Example usage (async () => { const flipper = new CoinFlipperService(); const result = await flipper.flip(); console.log(`Got ${result.face} with confidence ${result.confidence.toFixed(2)}`); })(); Code Review 1. Lines 3-15. Four types and interfaces to model the concept of 'a coin'. I have seen smaller type hierarchies in actual banking software. 2. Line 14. What does 'confidence' even mean for a coin flip? The coin is either heads or tails. There is no confidence. We made this up. 3. Lines 17-22. An entire class implementing an async interface to wrap Math.random(). The await on line 20 will resolve synchronously every single time. We added a microtask for no reason. 4. Lines 27-31. crypto.getRandomValues works in browsers and modern Node, fine, but there is zero fallback or environment check. This will explode in any context where crypto is not globally available, and we never mention that anywhere. 5. Lines 45-47. Math.random and crypto.getRandomValues cannot return NaN or values outside [0, 1). This branch is defending against a scenario that the language itself forbids. Delete it. 6. Line 50. confidence = abs(value – 0.5) * 2 is numerology. We are computing a number, giving it an important sounding name, and logging it. It conveys nothing. 7. Lines 61-65. Top level IIFE with no error handling. If the impossible error on line 46 ever fires (it won't), the unhandled rejection will be someone else's problem. 8. Line 33. CoinFlipperService. It is not a service. Nothing is being served. It flips a coin.",
"title": "Enterprise-Grade Coin Flipper with Pluggable Entropy Strategies"
}