Code Explainers

Browse the library

java
public class OrderSerializer {
 
    private final ObjectMapper mapper;
 

Configuring a reusable Jackson ObjectMapper

serialization json builder-pattern
Intermediate 8 steps
javascript
async function copyToClipboard(text) {
  if (navigator.clipboard && window.isSecureContext) {
    try {
      await navigator.clipboard.writeText(text);

Copying to the clipboard with a fallback

clipboard progressive-enhancement dom
Intermediate 8 steps
rust
use std::time::Duration;
use tokio::time::sleep;
 
#[derive(Debug)]

Async retry with exponential backoff in Rust

retry exponential-backoff async
Intermediate 8 steps
ruby
class ArticlesController < ApplicationController
  before_action :set_article, only: %i[show edit update destroy publish]
 
  def edit

Policy-based authorization in a Rails controller

authorization policy-object before-action
Intermediate 9 steps
java
package com.example.maintenance;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

How a scheduled cleanup job runs in Spring

scheduling cron transactions
Intermediate 6 steps
go
package handlers
 
import (
	"fmt"

Handling secure file uploads in Gin

file-upload validation security
Intermediate 7 steps
javascript
import { useEffect, useRef, useState } from "react";
 
export function Dropdown({ label, children }) {
  const [open, setOpen] = useState(false);

Closing a dropdown on outside click in React

outside-click event-listeners cleanup
Intermediate 8 steps
ruby
class ArticlesController < ApplicationController
  DEFAULT_LIMIT = 25
  MAX_LIMIT = 100
 

Cursor-based pagination in a Rails controller

pagination cursor query-building
Intermediate 7 steps
java
@Component
public class RateLimitingFilter extends OncePerRequestFilter {
 
    private static final int MAX_REQUESTS = 100;

How a rate-limiting filter works in Spring

rate limiting concurrency servlet filter
Advanced 8 steps
go
package server
 
import (
	"net/http"

Serving a single-page app with Gin

static-files spa routing
Intermediate 7 steps
typescript
interface PollOptions<T> {
  intervalMs?: number;
  timeoutMs?: number;
  signal?: AbortSignal;

A cancellable polling helper in TypeScript

polling async-await abortsignal
Intermediate 9 steps
rust
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
 

Recursive file search by extension in Rust

recursion filesystem error-handling
Intermediate 8 steps
java
public List<Employee> parseEmployees(Path csvPath) throws IOException {
    List<Employee> employees = new ArrayList<>();
 
    try (BufferedReader reader = Files.newBufferedReader(csvPath, StandardCharsets.UTF_8)) {

Parsing a CSV into typed objects in Java

csv-parsing file-io try-with-resources
Intermediate 7 steps
typescript
type Ok<T> = { ok: true; value: T };
type Err<E> = { ok: false; error: E };
export type Result<T, E> = Ok<T> | Err<E>;
 

A Result type for typed error handling

discriminated-union error-handling type-guards
Intermediate 8 steps
javascript
'use server'
 
import { z } from 'zod'
import { redirect } from 'next/navigation'

How a Next.js Server Action validates a form

server-actions form-validation zod
Intermediate 7 steps
javascript
const cache = new Map();
const inflight = new Map();
 
async function fetchResults(query) {

Building a typeahead with an LRU cache

caching lru-eviction request-deduplication
Intermediate 9 steps
ruby
module ConfigMerge
  module_function
 
  def deep_merge(base, override)

Recursively merging nested config hashes in Ruby

recursion hash-merge immutability
Intermediate 8 steps
java
public final class RetryExecutor {
 
    private final int maxAttempts;
    private final Duration initialDelay;

Exponential backoff retry in Java

retry exponential-backoff jitter
Intermediate 8 steps