Skip to content

Instantly share code, notes, and snippets.

View rstraub's full-sized avatar

Roy Straub rstraub

View GitHub Profile
@rstraub
rstraub / preventing-bug-with-microtypes.kt
Created February 10, 2022 14:03
microtype bug prevention
data class Weight(val grams: Int)
data class Coffee(val type: Bean, val intensity: Int, val weight: Weight)
fun bugPrevention() = println(
// Doesn't Compile, integer is not a Weight
// Coffee(Bean.ARABICA, 1000, 2)
// Also doesn't compile: type mismatch
// Coffee(Bean.ARABICA, Weight(1000), 2)
@rstraub
rstraub / bug-by-primitive-obsession.kt
Created February 10, 2022 13:49
bug-by-primitive-obsession
fun doStuff() = print(Coffee(Bean.ARABICA, 1000, 2))
@rstraub
rstraub / primitive-obsession.kt
Last active February 10, 2022 13:40
primitive-obsession.kt
enum class Bean {
ARABICA,
ROBUSTA,
BLEND
}
data class Coffee(val type: Bean, val intensity: Int, val weight: Int)
@rstraub
rstraub / expressive-test-with-tdbs.java
Created January 29, 2022 11:04
Using a Test Data Builder makes intent of test clear
@Test
void increased_readability_by_test_data_builder() {
Invoice invoice =
anInvoice()
.from(USA)
.with(
aPurchasedBook().of(aNovel().costing(50.0))
).build();
assertEquals(56.35, invoice.computeTotalAmount());
@rstraub
rstraub / non-expressive-test.java
Created January 29, 2022 11:02
Test does not convey its intent due to mechanism
@Test
void an_unreadable_test() {
Country country = new Country("USA", Currency.US_DOLLAR, Language.ENGLISH);
Author author = new Author("Oscar Wilde", country);
Novel novel = new Novel(
"The picture of dorian gray",
50.00,
author,
Language.ENGLISH,
Lists.newArrayList(Genre.MYSTERY)