The other day I was talking to a guy about a possible freelancing gig and he said how wonderful it was that I should bring up the topic of unit/automated testingwithout being asked. Said that most (many?) developers don't have the level of rigor to use automated testing.
My reaction was one of disbelief "Rigor!? But automated testing is one of the laziest things a developer can do! It speeds stuff up _so much!"_
As luck would have it, last night I was hit over the head with my own words and nearly died debugging a single function.
I was working on Stripe webhooks and for security reasons decided not to use the event data sent in request body. Makes sense right? Take the event id from request body, then fetch the actual event from Stripe.
It's the only way to be certain you aren't responding to bogus events sent by an evil person trying to make you look bad (nothing actually bad can happen, at worst a customer would get extra paid invoice emails).
Due to poor decoupling - I didn't really want to decouple a 6 line function into two functions - everything was now difficult to test. I can't create events on Stripe's servers with unit tests and without actual events existing I can't test the function works as it's supposed to.
How many bugs can you put in a 6 line function anyway?
When the client tested on staging ... it didn't work. Invoice email wasn't sent and Stripe complained of a 500 error.
It took me almost two hours to fix all the bugs because my testing cycle looked like this:
- change code
- commit to develop branch
- switch to staging branch
- merge develop branch into staging
- push to github
- change to other terminal window
- pull from staging branch
- restart python processes
- go to Stripe dashboard
- pick customer
- create invoice item
- create actual invoice
- choose invoice
- pay invoice
- go to Stripe logs
- find invoice.payment_succeeded webhook
- scroll down to response
- look through raw html of django's error page
- find symptom
- GOTO 1.
That's right, a whopping 20 step debug cycle all because I'm an idiot and couldn't find a way to automate this. Or maybe I was too tired to do the unobvious thing ... although I still don't want to split a 6 liner into two functions.
With proper unit testing the debug cycle would look like this:
- change code
- run tests
- symptom thrown in face
- GOTO 1.
Much lazier right?
For the record, those six lines of code contained 4 bugs ->
- forgot to import a module
- different event data structure than I understood from the docs
- misnamed variables from one line to another
Yes, all of those could easily have been caught if my test coverage was actually any good! And then not only would I not look like an idiot in front of the client, I'd probably spend no more than ten minutes fixing this.
Let this be a lesson both to you and to Future Swizec!
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companiesLearn more
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️