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 ServiceControlPolicy { | |
private SpannerDatabase spannerDB; | |
private QuotaManager quotaManager; | |
public void applyPolicyChange(PolicyChange change) { | |
if (change == null) { | |
// Assuming it comes from an external API | |
// Beyond your control | |
change = new NullPolicyChange(); | |
} |
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 ServiceControlPolicy { | |
private SpannerDatabase spannerDB; | |
private QuotaManager quotaManager; | |
public void applyPolicyChange(PolicyChange change) { | |
// NULL POINTER: change can be null | |
Policy policy = spannerDB.getPolicy(change.getPolicyId()); | |
// NULL POINTER: policy can be null from the database | |
String quotaField = policy.getQuotaField(); | |
// NULL POINTER: quotaField can be null (blank field) |
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
def find_minimum_price(products): | |
min_price = float('inf') | |
for product in products: | |
if product.price < min_price: | |
# This is an essential IF, you should not remove it | |
min_price = product.price | |
# No accidental IF here (if min_price is None:) | |
return min_price if min_price != float('inf') else None |
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
def find_minimum_price(products): | |
min_price = None | |
for product in products: | |
if min_price is None: | |
min_price = product.price | |
elif product.price < min_price: | |
min_price = product.price | |
return min_price |
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); |
NewerOlder