I don't know too much about it myself but from my understanding, you basically use a Pokemon that breeds and hatches fast (like Magikarp) to see how many eggs you get a shiny in, then reset and swap Magikarp for the Pokemon you want shiny, and reject eggs until you get the egg number the shiny Magikarp was in? I think that's the methodology of it, again, I don't know for sure.
I don't know about why that works in the code though.