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

Showing 661–690 of 770 public snippets

Go
Interface Segregation — Small Interfaces
"The bigger the interface, the weaker the abstraction." Go encourages small, focused interfaces (often just one method) — io.Reader, io.Writer, fmt.Stringer. Define them at the consumer side.
1h ago 0
Go
Benchmarks with testing.B
`BenchmarkXxx(b *testing.B)` functions are run by `go test -bench`. `b.N` is the loop count the framework auto-tunes; report results in ns/op, allocs/op.
1h ago 0
HTML
RTL (Right-to-Left) Document
Set `dir="rtl"` on the root element for languages that read right-to-left (Arabic, Hebrew, Persian). The browser flips text alignment, scroll, and form controls automatically. Use `dir="auto"` to let user-generated text decide per-block.
1h ago 0
Kotlin
apply — Configure Object, Return Itself
`x.apply { ... }` runs the block with `x` as `this` (so you call methods/set properties directly) and returns `x`. The canonical builder-style configuration.
1h ago 0
Kotlin
Jetpack Compose — Basic Composable
Compose is Android's modern declarative UI toolkit. A `@Composable` function describes UI; the runtime re-runs it when inputs change. Replaces XML layouts entirely.
1h ago 0
Python
NewType for Nominal IDs
Python's type system is structural — `UserId` and `PostId` are both just `int` unless you ask otherwise. `NewType` creates a distinct type with zero runtime cost for static checking only.
1h ago 0
Bash
Convert Between Timezones
Set TZ inline to print a date in a specific timezone, then read with another TZ. Avoids messing with the system timezone.
1h ago 0
Rust
fold / reduce / scan
`fold` is the general aggregator: starts from an accumulator, applies a fn to each item. `reduce` uses the first item as the seed. `scan` yields intermediate accumulator values.
1h 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.
1h ago 0
Rust
Result Combinators + the ? Operator
`Result<T, E>` chains like `Option`. The `?` operator unwraps `Ok` or early-returns the `Err`, converting via `From` so callers can use one error type for many sources.
1h ago 0
Go
Custom Generic Constraints
Type constraints are interfaces with type sets. Use them to express "any numeric" or "any ordered" without resorting to `any`.
1h ago 0
TypeScript
Capitalize / Title-Case
Two common case transforms. `capitalize` for the first letter only; `titleCase` for every word, with small "stop words" left lowercase except at the edges.
1h ago 0
Python
Async Retry with Exponential Backoff
Retry an async operation up to N times, doubling the wait each attempt with random jitter to avoid thundering herd. Predicate controls retryability so 4xx errors stop immediately.
1h ago 0
Java
java.time — Instant, LocalDate, ZonedDateTime
Java 8's `java.time` package replaced the broken `Date`/`Calendar`. Three types you actually use: `Instant` (UTC moment), `LocalDate` (date with no tz), `ZonedDateTime` (date+time in a zone).
1h 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.
1h ago 0
Java
HttpClient — Async (CompletableFuture)
`sendAsync` returns a `CompletableFuture<HttpResponse<T>>` — chain it like any other CF. Fan out N requests, wait for all with `CompletableFuture.allOf`.
1h ago 0
HTML
Skip-to-Main-Content Link
Keyboard users (and screen-reader users) tab through every nav link before reaching content. A skip-link as the first focusable element jumps straight to <main>. Required for WCAG 2.1 AA.
1h ago 0
Rust
tokio mpsc Channel
Async multi-producer / single-consumer channel. Producers `.send()`, the single receiver `.recv().await` items as they arrive. Bounded — the channel back-pressures producers when full.
1h ago 0
HTML
All Standard Input Types
HTML5 input types each have built-in validation, on-screen keyboards (mobile), and pickers. Use them — `<input type="number">` beats `<input type="text">` + JS validation every time.
1h ago 0
HTML
Card Pattern (article + link wrapper)
The standard content card — image, title, excerpt, meta. Make the WHOLE card clickable by wrapping the title link and using `::after` to extend the hit area to the full card.
1h ago 0
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.
1h ago 0
Bash
Find Largest Files and Directories
Quick "where did all my disk go?" answer. du sorts by size, ncdu (interactive) is better for exploration.
1h ago 0
Rust
Box<dyn Error> — Quick Boxed Errors
If you don't want the thiserror/anyhow dependency, `Box<dyn std::error::Error>` works as a catch-all. The `?` operator promotes any concrete error via `From`.
1h 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.
1h ago 0
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.
1h ago 0
Rust
Group By a Key (fold into HashMap)
No built-in `group_by`, but `fold` into a `HashMap` does the same thing in two lines — and the type system tracks keys / values correctly.
1h ago 0
Python
JWT Sign + Verify (PyJWT)
The de-facto JWT library for Python. HS256 demo with an exp claim and the standard "verify everything" decode flow. Mind that PyJWT raises specific exceptions you can catch separately.
1h ago 0
Rust
Custom Error Type with thiserror
The `thiserror` crate generates a clean `Error + Display + Debug` impl from an enum, with automatic `From` conversions. The library-author's error-type tool of choice.
1h ago 0
Java
try-with-resources
For any `AutoCloseable` (files, streams, DB connections, locks), declare it in `try(...)` and Java auto-calls `close()` even on exception. Replaces error-prone `try / finally` blocks.
1h ago 0
Java
CountDownLatch — Wait for N Events
Block one or more threads until a count of events occurs. Set the count up front; each event calls `countDown()`; waiters call `await()`. Classic "wait until all workers are ready" pattern.
1h ago 0