Kotlin minimalism for the lazy programmer

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.

1. Define all your services/components as singleton objects.

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…

2. Use mockk library to test

mockk library is here

@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