Code Explainers

Browse the library

ruby
class SessionsController < ApplicationController
  MAX_ATTEMPTS = 5
  THROTTLE_WINDOW = 15.minutes
 

Throttling failed logins in Rails

rate-limiting authentication caching
Intermediate 7 steps
python
from collections import OrderedDict
from typing import Callable, Hashable, Iterable, Iterator, TypeVar
 
T = TypeVar("T")

Two ways to dedupe while keeping order

deduplication generators ordered-data
Intermediate 7 steps
javascript
function makeReorderableList(listEl, onReorder) {
  let draggedItem = null;
 
  listEl.addEventListener('dragstart', (e) => {

Drag-to-reorder lists with the HTML5 drag API

drag-and-drop event-delegation dom-manipulation
Intermediate 8 steps
go
package handlers
 
type SignupRequest struct {
	Email           string `json:"email" binding:"required,email"`

Validated signup handler in Gin

request validation error handling http status codes
Intermediate 7 steps
python
from django.db.models import Sum, Count, F, DecimalField
from django.db.models.functions import TruncMonth, Coalesce
 
from .models import Order

Build a monthly revenue report in Django

aggregation orm group-by
Advanced 7 steps
rust
use std::net::SocketAddr;
use std::time::Duration;
 
use axum::{routing::get, Json, Router};

Per-IP rate limiting in Axum with tower-governor

rate-limiting middleware tower
Intermediate 7 steps
php
<?php
 
namespace App\Providers;
 

Binding a payment gateway in Laravel

dependency-injection service-container interface-binding
Intermediate 7 steps
ruby
class ProductsController < ApplicationController
  def index
    @products = Product
      .includes(:category, :brand)

Building a safe filterable product index in Rails

query objects scopes strong parameters
Intermediate 8 steps
java
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.math.BigInteger;
 

A thread-safe memoized factorial cache

memoization thread-safety caching
Intermediate 6 steps
typescript
export function chunk<T>(items: readonly T[], size: number): T[][] {
  if (size <= 0 || !Number.isInteger(size)) {
    throw new RangeError(`chunk size must be a positive integer, got ${size}`);
  }

Splitting work into sequential batches in TypeScript

generics async-await batching
Intermediate 5 steps
go
package handlers
 
import (
	"net/http"

Cookie-based sessions in Gin

authentication cookies middleware
Intermediate 7 steps
python
from pathlib import Path
 
 
def print_tree(root, prefix="", show_hidden=False):

Printing a directory tree with recursion

recursion filesystem sorting
Intermediate 6 steps
php
<?php
 
namespace App\Http\Controllers\Auth;
 

How a Laravel registration endpoint works

validation database-transactions events
Intermediate 8 steps
ruby
module LogParser
  class AccessLogStreamer
    SEVERITY_PATTERN = /\b(ERROR|WARN|FATAL)\b/
 

Streaming log lines with a Ruby enumerator

enumerators lazy-iteration regex-matching
Intermediate 6 steps
java
@RestController
@RequestMapping("/webhooks/stripe")
public class StripeWebhookController {
 

How a Stripe webhook controller works in Spring

webhooks signature-verification event-handling
Intermediate 7 steps
typescript
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError, timer } from 'rxjs';
import { mergeMap, retryWhen } from 'rxjs/operators';

Exponential backoff retries in Angular

retry exponential-backoff rxjs
Advanced 7 steps
javascript
import Papa from 'papaparse';
 
const MAX_SIZE = 5 * 1024 * 1024;
 

Validating and parsing CSV uploads in the browser

promises csv-parsing validation
Intermediate 8 steps
go
package validators
 
import (
	"regexp"

Custom validators in Gin

validation sync.once regexp
Intermediate 5 steps