> midway through writing code I start rethinking the design and start over again from scratch
What's the source of requirements you're building to and who verifies those?
If you're creating requirements out of thin air, just so you can build something (eg: you want to learn something) - your current approach is absolutely ok.