Day 21

Starting on notifications

Started working on making Prompt send notifications today. This is the core functionality of the application. Most of the work done before this is foundational work. Having a very minimal sign up flow is important, but I wish I’d gotten into this part of the application sooner. Hopefully being able to re-use some of my sign-up process in other applications will make things more efficient on my next project.

Keeping it simple

So what is the simplest thing that could possible work to send notifications?

Each user will have a list of prompts that they’ve created. Some of them may be sent already, and some of them may not. To track which prompts are sent, I’ve added a datetime sent_at column to the prompt table. I prefer using a datetime to a boolean (true/false) because you get some added information and can still easily track whether the prompt has been sent or not. If it has a value, it’s sent. If not, still waiting.

When it’s time to send a prompt for a given user, the simplest thing is to look at all the prompts that haven’t been sent and pick one at random. Just send the first one that the database provides and don’t worry about the order.

Not simple enough

This is where I should have started, but my brain is always trying to think two steps ahead, so I’ve already gotten ahead of myself. Because I’m anticipating a feature that allows the user to choose their next prompt from their list, I added a next column. This is a boolean (true/false) value to indicate which prompt is next.

I did manage to avoid jumping into the additional complexity of ordering the prompts, but now that I’m writing this out, I realize I could have started more simply than I did.

Why does this matter?

It almost seems silly to be concerned about adding one additional column. Maybe it is. But if you’re trying to write tests for your code, it’s easy to miss the simple cases unless you work through them first. In this case, I can start by testing whether there’s one more sent prompt for each user when I run the task to send notifications. Adding the next column, as simple as that seems, moves my thought process on to more complex concerns. I’m already thinking about how I’m going to change that flag for the next prompt on the list and I may miss the simpler concern of did a prompt get sent at all?

Maybe I’m smart enough to think of all the possible cases and test them. And no doubt you’re smarter than I am. But experience has proven to me that I’m pretty good at trying to do too many things at once. Even if I don’t miss things, I sometimes get bogged down and spend too much time thinking. Thinking is obviously a critical component to writing software, but I want to get better at writing code without overthinking. I want to write a bunch of code, then go back and edit it later, like someone writing a novel would. Testing tiny, obvious things gives me a safer way to do that. If I decide that having a next column is a horrible idea next week and rip all that code out, then I’ll still have my more basic tests in place to make sure that I didn’t miss the simpler stuff.

Tomorrow I’ll scale things back a little, make sure I have the basics covered, and then keep moving forward with the more interesting stuff.