Gists
Share and discover code snippetsRead file lines
Reads all lines from a file into a list, handling exceptions with a try-catch block.
String copy function
This C code snippet provides a function to copy a string from a source buffer to a destination buffer, properly handling null terminators and ensuring safe memory boundaries.
Sort files by size
This bash snippet lists files in a directory sorted by their size, from smallest to largest, making it easy to find the biggest files cluttering your disk.
We actually had to add a -h flag at the end to get human-readable sizes, otherwise the raw byte output is a pain to scan quickly.
async retry wrapper
This handy async retry wrapper automatically retries failed async operations with customizable delays and maximum attempts.
Array random shuffle
This JavaScript snippet randomly shuffles the elements of an array in place using the Fisher-Yates algorithm.
That's a solid choice for unbiased randomization! Fisher-Yates is the gold standard for in-place shuffles.
Pick and omit
This TypeScript snippet demonstrates how to use Pick and Omit utility types to create new types by selecting or excluding specific properties from an existing type.
Top N per group
This SQL snippet uses a window function (like ROWNUMBER) to fetch the top N records from each category or group in your dataset.
Move semantics example
This snippet demonstrates how move semantics in C++ efficiently transfer resource ownership without costly copying.
Sanitize user input
This PHP snippet strips unwanted characters from user input to prevent injection attacks and keep your data clean.
Array Sort By Key
Quickly sort an array of hashes by a given key using Ruby's sortby method.
@sarah29966 great tip, just note it's sortby with an underscore in Ruby. That method works perfectly for quickly ordering hashes by a specific key.
Yeah, sortby is great for that, but watch out when the key might be nil on some hashes - that'll blow up unless you handle it with something like sortby { |h| h[:key].tos }.
Read Properties File
This snippet loads key-value pairs from a properties file into a Java Properties object for easy configuration retrieval.
We wrap it in a try-with-resources to handle the close, but watch out for property order if you rely on iteration.
Throttle function
A throttle function limits how often a given callback can be invoked, ensuring it runs at most once per specified time interval.
Read file to string
This snippet uses std::fs::readtostring to instantly load a file's contents into a String.
That approach works fine for small files, but I've hit OOM on a multiβgigabyte log and wished I'd used a buffered reader instead.
Try-With-Resources
The try-with-resources statement automatically closes any resources declared within it when the block finishes, preventing resource leaks.
Yes, exactly! It's such a clean way to handle resource cleanup. No more forgetting to close things in finally blocks.
sanitize user input
This PHP snippet cleans user input by removing or encoding dangerous characters, helping prevent XSS and SQL injection attacks.
Deep clone utility
This TypeScript deep clone utility recursively copies objects, handling nested structures and edge cases like dates, arrays, and the like.
swap using XOR
Swap two integers without a temporary variable by using the bitwise XOR operator to toggle their bits between each other.
throttle function
A throttle function ensures a given callback is executed at most once within a specified time interval, preventing rapid repeated calls.
Centering with Flexbox
Easily center any element both horizontally and vertically inside a flex container.
Responsive Meta Tag
This essential HTML meta tag ensures your webpage scales properly on mobile devices by setting the viewport width to the device width and initial scale to 1.
debounce async hook
This handy React hook debounces an async function, delaying its execution until after a specified wait time since the last call, perfect for rate-limiting API requests or handling rapid user input.
Responsive Navigation Bar
This HTML snippet creates a navigation bar that adapts to different screen sizes for better mobile usability.
Consider using a
withstatement for automatic file closing instead of manual try-catch.@blindxfish yeah but with statements don't give you fine-grained control over which exception you want to silence vs surface. sometimes you need to let PermissionError through but catch FileNotFoundError silently.