Code Explainers

Browse the library

javascript
import { useEffect, useRef } from "react";
import { useLocation } from "react-router-dom";
 
export function RouteFocus({ title, children }) {

Managing focus on route change in React

accessibility focus-management routing
Intermediate 6 steps
python
import base64
import hashlib
import hmac
import os

How TOTP two-factor codes work

totp hmac authentication
Intermediate 7 steps
java
public final class MultipartParser {
 
    public record FilePart(String name, String filename, String contentType, byte[] content) {}
 

Parsing multipart form data by hand in Java

parsing byte-arrays http
Advanced 8 steps
php
<?php
 
namespace App\Jobs;
 

A retrying queue job in Laravel

queues background-jobs retries
Intermediate 7 steps
ruby
class Contact < ApplicationRecord
  PHONE_FORMATS = {
    "US" => /\A\+1\d{10}\z/,
    "GB" => /\A\+44\d{10}\z/,

Country-aware phone validation in Rails

validation callbacks regex
Intermediate 6 steps
python
from configparser import ConfigParser, ExtendedInterpolation
from pathlib import Path
 
 

Layered INI config loading in Python

configuration parsing defaults
Intermediate 8 steps
rust
use axum::{
    body::Body,
    extract::State,
    http::{header, StatusCode},

Streaming NDJSON from Postgres in Axum

streaming backpressure async
Advanced 9 steps
java
@AutoConfiguration
@EnableConfigurationProperties(RateLimiterProperties.class)
@ConditionalOnClass(RateLimiter.class)
public class RateLimiterAutoConfiguration {

Building a Spring Boot rate-limiter auto-config

auto-configuration conditional-beans rate-limiting
Intermediate 7 steps
javascript
const STORAGE_KEY = "theme-preference";
 
function getSystemTheme() {
  return window.matchMedia("(prefers-color-scheme: dark)").matches

Building a dark-mode toggle that respects the OS

dark-mode localstorage matchmedia
Intermediate 8 steps
ruby
module AppConfig
  module_function
 
  def fetch(key, default: nil, required: false)

Typed environment variable config in Ruby

environment-variables type-coercion configuration
Intermediate 7 steps
php
final class Container
{
    private array $bindings = [];
    private array $instances = [];

Building a DI container with reflection in PHP

dependency-injection reflection autowiring
Advanced 9 steps
typescript
interface JwtPayload {
  exp?: number;
  iat?: number;
  sub?: string;

Decoding a JWT to check expiry

jwt base64url type-guards
Intermediate 8 steps
go
package logbuffer
 
import (
	"bufio"

A buffered logger with background flushing in Go

concurrency buffering context
Intermediate 8 steps
python
from urllib.parse import urlparse
 
 
class RobotsRules:

Parsing and applying a robots.txt file

parsing longest-prefix-match state-machine
Intermediate 10 steps
rust
use std::net::{IpAddr, SocketAddr};
 
use axum::extract::{ConnectInfo, FromRequestParts};
use axum::http::request::Parts;

How a custom ClientIp extractor works in Axum

extractor http-headers proxy-forwarding
Intermediate 8 steps
java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Currency;
 

A safe Money value type in Java

immutability bigdecimal value-object
Intermediate 10 steps
javascript
'use client';
 
import { useRouter } from 'next/navigation';
import Link from 'next/link';

Archiving with router.refresh in Next.js

client-component data-mutation transitions
Intermediate 7 steps
ruby
class ThumbnailPool
  def initialize(worker_count: 4, capacity: 100)
    @queue = SizedQueue.new(capacity)
    @running = true

A thread pool for thumbnail jobs in Ruby

concurrency thread-pool bounded-queue
Advanced 7 steps