Code Explainers

Code explainers tagged #immutability

go
package logging
 
import (
	"context"

Deduplicating log attributes in Go's slog

decorator-pattern structured-logging immutability
Intermediate 8 steps
php
<?php
 
declare(strict_types=1);
 

Counting business days in PHP

dates immutability hash-lookup
Intermediate 7 steps
php
<?php
 
namespace App\Reports;
 

Bucketing spending by time period in Laravel

aggregation date-bucketing collections
Intermediate 9 steps
ruby
class EditorHistory
  MAX_DEPTH = 100
 
  def initialize(initial = "")

How undo/redo history works in Ruby

undo-redo state-management immutability
Intermediate 9 steps
java
public final class PrefixSearch {
 
    private final String[] words;
 

Prefix search with binary search bounds

binary-search sorting range-query
Intermediate 9 steps
java
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
public class AuditingConfig {
 

Automatic audit trails with Spring Data JPA

jpa-auditing security-context entity-listeners
Intermediate 7 steps
php
<?php
 
namespace App\Services;
 

Building a profanity filter in PHP

regex text-processing immutability
Intermediate 6 steps
php
<?php
 
namespace App\Services;
 

Calculating a cart total with a Money type

value-objects money-arithmetic domain-logic
Intermediate 7 steps
java
import java.util.Arrays;
import java.util.List;
 
public final class LatencyStats {

Computing latency percentiles in Java

percentiles interpolation immutability
Intermediate 9 steps
typescript
import { InjectionToken, inject, Provider, isDevMode } from '@angular/core';
import { WINDOW } from './window.token';
 
export interface AnalyticsConfig {

Layered config with an Angular InjectionToken

dependency-injection configuration factory-provider
Intermediate 8 steps
php
final class InvoiceCalculator
{
    private const SCALE = 4;
 

Precise money math with PHP's BCMath

bcmath arbitrary precision money
Intermediate 8 steps
javascript
import { useReducer, useCallback } from 'react';
 
function historyReducer(state, action) {
  const { past, present, future } = state;

Undo/redo form state with a React reducer

undo-redo reducer immutability
Intermediate 10 steps
java
import java.util.HashSet;
import java.util.Set;
 
public final class CollectionDiff<T> {

Diffing two collections with set operations

set-operations immutability generics
Intermediate 8 steps
java
public record AppConfig(
        String host,
        int port,
        String databaseUrl,

Loading typed config from env vars in Java

records configuration environment-variables
Intermediate 6 steps
java
public final class CaseConverter {
 
    private static final Pattern CAMEL_BOUNDARY =
            Pattern.compile("([a-z0-9])([A-Z])|([A-Z]+)([A-Z][a-z])");

Converting camelCase to snake_case in Java

regex string-manipulation utility-class
Intermediate 7 steps
java
public final class EmailNormalizer {
 
    private static final Pattern EMAIL_PATTERN = Pattern.compile(
        "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"

Normalizing email addresses in Java

validation regex normalization
Intermediate 8 steps
php
<?php
 
namespace App\Http\Requests\DataObjects;
 

Typed request DTOs in Laravel

data-transfer-object validation immutability
Intermediate 6 steps
ruby
class KeyTransformer
  def self.camelize(data)
    new.camelize(data)
  end

Recursively camelizing nested Ruby data

recursion data-transformation pattern-matching
Intermediate 7 steps