immutability

ruby
class Money
  include Comparable

  attr_reader :amount, :currency

  def initialize(amount, currency = 'USD')

Value objects for domain modeling

ruby value-objects domain-driven-design
by Sarah Mitchell 2 tabs
typescript
export interface Post {
  id: string;
  title: string;
  updatedAt: string;
}

Deduplicating Paginated API Results With a Normalized Entity Store and Reducer

react reducer normalization
by codesnips 3 tabs
php
<?php

namespace App\ValueObjects;

final readonly class NotificationSettings
{

Cast a JSON Column to an Immutable Value Object with a Custom Eloquent Cast in Laravel

laravel eloquent value-object
by codesnips 3 tabs
php
<?php

namespace App\Domain;

use InvalidArgumentException;

Serializing a Money Value Object with a Custom Symfony Normalizer

symfony serializer value-object
by codesnips 3 tabs
java
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Currency;
import java.util.Objects;

public final class Money {

Immutable Value Object with a Fluent Builder and build()-Time Validation in Java

builder-pattern immutability value-object
by codesnips 3 tabs
typescript
export interface CartItem {
  sku: string;
  name: string;
  unitPrice: number; // cents
  qty: number;
}

Shopping Cart Pricing with a useReducer State Machine and a Pure Discount Engine

react usereducer typescript
by codesnips 3 tabs
javascript
import { useReducer, useCallback } from 'react';

const initialState = (present) => ({ past: [], present, future: [] });

function historyReducer(state, action) {
  const { past, present, future } = state;

Build an Undo/Redo History Stack with a useHistory Reducer Hook in React

react hooks usereducer
by codesnips 3 tabs
ruby
class PriceBreakdown < Struct.new(:subtotal_cents, :discount_cents, :tax_cents, keyword_init: true)
  def initialize(*)
    super
    freeze
  end

Memoizing a Pricing Breakdown as an Immutable Value Object in Rails

rails memoization value-object
by codesnips 3 tabs
java
package com.example.billing.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration

Type-Safe Spring Boot Config with @ConfigurationProperties Records and Validation

spring-boot configuration validation
by codesnips 4 tabs
rust
use std::time::Duration;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    pub endpoint: String,
    pub timeout: Duration,

Lock-Free Config Reads With Arc-Swap and Copy-on-Write Snapshots in Rust

rust arc copy-on-write
by codesnips 3 tabs
java
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

import Transaction.Category;

Grouping and Summarizing Transactions with Java Stream Collectors

java streams collectors
by codesnips 3 tabs
typescript
export interface DocState {
  text: string;
  selection: number;
}

export interface Command<S> {

Undo/Redo in a React Text Editor with a Command-History Stack

react hooks undo-redo
by codesnips 3 tabs