Code Explainers

Browse the library

rust
use axum::{
    extract::ws::{Message, WebSocket, WebSocketUpgrade},
    response::IntoResponse,
};

How an Axum WebSocket echo server works

websockets async streams protocol handling
Intermediate 8 steps
go
package handlers
 
import (
	"fmt"

Handling multipart profile uploads in Gin

form binding file upload validation
Intermediate 7 steps
typescript
import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,

A catch-all exception filter in NestJS

error-handling exception-filter http
Intermediate 8 steps
javascript
const DIVISIONS = [
  { amount: 60, unit: 'seconds' },
  { amount: 60, unit: 'minutes' },
  { amount: 24, unit: 'hours' },

Building a human-friendly timeAgo formatter

internationalization relative-time lookup-table
Intermediate 6 steps
ruby
module DeepFreeze
  module_function
 
  def call(obj)

Recursively freezing nested Ruby data

recursion immutability pattern matching
Intermediate 9 steps
python
from django.db.models import Prefetch, Count
from django.views.generic import ListView
 
from .models import Article, Comment

Building an efficient article list in Django

orm query-optimization prefetching
Intermediate 6 steps
java
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
 

A thread-safe LRU cache in Java

lru-cache concurrency linkedhashmap
Intermediate 7 steps
rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::{IntoResponse, Response},

Typed error handling in an Axum handler

error-handling extractors validation
Intermediate 9 steps
go
package metrics
 
import (
	"sync/atomic"

A lock-free request counter in Go

concurrency atomics lock-free
Intermediate 6 steps
typescript
import { Module, Injectable } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import * as Joi from 'joi';
 

Validated, typed config in NestJS

configuration validation dependency-injection
Intermediate 6 steps
javascript
export function createSearchClient(baseUrl) {
  let inFlight = null;
 
  async function search(query, { signal } = {}) {

Cancelling stale requests in a search client

closures abortcontroller async
Intermediate 8 steps
php
<?php
 
namespace App\Events;
 

How a broadcast event works in Laravel

broadcasting websockets events
Intermediate 5 steps
ruby
class Webhooks::StripeController < ApplicationController
  skip_before_action :verify_authenticity_token
  skip_before_action :authenticate_user!
 

How a Rails Stripe webhook controller works

webhooks signature verification event dispatch
Intermediate 7 steps
python
from django.db import models
from django.utils.text import slugify
 
 

Auto-generating unique slugs in Django

slugs orm model-methods
Intermediate 7 steps
java
public record ServerConfig(String host, int port, boolean tls, List<String> allowedOrigins) {
 
    public ServerConfig {
        Objects.requireNonNull(host, "host is required");

Validating config with a Java record

records validation immutability
Intermediate 9 steps
go
package middleware
 
import (
	"time"

Building a request logger middleware in Gin

middleware structured-logging closures
Intermediate 6 steps
typescript
type User = {
  id: number;
  email: string;
  roles: string[];

Validating API data with TypeScript type guards

type-guards runtime-validation type-narrowing
Intermediate 8 steps
python
from functools import lru_cache
import math
 
 

Memoizing number theory with lru_cache

memoization number-theory caching
Intermediate 8 steps