Skip to content

Instantly share code, notes, and snippets.

View rstraub's full-sized avatar

Roy Straub rstraub

View GitHub Profile
@rstraub
rstraub / fp.md
Created December 1, 2024 13:15
FP train coupling and decoupling
def couple_train(train, train_set):
    return train + (train_set,)


def decouple_train(train):
    return train[:-1]


def train_length(train):
@rstraub
rstraub / oop.py
Created December 1, 2024 13:13
OOP train in python
class Train:
def __init__(self, composition):
self.composition = composition
def couple(self, train_set):
self.composition.append(train_set)
def decouple(self):
self.composition.pop()
@rstraub
rstraub / oop.md
Created December 1, 2024 13:11
OOP train coupling and decoupling
class Train:
    def __init__(self, composition):
        self.composition = composition

    def couple(self, train_set):
        self.composition.append(train_set)

    def decouple(self):
 self.composition.pop()
@rstraub
rstraub / constructing-microtypes-with-extensions.kt
Created February 10, 2022 15:24
Constructing microtypes with extensions
fun Int.toWeight(): Weight = Weight(this)
fun easyConstruction() = 1000.toWeight() // Weight(1000)
@rstraub
rstraub / microtype-operator-overloading.kt
Created February 10, 2022 15:19
Overloading operators with microtypes
@JvmInline
value class Weight(val grams: Int) {
operator fun plus(other: Weight) = Weight(grams + other.grams)
}
fun operatorStuff(): Weight = Weight(1000) + Weight(500) // Weight(1500)
@rstraub
rstraub / primitive-obsession-implicit-model.kt
Created February 10, 2022 14:48
Implicit model by primitive obsession
enum class Bean { ARABICA, ROBUSTA, BLEND }
data class Coffee(val type: Bean, val intensity: Int, val weight: Int)
@rstraub
rstraub / expressive-model-with-microtype.kt
Created February 10, 2022 14:46
Expressive model with microtype
enum class Bean { ARABICA, ROBUSTA, BLEND }
enum class Intensity { ONE, TWO, THREE, FOUR, FIVE }
data class Weight(val grams: Int) { /* implementation details */ }
data class Coffee(val type: Bean, val intensity: Intensity, val weight: Weight)
@rstraub
rstraub / microtype-improving-cohesion.kt
Created February 10, 2022 14:36
Microtype improving cohesion
data class Weight(val grams: Int) {
init {
require(grams >= 0) { "Weight should be a positive number" }
}
fun toKilograms() = grams / 1000.0
}
@rstraub
rstraub / microtype-preventing-duplication.kt
Created February 10, 2022 14:24
Microtype preventing duplication
data class Weight(val grams: Int) {
init {
require(grams >= 0) { "Weight should be a positive number" }
}
}
@rstraub
rstraub / primitive-obsession-duplication.kt
Created February 10, 2022 14:15
Duplication caused by primitive obsession
// Checks validity of weight
fun inputA(weight: Int) {
require(weight >= 0) { "Weight should be a positive number" }
// Logic with weight
}
// Also checks the validity of weight: duplication!
fun inputB(weight: Int) {
// Because the checks are implemented in different ways it's harder to spot.
if (weight < 0) throw IllegalArgumentException("Weight should be a positive number")