Code Explainers

Intermediate code explainers

rust
use std::fmt;
use std::str::FromStr;
 
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

A validated Email newtype in Rust

newtype validation error-handling
Intermediate 9 steps
ruby
class Account < ApplicationRecord
  has_many :projects, dependent: :destroy_async
  has_many :memberships, dependent: :destroy_async
  has_many :invoices, dependent: :restrict_with_error

Safely closing an Account in Rails

associations callbacks transactions
Intermediate 9 steps
python
from datetime import date
from enum import Enum
from typing import Annotated
 

Reusable query params with Pydantic in FastAPI

query-parameters validation pagination
Intermediate 8 steps
typescript
import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, FormControl, Validators, ValidatorFn } from '@angular/forms';
 
interface FieldSchema {

Building dynamic reactive forms in Angular

reactive-forms dynamic-forms validation
Intermediate 8 steps
java
@WebMvcTest(OrderController.class)
class OrderControllerTest {
 
    @Autowired

Slicing the web layer with @WebMvcTest in Spring

testing mocking web-layer
Intermediate 9 steps
go
func (r *EventRepository) BatchInsert(ctx context.Context, events []Event) error {
	if len(events) == 0 {
		return nil
	}

Batch inserting rows in a Go transaction

transactions batching database
Intermediate 7 steps
php
<?php
 
namespace App\Http\Middleware;
 

Verifying webhook signatures in Laravel

hmac middleware webhooks
Intermediate 7 steps
javascript
const ESCAPE_MAP = {
  '&': '&amp;',
  '<': '&lt;',
  '>': '&gt;',

Escaping HTML before injecting user content

xss-prevention escaping dom
Intermediate 6 steps
ruby
require "digest"
 
class UploadDeduplicator
  CHUNK_SIZE = 64 * 1024

Deduplicating uploads by content hash

hashing deduplication streaming-io
Intermediate 7 steps
python
import csv
from pathlib import Path
 
 

Splitting a large CSV into smaller files

file-io streaming csv
Intermediate 8 steps
typescript
import { Controller, Get, Param, StreamableFile, Res, NotFoundException, Header } from '@nestjs/common';
import { Response } from 'express';
import { createReadStream, promises as fsp } from 'fs';
import { join } from 'path';

Streaming CSV file downloads in NestJS

file-streaming http-headers error-handling
Intermediate 7 steps
java
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
 
    @EntityGraph(attributePaths = {"customer", "lineItems", "lineItems.product"})

Killing N+1 queries with @EntityGraph in Spring

orm fetching n+1-problem
Intermediate 5 steps
go
package main
 
import (
	"html/template"

Auto-escaping HTML with Go templates

templating xss auto-escaping
Intermediate 6 steps
javascript
function memoize(fn, resolver) {
  const cache = new Map();
 
  function memoized(...args) {

Building a memoize wrapper in JavaScript

memoization closures higher-order-functions
Intermediate 7 steps
ruby
require "csv"
 
class Api::V1::ProductImportsController < Api::V1::BaseController
  MAX_ROWS = 10_000

Bulk CSV imports with upsert_all in Rails

bulk-insert csv-parsing upsert
Intermediate 8 steps
java
@RestController
@RequestMapping("/api/orders")
public class OrderController {
 

Content-negotiated API versioning in Spring

api-versioning content-negotiation rest
Intermediate 7 steps
rust
use std::time::Duration;
 
use axum::{
    body::Body,

Structured request tracing in Axum

middleware observability tracing
Intermediate 7 steps
javascript
import { useState, useCallback, useEffect } from 'react';
 
function usePaginatedProducts(pageSize = 20) {
  const [items, setItems] = useState([]);

Cursor-based infinite scroll with a React hook

custom-hooks pagination async-fetching
Intermediate 8 steps