Code Explainers

Browse the library

java
@Configuration
public class DataLoaderConfig {
 
    @Bean

Batching GraphQL queries with DataLoaders in Spring

graphql batching n+1-problem
Advanced 8 steps
php
<?php
 
namespace App\Http\Requests;
 

Validating nested order payloads in Laravel

validation authorization nested-data
Intermediate 8 steps
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
python
import base64
import json
from flask import Blueprint, request, jsonify, url_for, abort
from sqlalchemy import and_, or_

Cursor pagination in a Flask API

cursor-pagination keyset-pagination rest-api
Advanced 9 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
rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    Json,

Distinguishing absent from null in an Axum PATCH

partial-update serde nullable-fields
Advanced 8 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
typescript
export function formatDuration(ms: number): string {
  if (!Number.isFinite(ms) || ms < 0) {
    return "00:00";
  }

Formatting milliseconds as a clock string

formatting integer-math guard-clause
Beginner 4 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