OpenMS icon indicating copy to clipboard operation
OpenMS copied to clipboard

Convert iterator for loops

Open timosachsenberg opened this issue 3 years ago • 7 comments

To range for loops for easier readability. Many have already been converted but some still remain.

timosachsenberg avatar Jun 06 '22 09:06 timosachsenberg

Hi, Can I contribute to this please?

rishi2802 avatar Sep 10 '22 04:09 rishi2802

Sure. Maybe start with a few of them and make a small Pull request so we can give feedback if you are on the right track.

timosachsenberg avatar Sep 10 '22 05:09 timosachsenberg

maybe a few things to consider:

  1. prefer const auto over just auto if the variable does not need to be modified.
  2. use auto if the type is small (roughly 16 bytes or less) , and you do not need to modify the container. Otherwise use auto&, i.e. take by reference, to avoid the copy (or enable modification). Points 1) and 2) can be combined, e.g. const auto&.
  3. use sensible names, e.g. if the former name of the variable using iterators was it (as it usually is), then you should find a better name (since it suggests an iterator, whereas the new range-based loop variable should be named like the type that underlies the variable, e.g. spec (for MSSpectrum) or p (for a Peak1D).

made-up example: Old:

for (MSExperiment::Iterator it = exp.begin(); it != exp.end(); ++it)
{
  it->setRT(0.0);
}

becomes (new)

for (auto& spec : exp)
{
  spec.setRT(0.0);
}

cbielow avatar Sep 11 '22 11:09 cbielow

is this issue still open.can i have a look into it.

Sainith123 avatar Feb 09 '23 04:02 Sainith123

There is already anboten PR. Thanks!

timosachsenberg avatar Feb 09 '23 07:02 timosachsenberg