I was once tasked with writing a tinder-style swiper and it left a mark on me. Starting off, it was pretty simple: there was a post someone wrote where they created a "Disney Tinder" game. I found the code and forked the repo here: https://github.com/johnnyboyy/disney-tinder/tree/master. The code worked well enough, but it took a long time to understand exactly what was going on. Libraries aside, this was the real confusing part:
Luckily, most of this code was from an example created by the react-with-gesture (now @use-gesture/react). The code itself is still too complicated, but at least there's some comments to help understand what's going on. From the Code Sandbox:
I prefer to have the code explain itself since the comments can easily become outdated. I believe it was Douglas Crockford that said, "comments are not checked by the compiler." I felt a strong pull to get this code expressed properly. I'm hoping also that I can express what I've done and why well enough as I'm still new to writing in general...
I started off trying to rename some of these variables and create some "helper variables" that could describe better what was going on technically. An example from the final code being:
At the time, the "rotation" bits were still included and it became clear to me they needed to be two separate components. I can't say exactly why or what the feeling is, but I hope the code can speak better to it than my words:
Rotatable
Each card is skewed a little based on it's position in the Deck, and the top most card is rotated into the straight up-and-down position. In the original code, the rotation and swiping are handled at the same time and in the same place, but it was calling out to be separated.
Swipeable
This was the code that handled the swiping action. I was eventually able to split this out and give the variables and props some better names for clarity.