Code Explainers

Browse the library

typescript
import { Injectable } from '@angular/core';
import {
  HttpInterceptor,
  HttpHandler,

Refreshing tokens with an Angular HttpInterceptor

http-interceptor token-refresh rxjs
Advanced 8 steps
go
package ratelimit
 
import (
	"context"

A token bucket rate limiter in Go

rate-limiting concurrency goroutines
Intermediate 7 steps
rust
use std::num::ParseIntError;
 
fn parse_line(line: &str) -> Result<Vec<i64>, ParseIntError> {
    line.split(',')

Collecting Results in Rust iterators

iterators error-handling result
Intermediate 7 steps
python
from django.core.cache import cache
from django.db.models import Count, Sum, Q
from django.utils import timezone
 

Caching a Django dashboard aggregate query

caching aggregation conditional-queries
Intermediate 7 steps
ruby
class BulkImporter
  BATCH_SIZE = 500
 
  def initialize(records)

Batching bulk inserts in Rails

batching bulk-insert deduplication
Intermediate 5 steps
php
<?php
 
namespace Database\Seeders;
 

Building blog test data with Laravel factories

seeding factories relationships
Intermediate 9 steps
java
import java.util.List;
import java.util.IntSummaryStatistics;
 
public class OrderReport {

Aggregating stats with IntSummaryStatistics

streams aggregation records
Intermediate 5 steps
typescript
type StorageSchema = Record<string, unknown>;
 
interface StorageOptions {
  namespace?: string;

A type-safe wrapper around localStorage

generics type-safety serialization
Intermediate 9 steps
go
package handlers
 
type BookURI struct {
	Collection string `uri:"collection" binding:"required,alphanum"`

Validating URI params in a Gin handler

input validation struct binding error handling
Intermediate 7 steps
rust
use std::sync::Arc;
 
use axum::{
    extract::State,

Offloading work with a channel in Axum

background-jobs message-passing shared-state
Intermediate 9 steps
python
import time
from contextlib import contextmanager
 
 

A reusable timing context manager in Python

context-manager timing generators
Intermediate 5 steps
javascript
const DEFAULT_OPTIONS = {
  method: 'GET',
  timeout: 5000,
  retries: 3,

A resilient fetch wrapper with retries and timeout

fetch retry timeout
Intermediate 8 steps
ruby
class Credentials
  SENSITIVE = %i[password api_key secret_token access_key].freeze
  REDACTED = "[REDACTED]".freeze
 

Redacting secrets in Ruby object output

serialization introspection security
Intermediate 7 steps
java
public class OrderRepository {
 
    private static final int BATCH_SIZE = 500;
    private static final String INSERT_SQL =

Batched JDBC inserts with transaction safety

jdbc batching transactions
Intermediate 8 steps
typescript
type Fetcher<T> = (key: string) => Promise<T>;
 
export class RequestDeduplicator<T> {
  private inFlight = new Map<string, Promise<T>>();

Deduplicating in-flight requests in TypeScript

deduplication promises caching
Intermediate 7 steps
go
package cache
 
import (
	"context"

Cache-aside reads with singleflight in Go

caching singleflight concurrency
Advanced 8 steps
ruby
module Multitenant
  extend ActiveSupport::Concern
 
  included do

How a multitenant concern scopes Rails models

multitenancy default-scope concern
Advanced 8 steps
ruby
module Api
  module V1
    class BaseController < ActionController::API
      rescue_from StandardError, with: :render_internal_error

Centralized API error handling in Rails

error-handling rescue-from json-api
Intermediate 7 steps