Code Explainers

Intermediate code explainers

php
<?php
 
namespace App\Http\Controllers;
 

A cached autocomplete endpoint in Laravel

caching validation query-ranking
Intermediate 8 steps
typescript
interface UserAgentInfo {
  browser: { name: string; version: string };
  os: { name: string; version: string };
  device: 'mobile' | 'tablet' | 'desktop';

Parsing a user-agent string with ordered rules

regex parsing pattern-matching
Intermediate 9 steps
javascript
const express = require('express');
const router = express.Router();
const db = require('../db');
 

Building a paginated orders page in Express

pagination routing sql-queries
Intermediate 7 steps
go
package middleware
 
import (
	"context"

Per-tenant daily rate limiting in Gin

rate-limiting middleware redis
Intermediate 8 steps
java
public List<DailySample> backfillMissingDates(List<DailySample> samples, double fillValue) {
    if (samples.isEmpty()) {
        return List.of();
    }

Backfilling gaps in a daily time series in Java

streams time-series treemap
Intermediate 5 steps
rust
use axum::{
    extract::Query,
    response::IntoResponse,
    Json,

Parsing query strings in Axum handlers

deserialization query-parameters defaults
Intermediate 7 steps
javascript
import { Suspense } from 'react';
import { searchProducts } from '@/lib/products';
import SearchInput from './search-input';
 

Streaming search results in a Next.js Server Component

server-components suspense streaming
Intermediate 8 steps
php
<?php
 
declare(strict_types=1);
 

Normalizing human names in PHP

unicode text-normalization transliteration
Intermediate 8 steps
python
from functools import wraps
 
from flask import Blueprint, abort, jsonify
from flask_login import current_user, login_required

Building an admin-only decorator in Flask

decorators authorization access-control
Intermediate 7 steps
ruby
class BackfillUsersFullName < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!
 
  BATCH_SIZE = 5_000

How to backfill a column safely in Rails

migrations batching backfill
Intermediate 7 steps
java
public final class Ulid {
 
    private static final char[] ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ".toCharArray();
    private static final SecureRandom RANDOM = new SecureRandom();

How to generate a ULID in Java

base32-encoding bit-manipulation identifiers
Intermediate 8 steps
typescript
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, timer, throwError } from 'rxjs';
import { switchMap, takeWhile, filter, take, catchError } from 'rxjs/operators';

Polling a job until it finishes in Angular

rxjs polling observables
Intermediate 7 steps
javascript
import { useState } from 'react';
 
function StarRating({ value = 0, max = 5, onChange, size = 24 }) {
  const [hovered, setHovered] = useState(null);

Building an accessible star rating in React

controlled-component accessibility state-management
Intermediate 8 steps
rust
use std::borrow::Cow;
 
pub fn escape_html(input: &str) -> Cow<'_, str> {
    let needs_escape = input

Zero-copy HTML escaping with Cow in Rust

clone-on-write zero-copy string-escaping
Intermediate 6 steps
php
<?php
 
namespace App\Http\Controllers;
 

Handling Stripe webhooks in Laravel

webhooks signature-verification dependency-injection
Intermediate 7 steps
python
from django.urls import path, include
 
app_name = "api"
 

How URL-namespaced API versioning works in Django

api versioning url routing namespaces
Intermediate 8 steps
ruby
class ProjectsController < ApplicationController
  def new
    @project = current_account.projects.build
    load_form_collections

The new/create pattern in a Rails controller

mvc strong-parameters tenant-scoping
Intermediate 7 steps
java
@RestController
@RequestMapping("/api/reports")
public class ReportController {
 

Header-driven endpoints in a Spring controller

rest api request headers dependency injection
Intermediate 7 steps