How I Structure Code

Darin Heavilin
4 min readFeb 13, 2021

The way we write code has changed over the years. In the 10+ years I’ve been writing code, I’ve followed different practices to “speed up development” or make code “more manageable” or “more readable”. It seems we as software engineers are always looking for ways to make our lives easier. We have to work in the code we write everyday, so why wouldn’t we want to make it easier, less frustrating, and less error-prone?

My style of writing code has been molded over the last couple of years. It’s based on domain driven design (DDD), with some shortcuts. It’s not perfect and may not work for everyone, but I’ve found it to better adhere to Clean Code principles, such as encapsulation, inheritance, single responsibility, and others.

Let’s show you what I mean with an example. We’re going to create a bank application that only retrieves account information and takes deposits (you don’t want to get your money out, do you?). Thinking of deposits, we can envision a few things.

  1. Controller endpoint to start a deposit and returns the new account balance
  2. Service with the method to implement the business logic to perform a deposit
  3. Repository to persist the deposit

--

--