Code Explainers

Intermediate code explainers

ruby
class WeatherLookup
  Result = Struct.new(:temperature, :conditions, keyword_init: true)
 
  class TimeoutError < StandardError; end

A resilient weather API service object in Rails

service-object http-client error-handling
Intermediate 7 steps
rust
use axum::{
    extract::{Path, State},
    routing::{get, post},
    Json, Router,

Composing Axum apps from feature modules

trait objects modular routing shared state
Intermediate 8 steps
typescript
interface ParsedCookie {
  name: string;
  value: string;
  domain?: string;

Parsing a Set-Cookie header in TypeScript

parsing string-manipulation type-safety
Intermediate 9 steps
javascript
import { cache } from 'react'
import { notFound } from 'next/navigation'
import { db } from '@/lib/db'
 

Deduping data fetches with React cache in Next.js

request-memoization data-fetching server-components
Intermediate 6 steps
go
package payment
 
import (
	"regexp"

Masking card numbers in Go

regex string-processing redaction
Intermediate 6 steps
java
public enum DataSourceType {
    PRIMARY,
    REPLICA
}

Read/write datasource routing in Spring

routing threadlocal read-replica
Intermediate 8 steps
javascript
class LRUCache {
  constructor(capacity) {
    if (capacity < 1) throw new RangeError('capacity must be positive');
    this.capacity = capacity;

Building an LRU cache with a JS Map

lru-cache data-structures insertion-order
Intermediate 8 steps
go
package slug
 
import (
	"regexp"

Turning titles into URL slugs in Go

unicode-normalization regexp string-processing
Intermediate 8 steps
ruby
class Post < ApplicationRecord
  belongs_to :author, counter_cache: false
  has_many :comments, dependent: :destroy
end

A custom counter cache in Rails

counter-cache callbacks associations
Intermediate 7 steps
php
<?php
 
namespace App\Http\Middleware;
 

A scheduled maintenance-window middleware in Laravel

middleware caching http-responses
Intermediate 7 steps
java
@ControllerAdvice
public class DeprecationHeaderAdvice implements ResponseBodyAdvice<Object> {
 
    private final Clock clock;

Adding deprecation headers in Spring

response interception http headers api deprecation
Intermediate 8 steps
python
from flask import Flask, g, current_app
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
 

Scoped SQLAlchemy sessions in Flask

database session-management connection-pooling
Intermediate 6 steps
javascript
import { useState } from 'react';
 
function TreeNode({ node, depth = 0 }) {
  const [expanded, setExpanded] = useState(depth === 0);

How a recursive file tree renders in React

recursion component-composition local-state
Intermediate 8 steps
go
package middleware
 
import (
	"github.com/gin-gonic/gin"

Injecting a scoped GORM query via Gin middleware

middleware dependency-injection request-context
Intermediate 9 steps
ruby
namespace :active_storage do
  desc "Purge blobs that have been unattached for more than 48 hours"
  task purge_stale: :environment do
    cutoff = 48.hours.ago

A Rake task to purge stale Active Storage blobs in Rails

rake active-storage batch-processing
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Building a profanity filter in PHP

regex text-processing immutability
Intermediate 6 steps
java
public List<Path> expandGlob(Path baseDir, String glob) throws IOException {
    FileSystem fs = baseDir.getFileSystem();
    PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
    boolean recursive = glob.contains("**");

Expanding glob patterns with NIO

glob-matching nio streams
Intermediate 5 steps
typescript
import { Component, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ProductRecommendationsComponent } from './product-recommendations.component';
 

Deferred loading with @defer in Angular

lazy-loading signals standalone-components
Intermediate 8 steps