More in Systems
Read a file into a string in Go
Systems
os.ReadFile replaced ioutil.ReadFile in 1.16 and is the one-liner for small files.
Async/await with a task group in Swift
Systems
Runs the downloads concurrently and collects them, with cancellation propagating to all of them.
Context with a timeout in Go
Systems
Cancels the request and everything downstream of it after two seconds. The defer is not optional.
Records and pattern matching in Java
Systems
A record plus a switch pattern removes most of the ceremony this used to take.
Null-safe chaining in Kotlin
Systems
?. stops at the first null and ?: supplies the fallback, replacing a stack of if statements.
Check a malloc result in C
Systems
The allocation that nobody checks is the one that fails in production.
Nullable reference types in C#
Systems
Turning the feature on makes the compiler point at every place a null could slip through.
Result and the question mark operator in Rust
Systems
? propagates the error to the caller, which is why Rust functions rarely need explicit error branches.
Random picks
Sort and deduplicate lines by a field
Text Processing
Sorts on the second column and keeps the first row of each group.
Undo the last commit but keep the changes
Git
Moves HEAD back one commit and leaves everything staged, which is what you usually want after committing too early.
Fetch with a timeout
JavaScript
fetch has no timeout of its own. AbortSignal.timeout is the short way to give it one.
Which DNS records does a domain have?
Networking
dig with +short strips everything but the answer.
Responsive images that do not blow the bandwidth budget
Web & CSS
srcset lets the browser pick; sizes tells it how wide the image will actually be rendered.
Reset a Postgres sequence after a bulk import
Databases
Copying rows with explicit ids leaves the identity sequence behind, so the next insert collides with an existing row.
Centre a box, two ways
Web & CSS
Grid is one line. Flex is three and gives you control over the axes separately.
Throttle with a trailing call
JavaScript
Runs at most once per interval but still fires once more after the last event, so the final state is never dropped.