Code Explainers

Browse the library

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
php
<?php
 
final class UserRepository
{

A safe PDO user repository in PHP

prepared-statements repository-pattern sql-injection
Intermediate 7 steps
rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;
 
pub fn top_k<T: Ord + Clone>(items: &[T], k: usize) -> Vec<T> {

Top-K selection with a bounded min-heap in Rust

heap top-k generics
Intermediate 8 steps
ruby
class ReportGenerator
  def initialize(account, period)
    @account = account
    @period = period

Memoized report metrics in Ruby in Rails

memoization query-composition service-object
Intermediate 7 steps
java
@RestController
@RequestMapping("/api/users")
public class UserController {
 

How a Spring REST controller maps users

rest-api dependency-injection dto-mapping
Intermediate 7 steps
typescript
import {
  createParamDecorator,
  ExecutionContext,
  UnauthorizedException,

Building a @CurrentUser decorator in NestJS

decorators authentication request-context
Intermediate 6 steps
python
import re
from collections import defaultdict
from pathlib import Path
 

Summarizing log files by date in Python

regex parsing aggregation
Intermediate 7 steps