How i should do projects

Im kinda new on programming, and always i try to do a project, it ends up full of bugs and the code ends up being a mess, so i wonder, how i should start a project