Code Explainers
Code explainers tagged #immutability
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
java
public record FixedWidthField(String name, int start, int end) { public String extract(String line) { int from = Math.min(start, line.length());
Parsing fixed-width text in Java
records
parsing
streams
Intermediate
7 steps
php
<?php declare(strict_types=1);
An immutable typed collection in PHP
immutability
value-objects
iterators
Intermediate
8 steps
java
package com.example.web.convert; import java.time.LocalDate; import java.time.format.DateTimeFormatter;
A custom ISO-week Converter in Spring
type conversion
value object
immutability
Intermediate
5 steps
typescript
type EditorState = { content: string; cursor: number; };
Undo/redo with two stacks in TypeScript
undo-redo
stacks
state-management
Intermediate
7 steps
javascript
import { createContext, useContext, useReducer } from 'react'; const CartContext = createContext(null);
Building a shopping cart with Context in React
state management
reducer
context
Intermediate
10 steps
python
import os from dataclasses import dataclass from functools import lru_cache
Loading typed config from environment variables
configuration
environment-variables
type-coercion
Intermediate
6 steps
php
final class ConfigResolver { private array $merged;
Layered config merging in PHP
recursion
immutability
variadics
Intermediate
8 steps
typescript
type Change = | { kind: "added"; path: string; value: unknown } | { kind: "removed"; path: string; value: unknown } | { kind: "updated"; path: string; from: unknown; to: unknown };
Building a recursive deep-diff in TypeScript
recursion
discriminated-unions
type-guards
Intermediate
9 steps
java
public final class ShippingAddress { private final String recipient; private final String line1; private final String line2;
Building immutable objects with the Builder pattern
builder-pattern
immutability
validation
Intermediate
7 steps
java
import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter;
Converting timestamps across time zones in Java
date-time
time-zones
instant
Intermediate
6 steps
typescript
import { Injectable, Scope, Inject } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { Request } from 'express';
Request-scoped context service in NestJS
dependency-injection
request-scope
immutability
Intermediate
8 steps
java
public class OrderSerializer { private final ObjectMapper mapper;
Configuring a reusable Jackson ObjectMapper
serialization
json
builder-pattern
Intermediate
8 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
ruby
module DeepFreeze module_function def call(obj)
Recursively freezing nested Ruby data
recursion
immutability
pattern matching
Intermediate
9 steps
java
public record ServerConfig(String host, int port, boolean tls, List<String> allowedOrigins) { public ServerConfig { Objects.requireNonNull(host, "host is required");
Validating config with a Java record
records
validation
immutability
Intermediate
9 steps
javascript
export function cloneState(state) { if (typeof structuredClone !== "function") { throw new Error("structuredClone is not available in this runtime"); }
Deep cloning with structuredClone
deep-copy
error-handling
immutability
Intermediate
7 steps
java
package com.example.billing; import java.math.BigDecimal; import java.math.RoundingMode;
Locale-aware currency formatting in Java
internationalization
rounding
immutability
Intermediate
6 steps