On Code And Types
And saving a bit of my sanityDecember 8, 2022 Photo by Daniel Frank from Pexels
Lately, I've been a stickler to adding types in my code. On hobby projects, I would use the TypeScript compiler and JSDoc to add types to my JavaScript. Even at work, I would use the same strategy with PHP, using PHPDoc and have my IDE aid me with types. On other things, I've been urging my colleagues to add interfaces to GraphQL API calls, ensuring Java classes have sensible types, all the way to having the DB columns be the correct types.
I'm already imagining my supervisor telling me to stop being a perfectionist. 🤣
But here's the thing. Ensuring that the data is using the correct types doesn't just spot bugs ahead of time. It can also help with productivity, reduce tech debt and code quantity. For example, dates can be stored and passed around as strings (e.g. ISO format) or an number (a timestamp). But once you start consuming that date, like getting the year, you now have to do the type conversions manually and everywhere, probably put a converter function in a "utils" package that nobody knows about. But if it were a date begin with, you can just call date.getYear()
right away.
So get your types right. A little effort now can save you your hair and sanity in the long run.
PS: Been a while since I wrote something on this blog. Just been very busy at work, taking on more projects, doing a wider variety of things. Recently, I've been sent over to the Java+JavaScript side of the house to help with some stuff. Still loving the Optional and Streams in Java. And everything ES2015 and after is a wish come true, younger me would be excited to use this version of JavaScript.
PPS: I know that I write only when I have something to complain about. Taking advantage of the situation to get myself to write something this time. Maybe go back to the usual monthly cadence next year.