Code Explainers

Browse the library

go
package store
 
import (
	"database/sql"

Wrapping and inspecting errors in Go

error-handling error-wrapping sentinel-errors
Intermediate 8 steps
php
<?php
 
namespace App\Support;
 

Building a URL slug from any title in PHP

regex transliteration string-processing
Intermediate 8 steps
python
from collections import deque
 
 
class RollingAverage:

A rolling average over a fixed window

sliding-window running-sum deque
Intermediate 7 steps
javascript
function groupBy(items, keySelector) {
  const resolveKey = typeof keySelector === 'function'
    ? keySelector
    : (item) => item[keySelector];

Building a flexible groupBy in JavaScript

higher-order-functions reduce data-transformation
Intermediate 6 steps
rust
use axum::{
    response::sse::{Event, KeepAlive, Sse},
    routing::get,
    Router,

Streaming live price ticks with SSE in Axum

server-sent-events broadcast-channel streaming
Advanced 8 steps
java
@Entity
@Table(name = "accounts")
public class Account {
 

Optimistic locking with @Version in Spring

optimistic-locking jpa concurrency
Advanced 7 steps
typescript
interface TokenBucketOptions {
  capacity: number;
  refillPerSecond: number;
}

How a token bucket rate limiter works

rate-limiting token-bucket lazy-evaluation
Intermediate 7 steps
go
package main
 
import (
	"context"

Graceful HTTP shutdown in Go

graceful-shutdown signals goroutines
Intermediate 8 steps
php
<?php
 
declare(strict_types=1);
 

Streaming file lines with a PHP generator

generators lazy-evaluation file-io
Intermediate 7 steps
ruby
class SessionsController < ApplicationController
  MAX_ATTEMPTS = 5
  THROTTLE_WINDOW = 15.minutes
 

Throttling failed logins in Rails

rate-limiting authentication caching
Intermediate 7 steps
python
from collections import OrderedDict
from typing import Callable, Hashable, Iterable, Iterator, TypeVar
 
T = TypeVar("T")

Two ways to dedupe while keeping order

deduplication generators ordered-data
Intermediate 7 steps
javascript
function makeReorderableList(listEl, onReorder) {
  let draggedItem = null;
 
  listEl.addEventListener('dragstart', (e) => {

Drag-to-reorder lists with the HTML5 drag API

drag-and-drop event-delegation dom-manipulation
Intermediate 8 steps
go
package handlers
 
type SignupRequest struct {
	Email           string `json:"email" binding:"required,email"`

Validated signup handler in Gin

request validation error handling http status codes
Intermediate 7 steps
python
from django.db.models import Sum, Count, F, DecimalField
from django.db.models.functions import TruncMonth, Coalesce
 
from .models import Order

Build a monthly revenue report in Django

aggregation orm group-by
Advanced 7 steps
rust
use std::net::SocketAddr;
use std::time::Duration;
 
use axum::{routing::get, Json, Router};

Per-IP rate limiting in Axum with tower-governor

rate-limiting middleware tower
Intermediate 7 steps
php
<?php
 
namespace App\Providers;
 

Binding a payment gateway in Laravel

dependency-injection service-container interface-binding
Intermediate 7 steps
ruby
class ProductsController < ApplicationController
  def index
    @products = Product
      .includes(:category, :brand)

Building a safe filterable product index in Rails

query objects scopes strong parameters
Intermediate 8 steps
java
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.math.BigInteger;
 

A thread-safe memoized factorial cache

memoization thread-safety caching
Intermediate 6 steps