Most times when confronted with a “tricky” or new coding issue the best option is not to just start writing code figuring you have the correct path in mind.
Stop and think about it.
Do a bit of research.
Try a bit of code and then think about how it will work out if you keep going.
Try another method and think about that one.
I’ve long liked the idea that good code is written three times. First you write it, and figure out how it really should have been written. So you write the code again, to find out all the ways you could optimize it and clean it up.
Then you write it the third time, which is what you’ll ship based on the first two attempts to write the code that weren’t as good as they could be.