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

Showing 91–120 of 770 public snippets

Kotlin
Generic Class and Function
`<T>` declares a type parameter. Use it on classes (`Box<T>`) and functions (`fun <T> identity(x: T) = x`). Type bounds (`T : Comparable<T>`) constrain what T can be.
2m ago 0
Java
JUnit 5 — Parameterized Tests
`@ParameterizedTest` runs the same test with multiple inputs. Use `@CsvSource`, `@MethodSource`, or `@EnumSource` for data. Eliminates copy-paste in test classes.
2m ago 0
Java
Immutable Collections — List.of / Map.of
Java 9+ static factory methods return immutable collections with no nulls allowed. Concise, fast, and safer to share across threads or APIs than mutable equivalents.
2m ago 0
JavaScript
Random Hex Color
Generates a random 6-digit hex colour string. Useful for seeding avatar backgrounds, chart series colours, placeholder UI elements, and testing colour-dependent components. The crypto version produces a more unpredictable result suitable for generating unique palette tokens.
2m ago 0
SQL
Generated / Computed Columns
A column whose value is derived from other columns and maintained automatically. PostgreSQL has STORED (persisted) and MySQL adds VIRTUAL (computed on read). Saves you from triggers for derived data.
2m ago 0
Java
ExecutorService — Thread Pools
`ExecutorService` is the modern way to manage threads. Submit Callables/Runnables, get Futures back. Always shutdown the executor — and prefer try-with-resources on Java 19+ where ExecutorService is AutoCloseable.
2m ago 0
Kotlin
Coroutine Testing with runTest
`runTest { }` from kotlinx-coroutines-test gives you a virtual time scheduler — `delay(1000)` finishes instantly while preserving ordering. No more flaky 1-second test sleeps.
2m ago 0
TypeScript
invariant() — Always-On Assertion
Throw if a condition is false, with TypeScript narrowing the type afterwards. Replaces ad-hoc `if (!x) throw` ladders and gives you type-safe guards in one line.
2m ago 0
Bash
Cron Entry From Script
Append a cron job idempotently — grep first to avoid duplicates on re-run. Doesn't require editing files manually.
2m ago 0
Kotlin
zip and zipWithIndex
`zip` pairs elements positionally; the result is the length of the shorter list. `withIndex()` gives you `(index, value)` pairs without the manual `forEachIndexed`.
2m ago 0
Kotlin
Local Functions
Functions can be declared inside other functions. Closures over outer scope let you eliminate parameter-threading. Use sparingly — too many nested locals hurts readability.
2m ago 0
Kotlin
select — Choose Among Channels
`select { }` lets a coroutine wait on multiple suspending operations and proceed when the first one is ready — like nginx select() but for channels, deferreds, and timers.
2m ago 0
SQL
ARRAY_AGG and JSON_AGG (PostgreSQL)
Roll up grouped rows into a PostgreSQL array or JSON array — often a faster replacement for the application doing the same "group children under parent" join.
2m ago 0
PHP
Resize Image to Max Dimension
Shrink an uploaded image so its longest side is no more than $maxDim pixels, preserving aspect ratio. Re-encodes to JPEG at the given quality. Uses the GD extension.
2m ago 0
SQL
Views and Materialized Views
A view is a named query — no data stored. A materialized view caches the result; refresh it on demand. Great for expensive joins or aggregations that don't need to be real-time.
2m ago 0
HTML
Video Element with Captions
`<video>` with multiple `<source>` formats (MP4 + WebM), `controls`, `poster`, and a captions track via `<track kind="captions">`. All shipped with the browser — no JS player needed.
2m ago 0
Kotlin
Immutable vs Mutable Collections
`listOf` / `setOf` / `mapOf` build READ-ONLY views; `mutableListOf` / `mutableSetOf` / `mutableMapOf` are mutable. Default to immutable — the call site is a contract about intent.
2m ago 0
Kotlin
fold and scan
`fold` accumulates from a seed across all items. `runningFold` / `scan` yields every intermediate accumulator value — running totals, cumulative metrics.
2m ago 0
HTML
Comment / Review Markup with Schema
Comments and reviews can also have Schema.org markup. The `Review` type appears under products in Google search results with star ratings; `Comment` is for general user feedback.
2m ago 0
SQL
Window Function vs N+1 Query
Don't fetch a list, then loop in code to count each parent's children. Use a window function or a single GROUP BY — turn 1,001 queries into 1.
2m ago 0
SQL
Indexes — B-tree, Partial, Composite
Indexes are the difference between a 50ms query and a 50s one. Composite indexes match queries that filter / sort on the columns in order; partial indexes skip irrelevant rows for big space savings.
2m ago 0
SQL
Full-Text Search (PostgreSQL tsvector)
`tsvector` + `tsquery` is built-in full-text search — stemming, ranking, multilingual. For most apps this beats reaching for Elasticsearch right away.
2m ago 0
Java
Optional — Stop Returning Null
`Optional<T>` makes "may be absent" part of the type signature so callers can't forget the null check. `map`, `orElse`, `ifPresent` chain naturally — no `if (x != null)` boilerplate.
2m ago 0
Kotlin
Star Projections (List<*>, etc.)
`List<*>` means "list of something I don't need to know exactly" — read-only with Any? as element type. Useful when you genuinely don't care about the parameter.
2m ago 0
Bash
Background Jobs + wait
Run several commands in parallel with `&`, then wait for all of them with `wait`. Collect exit codes via $! and check after wait returns.
2m ago 0
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
Go
strings package essentials
The most-reached-for functions in the `strings` package: Split, Contains, TrimSpace, ToLower/Upper, Replace, Index, HasPrefix/Suffix.
2m ago 0
SQL
EXPLAIN / EXPLAIN ANALYZE
`EXPLAIN` shows the query plan; `EXPLAIN ANALYZE` actually runs it and shows real timings. The first tool when a query is slow — look for sequential scans on big tables.
2m ago 0
TypeScript
takeWhile / dropWhile
Consume or skip leading elements as long as a predicate holds. Useful for parsing prefixed sequences (e.g. all leading whitespace tokens, all unread notifications, etc.).
2m ago 0
Python
Multipart File Upload
POST a file (or several) as multipart/form-data using requests, with optional extra form fields. Lets you upload to /avatar-style endpoints without manual boundary construction.
2m ago 0