Code Explainers

Browse the library

typescript
type RetryableRequest<T> = () => Promise<T>;
 
interface RetryOptions {
  maxAttempts?: number;

Retrying HTTP requests with backoff

retry exponential-backoff error-handling
Intermediate 8 steps
java
public class AccountRepository {
 
    private final DataSource dataSource;
 

Optimistic locking with a version column in JDBC

optimistic-locking jdbc concurrency
Intermediate 8 steps
python
from flask import Flask, request, Response
from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST
import time
 

Instrumenting a Flask app with Prometheus

metrics observability middleware
Intermediate 6 steps
ruby
class WeightedMovingAverageForecaster
  DEFAULT_WINDOW = 5
 
  def initialize(window: DEFAULT_WINDOW, weights: nil)

A weighted moving average forecaster in Ruby

forecasting weighted-average normalization
Intermediate 8 steps
go
package dedup
 
import (
	"crypto/sha256"

Finding duplicate files by content hash in Go

hashing filesystem deduplication
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Calculating a cart total with a Money type

value-objects money-arithmetic domain-logic
Intermediate 7 steps
javascript
class DownloadTracker {
  constructor(totalBytes, { windowMs = 3000 } = {}) {
    this.totalBytes = totalBytes;
    this.windowMs = windowMs;

Estimating download speed with a sliding window

sliding-window getters rate-calculation
Intermediate 7 steps
typescript
import { Directive, ElementRef, EventEmitter, NgZone, OnDestroy, OnInit, Output } from '@angular/core';
 
interface Point {
  x: number;

Throttling pointer events with NgZone in Angular

change-detection requestanimationframe event-throttling
Advanced 8 steps
java
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 

Rendering ${} templates with regex in Java

regex string-templating matcher-api
Intermediate 8 steps
python
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import (
    DataRequired,

Building a signup form with Flask-WTF

form validation declarative fields validators
Intermediate 7 steps
typescript
import {
  MiddlewareConsumer,
  Module,
  NestModule,

How middleware binds to routes in NestJS

middleware routing modules
Intermediate 6 steps
javascript
import { useState, useEffect, useRef } from 'react';
 
export function SectionSearch({ sections }) {
  const [query, setQuery] = useState(() => decodeURIComponent(window.location.hash.slice(1)));

Syncing search state with the URL hash in React

debouncing url-state useeffect
Intermediate 7 steps
python
from django.db import models
 
 
class Order(models.Model):

Tracking field changes in a Django model

change-tracking audit-log model-lifecycle
Intermediate 8 steps
typescript
interface Reading {
  timestamp: number;
  value: number;
}

A weighted moving average in TypeScript

moving-average sliding-window smoothing
Intermediate 6 steps
javascript
import OpenAI from 'openai';
 
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
 

Streaming OpenAI tokens from a Next.js edge route

streaming edge-runtime readablestream
Advanced 9 steps
python
from fastapi import APIRouter, Depends, HTTPException, status
 
from app.config import settings
from app.dependencies import get_current_user

Gating a FastAPI route behind a feature flag

feature-flags dependency-injection authorization
Intermediate 7 steps
typescript
export interface FormatBytesOptions {
  decimals?: number;
  binary?: boolean;
}

Formatting byte counts into human-readable sizes

formatting logarithms default-parameters
Intermediate 8 steps
javascript
class Carousel {
  constructor(root, { interval = 5000 } = {}) {
    this.root = root;
    this.track = root.querySelector('.carousel__track');

Building an accessible auto-playing carousel

dom state-management timers
Intermediate 9 steps