Code Explainers

Intermediate code explainers

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
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
rust
use axum::{
    body::Body,
    extract::Request,
    http::{header::HeaderValue, HeaderName},

Request ID middleware in Axum

middleware tracing request-extensions
Intermediate 7 steps
php
<?php
 
namespace App\Http\Controllers;
 

Handling avatar uploads in Laravel

file-upload image-processing validation
Intermediate 8 steps
ruby
class HostnameValidator < ActiveModel::EachValidator
  MAX_LENGTH = 253
  LABEL = /\A(?!-)[a-z0-9-]{1,63}(?<!-)\z/i
 

Writing a custom hostname validator in Rails

validation custom validator regex
Intermediate 8 steps
ruby
class Survey < ApplicationRecord
  has_many :questions, dependent: :destroy
  has_many :respondents, dependent: :nullify
 

Nested survey forms in Rails

nested-attributes validations associations
Intermediate 9 steps
php
<?php
 
namespace App\Security;
 

Secure password hashing with Argon2id in PHP

password-hashing argon2id timing-attacks
Intermediate 8 steps
python
import time
import threading
 
 

How a thread-safe token bucket rate limiter works

rate-limiting concurrency locking
Intermediate 6 steps
go
package logfmt
 
import (
	"bytes"

Pooling buffers for logfmt encoding in Go

object-pooling memory-reuse serialization
Intermediate 8 steps
typescript
import { z } from "zod";
 
const ProductQuerySchema = z.object({
  page: z.coerce.number().int().positive().default(1),

Validating URL query params with Zod

validation schema type-inference
Intermediate 8 steps