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
Rust Custom Display + Debug Implementation
`Debug` (for `{:?}`) is usually derived. `Display` (for `{}`) is hand-written and goes through the same `std::fmt::Formatter` API.
TypeScript Template Tag for Safe HTML
Build HTML strings with a tagged template literal that auto-escapes every interpolated value. Eliminates a class of XSS bugs without pulling in a templating library.
Kotlin Custom Property Delegate
Any object with `getValue` / `setValue` operator methods can serve as a delegate. Lets you encapsulate cross-cutting behavior (logging, persistence, validation).
Rust Parse Strings to Typed Values
`str::parse::<T>()` works for every type that implements `FromStr` — every numeric type, `bool`, `IpAddr`, `Uuid` (with the crate), etc. Returns `Result` so you can chain with `?`.
Bash OS / Distro Detection
Read /etc/os-release for distro info — it's the standard on every modern Linux. Branch deploy scripts on distro to pick apt vs. dnf vs. apk.
Bash Detect Whether Running as Root
Several scripts must run as root (or refuse to). EUID is more reliable than `whoami == root` because sudo without -E may change one but not the other.
Go strconv — String ↔ Number Conversions
`strconv.Atoi` / `Itoa` for the common int<->string case; `ParseFloat`, `ParseBool`, `Quote` for everything else. Always handle the error — strings can be anything.
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.
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.
SQL CREATE TABLE with Constraints
Declare data integrity rules right in the schema — primary keys, foreign keys, unique constraints, NOT NULL, CHECK constraints, defaults. The DB enforces them so application bugs can't corrupt your data.
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.
Bash Check Systemd Service Is Running
`systemctl is-active` is the right tool — exit code 0 if running, non-zero otherwise. Combine with restart-on-fail for poor-man's supervision.
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.
Bash Memory / CPU / Load Quick Check
One-liners for the three most-requested system metrics. Use in monitoring scripts or as ad-hoc sanity checks.
PHP Try / Retry With Logging
Run a callable up to N times; log each failed attempt and bubble the last exception. Compact, no dependencies, useful for transient failures (S3 puts, email sends, etc.).
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.).
SQL UUID Primary Keys
UUIDs as PKs avoid sequence contention across services and let clients generate IDs offline. v4 is random (worst for B-tree indexes); v7 is time-ordered (B-tree friendly).
Bash Create User With Sensible Defaults
Provision a new user account with a home directory, default shell, and sudo membership. Idempotent — re-running on an existing user is a no-op.