Which is a better way to learn programing? To learn by going through a course and following the curriculum or by doing project?
Some people know and understand the basics because they followed a structure learning resource to the end before attempting any project. Some others just begin by trial and error, and end up building good projects. But they may not understand their own lines of codes.
So which approach would you recommend?
You've got the answer for yourself. Learning courses grants you understanding of the basics which makes it easier to build projects. You will never get good result without knowing well what you're doing.