This is not the way to go if you want to make the code more readable. If things must be done in a loop, then leave them in this loop. Splitting the code into two loops changes the order of execution.
You can enhance readability by extracting the complex code to methods. Especially when you choose speaking method names. Avoid names like ExecuteStep1
, ExecuteStep2
, etc.
for (int i = 0; i < n; i++) {
var data = ReadData(i);
var transformed = TransformData(data);
SaveTransFormedData(i, transformed);
}
In the real code you would replace the generic Data
by a concrete term like Bookings
.
Note that I/O operations are measured in milliseconds while pure code operations are measured in nanoseconds. There is a factor of one million between the two! So, calling a few additional methods will not hurt you.
Another option is to avoid this loop completely is to move it to these methods and to let these methods accept and return IEnumerable<T>
and make them extension methods. Then you can write:
ReadData().TransformData().SaveTransFormedData();
See: