Stirring spaghetti (or any other type of pasta) while it cooks serves several important purposes:
Prevents sticking: Stirring the pasta helps prevent it from sticking together or to the bottom of the pot. This is particularly important during the first few minutes of cooking when the pasta is still firm and tends to clump together.
Ensures even cooking: Stirring the pasta ensures that all pieces are evenly exposed to the boiling water and heat. This helps to cook the pasta uniformly, so you don't end up with some parts being undercooked and others overcooked.
Helps avoid boil-overs: Stirring the pasta occasionally helps to reduce the likelihood of water boiling over the pot's edge. Pasta tends to create foam during cooking, and stirring helps control this foaming and prevents excessive boil-overs.
Releases excess starch: Pasta releases starch into the boiling water as it cooks. Stirring helps to disperse this starch, preventing the pasta from becoming overly sticky or gummy.
Tests for doneness: By stirring and occasionally removing a piece of pasta for tasting, you can check for the desired level of doneness. This is especially important for achieving the perfect "al dente" texture, where the pasta is cooked but still has a slight firmness when bitten.
Remember that when cooking pasta, it's essential to use a large pot with plenty of water to ensure the pasta has enough space to cook evenly. Use a generous amount of salt in the boiling water to enhance the pasta's flavor. Stir the pasta occasionally throughout the cooking process, but avoid stirring too vigorously to prevent breaking the pasta.