This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// user-api-v1.json - Version 1 (maintained) | |
{ | |
"id": 317, | |
"name": "Mr Nimbus", | |
"email": "[email protected]", | |
"nationalities": "Brazilian,Canadian,Oceanic" | |
} | |
// user-api-v2.json - Version 2 | |
// (new structure, backward compatible) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// user-api-v1.json - Original API response | |
{ | |
"id": 317, | |
"name": "Mr Nimbus", | |
"email": "[email protected]", | |
"nationalities": "Brazilian,Canadian,Oceanic" | |
} | |
// Later changed to this without versioning: | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use axum::{ | |
http::StatusCode, | |
response::Json, | |
routing::post, | |
Router, | |
}; | |
use serde_json::{json, Value}; | |
async fn process_payment( | |
Json(payload): Json<Value> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use axum::{ | |
http::StatusCode, | |
response::Json, | |
routing::post, | |
Router, | |
}; | |
use serde_json::{json, Value}; | |
async fn process_payment( | |
Json(payload): Json<Value> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShoppingCart { | |
private final List<Item> items = new ArrayList<>(); | |
// This version uses Optionals | |
// Not all programming languages support this feature | |
private Optional<Coupon> coupon = Optional.empty(); | |
public void addItem(Item item) { | |
items.add(item); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShoppingCart { | |
private List<Item> items = new ArrayList<>(); | |
// 1. Identify nullable optional attributes | |
// that could be collections | |
// 2. Replace single nullable objects with empty collections | |
private List<Coupon> coupons = new ArrayList<>(); | |
public void addItem(Item item) { | |
this.items.add(item); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShoppingCart { | |
private List<Item> items = new ArrayList<>(); | |
private Coupon coupon = null; | |
public void addItem(Item item) { | |
this.items.add(item); | |
} | |
public void redeemCoupon(Coupon coupon) { | |
this.coupon = coupon; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvoiceProcessor: | |
def __init__(self, billing_ledger): | |
self.billing_ledger = billing_ledger | |
def process_invoice(self, customer, amount): | |
# Pure business logic with proper domain objects | |
if customer.credit_limit < amount: | |
raise CreditLimitExceededException() | |
# Business calculations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvoiceProcessor: | |
def process_invoice(self, invoice_data, database): | |
# Business logic mixed with database access | |
customer = database.execute( | |
"SELECT * FROM customers WHERE id = ?", | |
invoice_data['customer_id'] | |
).fetchone() | |
if customer['credit_limit'] < invoice_data['amount']: | |
raise Exception("Credit limit exceeded") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Invoice { | |
// 1. Identify all public uses of sequential IDs | |
// in APIs, URLs, or UI elements | |
private string $customerName; | |
private array $items; | |
public function __construct( |