Gists
Share and discover code snippetsCoalesce Null Default
Returns the first non-null value from a list of expressions, using a default if all are null.
Ternary null coalescing
Assigns a default value when a variable is null, using PHP's shorter null coalescing operator.
Strict Type Predicate
A TypeScript utility that creates a type guard function to verify if a value matches a specific type at runtime, ensuring type safety when working with uncertain data.
concurrent HTTP requests
A Go code snippet that fires off multiple HTTP requests in parallel using goroutines and a sync.WaitGroup to handle them concurrently.
Sort array descending
This Rust code snippet sorts an array in descending order using the sortby method with a comparison closure.
Array destructuring swap
Swaps two variables without a temporary variable using array destructuring.
Center with Flexbox
This CSS snippet centers any child element both horizontally and vertically inside a flex container.
Array Shuffle Fisher Yates
Shuffles an array in place using the Fisher-Yates algorithm for uniform random distribution.
Array destructuring swap
Swaps two values without a temporary variable by using array destructuring assignment.
Hash String Comparison
Compares two strings by their hash values to check for equality efficiently.
Hashing for equality works great until you hit a collision, and then you're stuck debugging a false positive that looks like a bug in your comparison logic.
Iterate with enumerate
Enumerate in Rust lets you iterate over a collection while automatically tracking the index of each element.
Async file read
This snippet reads the contents of a file asynchronously using Tokio's tokio::fs::readtostring function.
Ternary inline condition
Returns one of two values based on a true or false condition, mimicking an if-else statement in a single line.
COALESCE for defaults
Returns the first non-null value from a list, making it easy to set fallback defaults for missing data.
Flexbox Center Alignment
This snippet centers a child element both horizontally and vertically within its parent using Flexbox.
truncate cascade tables
This SQL snippet drops all dependent foreign key relationships before truncating a table.
File exists check
This Bash snippet returns true before attempting to write if a file exists, preventing overwrite errors.
PHP array map filter
Applies a callback to each element of an array, then filters the results based on a condition.
re.sub cleanup string
This Python snippet uses regex to remove unwanted characters or patterns from a string.
Move Semantics Example
This snippet demonstrates efficient resource transfer by using move semantics to avoid expensive deep copies when passing temporary objects.
The null coalescing operator (
??) works well for undefined keys too, not just null values. In your example,$username ?? 'guest'avoids an undefined index notice if$usernameisn't set, which the ternary operator (isset($username) ? $username : 'guest') also handles but with more syntax. Just watch for nested coalescing, as it evaluates left to right and can mask deeper null issues in chained calls like$a ?? $b ?? $c.