Code Explainers

Browse the library

java
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
 
    @QueryHints(@QueryHint(name = HINT_FETCH_SIZE, value = "1000"))

Streaming large result sets in Spring Data JPA

streaming jpa memory-management
Advanced 6 steps
javascript
async function pollJobUntilComplete(jobId, { interval = 2000, timeout = 60000, signal } = {}) {
  const deadline = Date.now() + timeout;
 
  while (true) {

Polling a job until it finishes in JavaScript

polling async-await abortsignal
Intermediate 6 steps
ruby
class Throttle
  def initialize(interval)
    @interval = interval
    @mutex = Mutex.new

A thread-safe throttle in Ruby

concurrency rate-limiting mutex
Intermediate 7 steps
typescript
type CheckoutState = "cart" | "shipping" | "payment" | "review" | "confirmed";
 
type CheckoutEvent =
  | { type: "PROCEED" }

A typed checkout state machine in TypeScript

state-machine union-types type-safety
Intermediate 7 steps
go
package handlers
 
import (
	"net/http"

Rendering HTML pages with Gin handlers

http-handlers templating error-handling
Beginner 8 steps
rust
use axum::{
    body::Body,
    extract::Path,
    http::{header, StatusCode},

Streaming file downloads in Axum

streaming file-serving path-traversal
Intermediate 8 steps
java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

Three ways to deduplicate a list by key in Java

streams deduplication collectors
Intermediate 6 steps
php
<?php
 
function verifyJwt(string $token, string $secret): array
{

Verifying an HS256 JWT in PHP

jwt hmac authentication
Intermediate 9 steps
python
from fastapi import FastAPI, Request, status
from fastapi.responses import JSONResponse
 
app = FastAPI()

Custom exception handlers in FastAPI

exception handling error responses separation of concerns
Intermediate 7 steps
ruby
class Article < ApplicationRecord
  before_validation :generate_slug, on: :create
 
  validates :slug, presence: true, uniqueness: true,

How URL slugs work in Rails

callbacks validations slugs
Intermediate 7 steps
ruby
class CheckoutService
  Result = Struct.new(:success?, :order, :error, keyword_init: true)
 
  def self.call(...)

How a Rails checkout service object works

service object transactions result object
Intermediate 9 steps
ruby
class User < ApplicationRecord
  has_one_attached :avatar
 
  validate :acceptable_avatar

Validating and serving avatars with Active Storage in Rails

active storage file uploads validation
Intermediate 9 steps
ruby
class Article < ApplicationRecord
  belongs_to :author
 
  default_scope { where(deleted_at: nil) }

How scopes compose in Rails

scopes query-composition activerecord
Intermediate 9 steps
ruby
class Employee < ApplicationRecord
  validates :name, presence: true
  validates :salary, numericality: { greater_than: 0 }
 

How STI models share behavior in Rails

single-table-inheritance inheritance method-overriding
Intermediate 8 steps
ruby
class ArticlesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_article, only: %i[show edit update destroy]
  before_action :authorize_owner!, only: %i[edit update destroy]

How before_action filters guard a Rails controller

filters authorization callbacks
Intermediate 9 steps
ruby
class CreateCommentsWithCounterCache < ActiveRecord::Migration[7.1]
  def change
    create_table :posts do |t|
      t.string :title, null: false

How counter caches work in Rails

counter-cache migrations associations
Intermediate 6 steps
ruby
class ProductCatalog
  def featured_products
    Rails.cache.fetch("catalog/featured_products", expires_in: 12.hours) do
      Product.where(featured: true)

Caching patterns with Rails.cache.fetch

caching cache-keys expiry
Intermediate 5 steps
ruby
class RegistrationForm
  include ActiveModel::Model
  include ActiveModel::Attributes
 

How a Rails form object spans two models

form object validations transactions
Intermediate 7 steps