I’ve been programming for years, but I can never quite decide the correct place to comment on the function of a for loop. Before, to the right, or after?
// iterate over rows, blah blah...
for (std::string row : csv) {
...
}
for (std::string row : csv) { // iterate over rows, blah blah...
...
}
for (std::string row : csv) {
// iterate over rows, blah blah...
...
}
Commenting to the right saves a line, but is less readable and tends to make the line go over 80 characters. Commenting below kind of works, but that may be where I place the comment for the first block of code within the for loop. What’s your preferred style and why?
The most widespread commenting style I’ve seen is commenting before doing, so I prefer the first option. It is simply more consistent with the commenting style in the rest of the code, i.e., it does not give a specific treatment to loops.