Code Explainers

Browse the library

go
func UpdateArticle(store *ArticleStore) gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Param("id")
 

Optimistic concurrency with If-Match in Gin

optimistic-locking http-preconditions etag
Intermediate 9 steps
python
from enum import Enum
from typing import Annotated
 
from fastapi import FastAPI, Query, Request, status

Custom validation error responses in FastAPI

validation error-handling enums
Intermediate 8 steps
javascript
function collapseConsecutiveLogs(lines, { keyFn = (l) => l.message } = {}) {
  const groups = [];
 
  for (const line of lines) {

Collapsing consecutive log lines in JavaScript

grouping run-length-encoding data-transformation
Intermediate 7 steps
rust
use axum::{
    extract::FromRequestParts,
    http::{request::Parts, HeaderMap, StatusCode},
};

Building a replay-guard extractor in Axum

custom-extractor replay-protection shared-state
Advanced 8 steps
typescript
import { Directive, Input, HostBinding, HostListener } from '@angular/core';
import { NG_VALIDATORS, Validator, AbstractControl, ValidationErrors } from '@angular/forms';
 
@Directive({

A custom range validator directive in Angular

form-validation directives accessibility
Intermediate 8 steps
php
<?php
 
namespace App\Models;
 

Storing JSON preferences in a Laravel model

eloquent casting json
Intermediate 7 steps
ruby
class AdminConstraint
  def self.matches?(request)
    new(request).matches?
  end

How routing constraints gate admin routes in Rails

routing authorization constraints
Intermediate 8 steps
java
package com.example.api.config;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

Configuring CORS in Spring MVC

cors configuration dependency-injection
Intermediate 6 steps
go
type Counter struct {
	counts sync.Map
}
 

A concurrent counter with sync.Map in Go

concurrency atomics sync.map
Advanced 8 steps
python
from fastapi import Depends, FastAPI, Header, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
 

Chaining tenant dependencies in FastAPI

dependency injection multi-tenancy authentication
Intermediate 7 steps
javascript
const express = require('express');
const crypto = require('crypto');
const router = express.Router();
 

Optimistic locking with ETags in Express

optimistic-locking etag conditional-requests
Intermediate 7 steps
rust
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;

Opaque pagination cursors in Rust

pagination serialization base64
Intermediate 8 steps
typescript
import { useCallback, useRef, useState } from "react";
 
type Tag = { id: string; label: string };
 

A debounced, abortable tag-input hook in React

custom-hooks debounce abort-controller
Advanced 10 steps
php
<?php
 
declare(strict_types=1);
 

Counting business days in PHP

dates immutability hash-lookup
Intermediate 7 steps
ruby
require "openssl"
require "base64"
 
module SecureToken

Authenticated encryption with AES-GCM in Ruby

encryption aes-gcm authentication
Advanced 8 steps
java
@Configuration
public class PaymentGatewayClientConfig {
 
    @Bean

Building a resilient RestClient in Spring

retry http-client backoff
Intermediate 9 steps
go
package handlers
 
import (
	"net/http"

Custom struct-level validation in Gin

validation struct-tags cross-field
Intermediate 7 steps
python
from django.db import models
from django.db.models import F, Sum, DecimalField
from django.db.models.functions import Coalesce
 

Recomputing order totals in Django

annotations aggregation custom-manager
Intermediate 9 steps