💾 Archived View for gemini.spicyporkbun.online › text › kotlin.gmi captured on 2022-07-16 at 13:29:17. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-11-30)
-=-=-=-=-=-=-
How many times have you been told to use interfaces/DI and avoid singletons?
I’m lazy and I hate unnecessary boilerplate. Making an interface for one implementation isn’t smart planning, it’s bloat. DI? Do I really need it? If you want to make a small Kotlin service quickly, ditch the cruft.
object BossService { ... } object EmployeeService { fun calculateWage(e: Employee) { val x = BossService.getOpinion(e) ... } }
Cool, we just got rid of any need for interfaces or DI in the main source. Everything’s globally accessible. And did we lose the ability to write meaningful tests? Nope…
@Test fun testEmployeeService() { val employee = createEmployee() mockkObject(BossService) every { BossService.getOpinion(employee) } returns Opinion.GREAT assertEquals(100, EmployeeService.calculateWage(employee)) }
The use of singletons didn’t make the tests any harder.
Obviously for larger services, you’ll make different choices (making everything a global singleton is not such a good idea there). But for the little guys, just be lazy. It’s good for you.
“We will encourage you to develop the three great virtues of a programmer: laziness, impatience, and hubris.” - Larry Wall