SaveSnippets
Community
Pricing
Sign In
Get Started
@admin
ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes
Showing
451–480
of
770
public snippets
Go
Functional Options Pattern
Replace giant config structs with a variadic list of `Option` functions. Lets you add optional parameters later without breaking existing callers. The standard Go API design for constructors.
1h ago
0
Java
Files.walk — Recursive Tree Traversal
`Files.walk` returns a lazy Stream over every entry under a path. Filter with stream operations; remember to close the stream (or use try-with-resources).
1h ago
0
SQL
TRUNCATE vs DELETE
`DELETE` is logged per-row and respects triggers; `TRUNCATE` deallocates whole pages and resets the table to empty. `TRUNCATE` is much faster on large tables but has caveats (no WHERE, can't be rolled back in some DBs).
1h ago
0
PHP
Random Readable Token
Generate a short, human-readable random token using an alphabet that omits look-alike characters (0/O, 1/l/I). Useful for invite codes, short-lived signin tokens, password reset codes — anything a human might type.
1h ago
0
PHP
Bulk INSERT in a Single Statement
Insert a list of rows in one round trip by building a multi-VALUES statement with placeholders. Orders-of-magnitude faster than looping single INSERTs.
1h ago
0
PHP
Secure Session Bootstrap
Start a session with all the security flags you should always set: HttpOnly, SameSite=Strict, Secure on HTTPS, custom name, and an idle-timeout regeneration.
1h ago
0
TypeScript
Sum / Mean / Median (numeric arrays)
The descriptive-stat trio for number[]. Median sorts a copy so the input is left alone. Empty input returns 0 / 0 / 0 rather than NaN — opinionated, but predictable.
1h ago
0
Bash
Uppercase / Lowercase / Title Case
Bash 4+ has built-in case conversion via parameter expansion. No need for `tr` or `awk` for most cases.
1h ago
0
Bash
Check URL Returns 200
Quick health-check helper: returns 0 if a URL responds with a 2xx status, non-zero otherwise. Use in pre-deploy smoke tests.
1h ago
0
Go
time.Format — The Reference Time
Go's time formatting uses a reference date instead of strftime tokens: `Mon Jan 2 15:04:05 MST 2006` (which is 01/02 03:04:05PM '06 -0700, or 1/2/3/4/5/6/7 — mnemonic). Strange at first, instantly memorable.
1h ago
0
Java
Collectors.toMap with Merge Function
`toMap` builds a `Map<K, V>` from a stream. The 3-arg form takes a merge function for handling duplicate keys — without it, duplicates throw IllegalStateException.
1h ago
0
HTML
Select with Optgroup
`<optgroup label="...">` visually groups options inside a `<select>`. Renders as a non-selectable header in every browser; screen readers announce the group as context.
1h ago
0
Kotlin
Compose State Hoisting
Lift state UP to where it's needed. A composable becomes "stateless" (taking `value` + `onValueChange`) which makes it reusable and testable. The Compose mantra.
1h ago
0
Kotlin
JDBC with use { } — Auto-Close Connections
Plain JDBC is fine for simple cases. Wrap connections / statements / result sets in `use { }` for guaranteed cleanup even on exceptions.
1h ago
0
PHP
PDO Transaction Wrapper
Run a closure inside a transaction. Commits on success, rolls back on any exception, then re-throws so the caller can react. Avoids the bug-prone copy-paste of begin/commit/rollBack.
1h ago
0
PHP
Paginated Query Helper
Run a SELECT with LIMIT/OFFSET and also return the total row count, so you can render "Page 3 of 47" UIs. Single function, two round trips.
1h ago
0
PHP
Haversine Distance Between Two Coordinates
Compute the great-circle distance between two latitude/longitude points using the haversine formula. Returns kilometers; multiply by 0.621 for miles. Useful for "stores near me" features.
1h ago
0
Python
Async Context Manager (aiohttp pattern)
Define an async resource using `@asynccontextmanager` from contextlib. Cleaner than writing a class with __aenter__/__aexit__ for one-off wrappers.
1h ago
0
Python
Generate Strong Random Password
Build a password using the `secrets` module (CSPRNG) with rejection sampling for unbiased distribution. Use this — NOT random.choice, which is seeded predictably.
1h ago
0
Bash
flock — Cron-Singleton Lock
Prevent two copies of a cron job from running simultaneously. flock takes an FD to a lock file; if the lock is held, the second invocation exits immediately.
1h ago
0
Go
SHA-256 Hash of File or String
`crypto/sha256` is the canonical hash. Use the streaming `Hash` interface for files — never load multi-GB content into memory just to hash it.
1h ago
0
Java
Stream.generate / iterate — Infinite Streams
Build streams from a seed + a function. `generate(supplier)` calls the supplier each time; `iterate(seed, next)` applies a unary op. Always pair with `limit` to bound them.
1h ago
0
HTML
Datalist — Autocomplete Suggestions
`<datalist>` attaches type-ahead suggestions to any text input. The user can still type something custom — it's suggestions, not a hard constraint.
1h ago
0
Kotlin
Operator Overloading
Specially-named methods or extensions get operator syntax: `plus → +`, `minus → -`, `times → *`, `get → []`, `invoke → ()`, `contains → in`. Use sparingly for types that have natural arithmetic semantics.
1h ago
0
PHP
cURL POST JSON
Post a JSON body with the right Content-Type header and parse the response. Returns the decoded body or throws on a non-2xx response.
1h ago
0
PHP
Business Days Between Two Dates
Count weekdays (Mon-Fri) between two dates, excluding an optional list of holidays. Direction-aware (works even if $end < $start).
1h ago
0
PHP
Build & Sign a JWT (HS256)
Generate a JWT manually using only base64-url and hash_hmac — no library required. Demonstrates header/payload/signature concatenation and the exp claim.
1h ago
0
TypeScript
Promisify a Callback Function
Convert a Node-style `(err, result)` callback API into a Promise-returning function. Type-safe via generics — the inferred Promise resolves to the original callback's result type.
1h ago
0
Bash
Substring + Find/Replace (parameter expansion)
Bash's ${var:offset:length} and ${var//pattern/replacement} let you do most string operations without ever spawning sed. Faster, and the syntax is portable across modern shells.
1h ago
0
Rust
Iterator Chain — map / filter / collect
Rust iterators are lazy and zero-cost. Chain transformations and finally collect into a concrete container. The compiler unrolls the whole pipeline into a tight loop.
1h ago
0
1
…
14
15
16
17
18
…
26