# Introduction

When writing unit tests, the acronym "AAA" (Arrange, Act, Assert) is a helpful way to keep things clear. In particular, it really helps with the separation of concerns. A unit of code should do one and only one thing.

  • single output/return
  • pure functions
  • side-effects
  • unit testing
  • structuring code

I like to use the same approach in all levels of my application as much as possible.

# Application Scope

  1. Handle External Data (Arrange) Before I accept any external data from an API, user or system input, etc. I first "arrange" the data by serializing it into a structure that will be consistent from here on in my application. Even if the external data comes from a "trusted" source, like my own API, I serialize it. That way, if/when I change my API, I'll have to do minimal effort to keep the rest of my application working.
  • data mapping
  • serializers
  1. Do Application Stuff (Act) Now that I have predictable and consistent data, thre rest of my application can do its thing.

  2. Prepare to send External Data (Assert) Before sending any data out to an API call, user or system output, etc. I then do the reverse of step 1. I map my "internal" data structure to match expected outputs.

# Function Scope

  1. Handle Parameter Data (Arrange) When a function accepts parameter data, the first thing I do is make sure it's correct and in the right format. With staticly typed languages, this is often handled for me at some level, but there can still be work to do. For instance, it might be good to merge arrays or flatten/denormalize nested data.

  2. Do Function Stuff (Act) With my data ready, I can now do what I need to, almost always without logical statements. This keeps things really simple.

  3. Return (Assert) After doing my function stuff, I now make sure any outgoing response data is in the right format. Sometimes this means normalizing data, but often Step 2 ends up with the right result.

# Summary

It's incredible how much cleaner and simpler my code is when I make sure to get Step 1 (Arrange) right.

  • immutable
  • no conditional logic
  • expressions over statements