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
Bash Safe Bash Script Template
Start every script with the same skeleton: strict error mode (errexit, nounset, pipefail), an IFS reset, and a main() function. Catches silent failures that have lost engineers weeks of debugging.
Go Atomic File Write (temp + rename)
Write to a temp file in the SAME directory, then `os.Rename` to the final name. Rename is atomic on POSIX — readers never see a half-written file.
Rust Read a File — Whole / Lines / Bytes
Three common patterns: load it all into memory, iterate line-by-line, or stream bytes. Pick by file size — `read_to_string` is convenient but bad for huge files.
Bash Atomic File Write (write then rename)
Write to a temp file in the SAME directory, then `mv` to the final name. mv on the same filesystem is atomic — readers never see a half-written file.
Kotlin Read a File — Whole, Lines, or Stream
Stdlib extensions on `java.io.File` and `java.nio.file.Path`. Use `readText()` for small files, `useLines { }` for memory-efficient line-by-line.
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.
Rust Atomic File Write (tempfile + persist)
Write to a temp file in the SAME directory, then atomically `persist` (rename). The `tempfile` crate handles cleanup if you crash before persisting.
Bash Read File Line-By-Line (the safe way)
The classic `for line in $(cat file)` is wrong — it word-splits and globbing fires on filenames. Use `while IFS= read -r line` with input redirection.
Python Safe JSON Read/Write
Idiomatic helpers for reading + writing JSON to disk, with atomic writes and pretty-printed output. Catches the common "edit and save → corrupt file on crash" failure mode.
Go Read File — Whole / Lines / Bytes
Three common patterns: load it all (`os.ReadFile`), iterate line-by-line with `bufio.Scanner`, or stream chunks via `io.Reader`. Pick by file size.
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.
Java BufferedReader / BufferedWriter
For large files or line-oriented protocols, wrap a Reader/Writer in `Buffered*`. Reduces per-call overhead from many small reads to fewer large ones. Always close with try-with-resources.
PHP Recursive Deep Merge
Merge two associative arrays at any nesting depth — like array_merge_recursive, but later values OVERWRITE earlier ones at the leaf level instead of combining them into arrays. Perfect for config-file overrides.
PHP Smart Title Case
Convert a string to title case while keeping common short words (a, an, the, of, etc.) lowercase — unless they appear at the start or end of the title.
Bash Find Largest Files and Directories
Quick "where did all my disk go?" answer. du sorts by size, ncdu (interactive) is better for exploration.
SQL IS DISTINCT FROM — Null-Safe Equality
`=` returns NULL when either side is NULL. `IS DISTINCT FROM` (and its inverse `IS NOT DISTINCT FROM`) treat NULLs as equal to themselves — the right tool for change detection.
PHP Measure Code Block Execution Time
Time a closure with microsecond precision and return both its result and the elapsed milliseconds. Great for quick perf experiments without pulling in a profiler.
Bash Split String into Array on Delimiter
Use IFS + read or readarray to split safely. Don't use word-splitting tricks — they break on edge cases (empty fields, spaces in values).