Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ancms2600/2d6d8783793545c09cb670cefbce6f44 to your computer and use it in GitHub Desktop.
Save ancms2600/2d6d8783793545c09cb670cefbce6f44 to your computer and use it in GitHub Desktop.
Sneaky single-line temporary scoping of variable declarations and cache-reuse (Javascript trivia)
// e.g., when you want parameters passed in a pre-sorted order, but you don't want to impose on the developer
{ // shadow to avoid mutating input
// three examples computing Math.min() just once, though two assignments:
const toZero = Math.min(a,c), a = a - toZero, c = c - toZero; // toZero still in scope by EOL (wasteful?)
with({toZero:Math.min(a,c)}) const [a,c] = [a-toZero,c-toZero]; // toZero out of scope by EOL (discouraged in "use strict" mode)
const [a,c] = [a,c].map((toZero=>v=>v-toZero)(Math.min(a,c))); // toZero out of scope by EOL
const [a,c] = (()=>{ const toZero = Math.min(a,c); return [a-toZero,c-toZero]; })(); // toZero out of scope by EOL (long-winded)
}
@ancms2600
Copy link
Author

a collection of possibly the worst coding practices known to man, all in one place! 😱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment