Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
PHP Auto-Reconnect on "MySQL has gone away"
Long-running daemons sometimes lose their MySQL connection mid-run. Wrap your DB calls to retry once after a transient connection-lost error.
Rust Format Strings (println! / write!)
Rust's format macros take an inline format string with `{}` placeholders. Named arguments, alignment, precision, debug formatting — all in the format spec.
SQL Correlated Subquery
A subquery that references the outer query's row. Slower than a JOIN for big result sets but sometimes the most readable expression.
Rust Builder Pattern with Method Chaining
Construct complex objects step-by-step with `self`-returning methods. Type-state can enforce required vs. optional fields at compile time — but the simple version is plenty for most cases.
PHP Verify a JWT (HS256)
Pair to jwtSign: verify the signature, check the exp claim, and return the decoded payload — or null on any failure. Uses hash_equals for constant-time signature comparison.
SQL Median with PERCENTILE_CONT
SQL has no `MEDIAN()` — use `PERCENTILE_CONT(0.5) WITHIN GROUP`. The same function gets you P95, P99, quartiles, etc. for latency analysis.
SQL Soft Delete with Partial Index
Set a `deleted_at` timestamp instead of physically removing rows — preserves history and lets you "undelete". Pair with a partial index for fast queries that skip the soft-deleted rows.
PHP Format Money with Currency
Format an amount of cents as a localized currency string using NumberFormatter from intl. Falls back to a basic sprintf if intl isn't available.
Bash Pretty-Print JSON from cURL
Pipe API responses through jq for syntax-colored, indented output. Also great for extracting specific fields without writing a parser.
SQL INNER JOIN — Match Rows in Both Tables
Returns rows where the join condition matches in BOTH tables. The default JOIN. Use table aliases (`u`, `o`) for readability when joining 3+ tables.
PHP Simple File-Backed Cache
A tiny key-value cache with TTL, persisted as a serialized PHP file per key. Good enough for memoizing expensive computations on a single machine; replace with Redis when scaling out.
Java Path Operations — Resolve, Relativize, Normalize
`Path` (NIO) replaces `File` for new code. Operator-like methods compose paths cleanly across OSes — and the underlying file isn't touched until you do I/O.
TypeScript Concurrency Limiter (semaphore)
Run an array of async tasks but limit concurrency to N at a time — like p-limit, in 30 lines. Preserves input order in the output array.
Java Collectors.groupingBy — Group by Key
Like SQL GROUP BY for streams. Produces a `Map<K, List<V>>` (or any downstream collector you specify). Indispensable for analytics-style aggregations.
Go Graceful HTTP Shutdown
On SIGINT/SIGTERM, stop accepting new connections but let in-flight requests finish. The `http.Server.Shutdown` method does exactly that — with a deadline.
PHP UTF-8 Safe substr / strlen
Wrap mb_* with sensible defaults so you stop accidentally chopping multi-byte characters in half. Defensive helpers for projects where input is not guaranteed ASCII.
JavaScript Validate Email Address
Validates an email address using a battle-tested regex that covers the vast majority of real-world addresses without being overly strict. For server-side code, pair this with a confirmation email step — client-side regex alone is not a security measure. Returns a boolean for easy conditional use.
JavaScript Deep Clone Object
Creates a true deep copy of any serialisable object or array — nested objects, arrays, dates (as ISO strings). Uses structuredClone when available (modern browsers/Node 17+) and falls back to JSON round-trip for older environments. Does not handle functions, undefined, or circular references.