Code Explainers

Browse the library

ruby
class OrderConfirmationJob < ApplicationJob
  queue_as :mailers
 
  retry_on Net::OpenTimeout, wait: :polynomially_longer, attempts: 5

Resilient background jobs in Rails

background-jobs retries idempotency
Intermediate 8 steps
javascript
import { useCallback, useEffect, useRef, useState } from 'react';
 
export function useInfiniteScroll(fetchPage) {
  const [items, setItems] = useState([]);

Building an infinite scroll hook in React

custom-hooks intersection-observer pagination
Advanced 6 steps
rust
use std::error::Error;
use std::fmt;
 
#[derive(Debug)]

Building a typed error enum in Rust

error-handling enums trait-implementation
Intermediate 9 steps
php
class PostController extends Controller
{
    public function index(Request $request)
    {

Eager loading without N+1 in Laravel

eager-loading n+1 query-builder
Intermediate 9 steps
python
import random
import time
import logging
from functools import wraps

A retry decorator with exponential backoff

decorators retry exponential-backoff
Intermediate 6 steps
go
package webhooks
 
import (
	"crypto/hmac"

Verifying GitHub webhooks in Gin

hmac webhooks middleware
Intermediate 6 steps
rust
use std::time::Duration;
 
use axum::{routing::get, Router};
use tokio::net::TcpListener;

Graceful shutdown in an Axum server

graceful-shutdown signal-handling async
Intermediate 8 steps
python
from django.db import models
from django.utils import timezone
 
 

How soft deletes work in Django

soft-delete querysets managers
Intermediate 10 steps
php
class PostController extends Controller
{
    public function index(Request $request): AnonymousResourceCollection
    {

Filtered, paginated JSON APIs in Laravel

eloquent query-builder api-resources
Intermediate 8 steps
javascript
import { useState, useEffect, useMemo } from 'react';
 
function useDebounce(value, delay = 300) {
  const [debounced, setDebounced] = useState(value);

Debounced search with cancellation in React

custom-hooks debounce abortcontroller
Intermediate 9 steps
java
package com.example.payments.config;
 
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;

Type-safe config with Spring records

configuration validation records
Intermediate 6 steps
ruby
module SoftDeletable
  extend ActiveSupport::Concern
 
  included do

How a soft-delete concern works in Rails

concerns soft-delete scopes
Intermediate 8 steps
typescript
type CloneInput = Record<string, unknown> | unknown[] | Map<unknown, unknown> | Set<unknown>;
 
export function deepClone<T>(value: T): T {
  if (typeof structuredClone === "function") {

Building a deepClone with cycle safety

recursion deep-copy cycle-detection
Intermediate 8 steps
rust
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
 

Buffered log scanning in Rust

buffered-io error-handling streaming
Intermediate 9 steps
python
from functools import wraps
 
 
def memoize(func):

Building a memoize decorator in Python

decorators closures memoization
Intermediate 5 steps
php
<?php
 
namespace App\Support;
 

Grouping records with array_reduce in PHP

array-reduce grouping higher-order-functions
Intermediate 6 steps
java
@Component
public class RequestTimingInterceptor implements HandlerInterceptor {
 
    private static final Logger log = LoggerFactory.getLogger(RequestTimingInterceptor.class);

How a Spring HandlerInterceptor times requests

interceptor request-lifecycle logging
Intermediate 6 steps
ruby
class Post < ApplicationRecord
  before_validation :generate_slug, on: :create
 
  validates :slug, presence: true, uniqueness: true

How a Rails model builds unique slugs

slugs callbacks validations
Intermediate 7 steps