admin
@admin ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes

Showing 121–150 of 770 public snippets

Rust
Atomic Counter (lock-free)
For shared counters and flags, `AtomicU64` / `AtomicBool` etc. are much faster than `Mutex`. Operations like `fetch_add` are single CPU instructions on most architectures.
2m ago 0
Bash
Print a Boxed Banner
Wrap a string in a Unicode box for important headers. Auto-sizes to the longest line. Helps milestone messages stand out in long CI logs.
2m ago 0
Kotlin
Parameterized Tests with JUnit 5
JUnit 5 + Kotlin: `@ParameterizedTest` runs the same test body with multiple input rows. `@CsvSource` for inline data, `@MethodSource` for complex args.
2m ago 0
Kotlin
use { } — Auto-Close Resources
`use { }` calls `close()` on any `Closeable` (file, stream, JDBC connection, …) when the block exits — even on exception. Kotlin's try-with-resources.
2m ago 0
Kotlin
Coroutine Timeout with withTimeout
`withTimeout(ms) { ... }` cancels the inner coroutine if it doesn't finish in time and throws `TimeoutCancellationException`. `withTimeoutOrNull` returns null instead.
2m ago 0
Bash
Check Command Exists Before Using It
`command -v` is the portable, fast way to check whether a binary is on $PATH. Avoid `which` (its behavior varies between systems).
2m ago 0
Go
Prepared Statement Reuse
For repeated queries (loops, bulk inserts), prepare once and re-use. The driver caches the parsed query and saves a round trip per invocation.
2m ago 0
PHP
cURL Multipart File Upload
Upload one or more files via multipart/form-data using cURL's CURLFile abstraction. No manual boundary or body construction needed.
2m ago 0
Bash
Multipart File Upload via cURL
Upload one or more files using -F. Optional extra form fields go through the same flag — quote carefully to preserve spaces.
2m ago 0
Bash
Most-Modified Files in Repo
Find the hottest files in a git history — useful for spotting hotspots that need refactoring or extra test coverage.
2m ago 0
Bash
Rotate Logs Manually
If a service doesn't hook into logrotate, you can still rotate a log file yourself in one safe pattern: rename current → .1, truncate the original, signal the process to re-open if needed.
2m ago 0
HTML
Sortable Table (semantic markup + JS hook)
Use `<button>` inside `<th>` and `aria-sort` on the column so screen readers announce sort state. The button is the keyboard-accessible trigger; JS only handles the actual sort.
2m ago 0
TypeScript
Generic Constraints with `extends`
Generics start unbounded — you can't access any properties of `T`. `T extends { … }` adds a constraint so the body can safely use known shape, while callers can still pass in narrower types.
2m ago 0
Java
Atomic File Write (move with ATOMIC_MOVE)
Write to a temp file in the same directory, then `Files.move` with `ATOMIC_MOVE` to the target. Readers never see a half-written file — critical for config / state files.
2m ago 0
PHP
Recursive Directory Walker
Yield every file under a directory using a generator and SPL's RecursiveIteratorIterator. Filter by extension or any other predicate without slurping all paths into memory first.
2m ago 0
Go
iota — Auto-Incrementing Constants (Enums)
Go has no `enum` keyword but `iota` inside a const block gives you the same effect. Each line is the previous expression with iota auto-incremented.
2m ago 0
Python
Context Manager via @contextmanager
Build a custom context manager without writing a class — just a generator with one `yield`. The code before yield runs on enter, after yield runs on exit (even on exception).
2m ago 0
Go
filepath.Walk — Recursive Tree Walk
`filepath.WalkDir` (Go 1.16+) is the modern, faster version. Visit every file/dir under a root; the callback decides whether to skip or process.
2m ago 0
Go
Worker Pool with Channels
Spawn N worker goroutines, feed them tasks over a jobs channel, collect results on a results channel. Idiomatic Go pattern for bounded concurrency.
2m ago 0
Go
select — Multiplex Channel Operations
`select` is like a switch for channels — runs whichever case is ready. Use `default` for non-blocking sends/receives; `case <-time.After(d)` for timeouts.
2m ago 0
Kotlin
tailrec — Tail-Call Optimization
Mark a recursive function `tailrec` and the compiler rewrites it as a loop — no stack frame per call, no StackOverflowError on deep recursion. Function must end with itself as the FINAL expression.
2m ago 0
Kotlin
Companion Objects
A `companion object` inside a class hosts what other languages call "static" members. Methods + properties on the companion are callable as `Foo.bar()`, but unlike static, they're a real object you can extend or implement interfaces on.
2m ago 0
TypeScript
safeJsonParse — Result-Typed
Parse JSON without ever throwing. Returns a Result-like discriminated union so callers must handle both success and failure paths at compile time.
2m ago 0
Bash
Unique While Preserving Order
`sort -u` re-orders. `awk '!seen[$0]++'` is the classic order-preserving dedupe — and it's a single line.
2m ago 0
Go
errors.Join — Aggregate Multiple Errors
Go 1.20+ ships `errors.Join`, which combines multiple errors into one. Stop accumulating errors in a slice and stringifying yourself — use the standard library.
2m ago 0
SQL
INSERT … SELECT — Bulk Copy
Copy/transform rows from a SELECT into another table — the cheapest way to move data within the database. Skip column lists at your peril; positional matching is fragile.
2m ago 0
Java
Stream Basics — filter / map / collect
The fluent pipeline for transforming collections. `filter` keeps matching elements, `map` transforms each one, `collect(toList())` materializes the result.
2m ago 0
SQL
RANK / DENSE_RANK
`RANK` leaves gaps after ties (1, 2, 2, 4); `DENSE_RANK` doesn't (1, 2, 2, 3). `ROW_NUMBER` is always unique. Pick by what tied rows should produce.
2m ago 0
Go
regexp.MustCompile + FindAll
`regexp.MustCompile` panics on a bad pattern at startup — perfect for package-level vars. Use `Find`, `FindString`, `FindAllStringSubmatch` to extract matches and capture groups.
2m ago 0
PHP
Atomic File Write
Write a file in a way that other processes never see a half-written state. Write to a temp file in the same directory, then rename — rename is atomic on POSIX filesystems.
2m ago 0