Code Explainers

Browse the library

typescript
import { useCallback, useRef, useState } from "react";
 
interface VirtualListProps<T> {
  items: T[];

How a virtualized list renders in React

virtualization windowing performance
Advanced 7 steps
javascript
const UNIT_MULTIPLIERS = {
  ms: 1,
  s: 1000,
};

Parsing CSS durations into milliseconds

parsing regular-expressions validation
Intermediate 6 steps
java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
 

Building a Spring MVC form controller

mvc form-handling validation
Intermediate 8 steps
go
func MergeSorted(a, b []int) []int {
	merged := make([]int, 0, len(a)+len(b))
	i, j := 0, 0
 

Merging two sorted slices in Go

two-pointers merge slices
Intermediate 6 steps
ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :user, :request_id, :user_agent, :ip_address
 
  resets { Time.zone = nil }

Per-request audit trails with Current in Rails

thread-local-state auditing concerns
Intermediate 8 steps
python
from collections import Counter
 
import numpy as np
from PIL import Image

Extracting a color palette with K-means

clustering image-processing kmeans
Intermediate 7 steps
rust
use serde_json::{Map, Value};
 
pub fn merge_config(base: &mut Value, override_with: Value) {
    match (base, override_with) {

Deep-merging JSON config layers in Rust

recursion json pattern-matching
Intermediate 9 steps
javascript
class RequestDeduplicator {
  constructor(fetcher) {
    this.fetcher = fetcher;
    this.inFlight = new Map();

Deduplicating in-flight requests in JavaScript

promises caching async
Intermediate 6 steps
go
package retry
 
import (
	"errors"

How a retry budget caps retries in Go

rate-limiting sliding-window concurrency
Intermediate 7 steps
ruby
class Category < ApplicationRecord
  belongs_to :parent, class_name: "Category", optional: true
  has_many :children,
           -> { order(:position) },

Modeling a self-referential category tree in Rails

self-referential-association recursion tree-structure
Intermediate 9 steps
java
public final class PrefixSearch {
 
    private final String[] words;
 

Prefix search with binary search bounds

binary-search sorting range-query
Intermediate 9 steps
php
<?php
 
namespace App\Http\Controllers;
 

Processing refunds safely in Laravel

dependency injection validation database transactions
Intermediate 8 steps
python
import re
 
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, PasswordField

Cleaning form input with Flask-WTF filters

form validation input sanitization filters
Intermediate 8 steps
typescript
import {
  createParamDecorator,
  ExecutionContext,
  UnauthorizedException,

A signed-cookie param decorator in NestJS

custom-decorators hmac timing-safe-comparison
Advanced 8 steps
rust
use axum::{
    extract::{Path, State},
    http::HeaderMap,
    response::{Html, IntoResponse},

Building an HTMX-aware toggle handler in Axum

extractors htmx sqlx
Intermediate 8 steps
javascript
import { NextResponse } from 'next/server';
import { readFile, stat } from 'node:fs/promises';
import { join, extname, normalize } from 'node:path';
import { createHash } from 'node:crypto';

Serving private assets from a Next.js route

path-traversal http-caching etag
Intermediate 6 steps
ruby
module WifiQrCode
  ESCAPE_CHARS = /([\\;,:"])/.freeze
 
  AUTH_TYPES = {

Building a Wi-Fi QR code payload in Ruby

string-formatting validation escaping
Intermediate 9 steps
java
public final class IniParser {
 
    private static final Pattern SECTION = Pattern.compile("\\[(.+?)\\]");
 

Parsing INI files in Java

parsing regex state-machine
Intermediate 10 steps