Code Explainers

Browse the library

python
import re
import unicodedata
 
_SLUG_STRIP = re.compile(r"[^\w\s-]")

Building URL-safe slugs in Python

string-processing regex unicode-normalization
Intermediate 7 steps
java
package com.example.orders.repository;
 
import com.example.orders.domain.Order;
import org.springframework.data.jpa.repository.JpaRepository;

Projection interfaces in Spring Data JPA

projections jpql aggregation
Intermediate 7 steps
typescript
type Task<T> = () => Promise<T>;
 
export class Mutex {
  private queue: Array<() => void> = [];

Building an async mutex in TypeScript

concurrency promises locking
Advanced 7 steps
php
<?php
 
function parseMultipartEmail(string $raw): array
{

Parsing multipart emails with mailparse

mime-parsing email encoding
Intermediate 9 steps
ruby
class PaymentMethod < ApplicationRecord
  belongs_to :account
 
  encrypts :card_number, deterministic: false

How Rails encrypts sensitive payment fields

encryption validation scopes
Intermediate 9 steps
rust
use std::collections::HashMap;
 
#[derive(Debug, Clone)]
struct Row {

Building a tree from flat parent-id rows in Rust

recursion hashmap tree-building
Intermediate 6 steps
go
package middleware
 
import (
	"net/http"

Per-IP rate limiting middleware in Gin

rate-limiting token-bucket concurrency
Intermediate 7 steps
python
import os
import tarfile
from pathlib import Path
 

Safely compressing and extracting tar.gz archives

file-io compression path-traversal
Intermediate 6 steps
java
@RefreshScope
@Component
public class FeatureSettings {
 

Refreshable feature settings in Spring

caching configuration dependency-injection
Intermediate 7 steps
typescript
import { useEffect, useRef, useState } from "react";
 
export function useClickOutside<T extends HTMLElement>() {
  const ref = useRef<T>(null);

A dismiss-on-outside-click hook in React

custom-hooks event-listeners cleanup
Intermediate 7 steps
php
<?php
 
namespace App\Resilience;
 

How a circuit breaker guards a flaky service

circuit-breaker resilience caching
Intermediate 9 steps
ruby
require "zlib"
require "stringio"
require "base64"
 

Compress-and-encode payloads in Ruby

compression encoding io-streams
Intermediate 8 steps
go
package middleware
 
import (
	"net/http"

Building a CORS middleware in Gin

cors middleware closures
Intermediate 9 steps
rust
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use chrono::{Duration, Utc};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc};

Refresh token rotation in Axum

authentication token-rotation reuse-detection
Advanced 9 steps
javascript
const express = require('express');
const router = express.Router();
const db = require('../db');
 

Building a paginated articles endpoint in Express

pagination input-validation async-await
Intermediate 8 steps
python
from collections import OrderedDict
from typing import Any, Hashable, Optional
 
 

Building an LRU cache with OrderedDict

caching eviction data-structures
Intermediate 6 steps
typescript
import { Injectable, NotFoundException } from '@nestjs/common';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

Decoupling side effects with NestJS events

event-driven decoupling dependency-injection
Intermediate 7 steps
java
public final class ZipExtractor {
 
    private final Path targetDir;
 

Extracting a zip safely in Java

path-traversal streams file-io
Intermediate 8 steps