My co-worker and I discussing about the code readability on simple loops. Suppose a method takes an integer parameter and does some operation on the elements of an array depending on the parameter. e.g
For the sake of simplicity, assume array bounds are negligible and there is no error generated by the codes I presented below.
function doSomeOp(targetIndex) {
for(var i = targetIndex + 5; i < someArray.length; i++) {
someArray[i] = 42;
}
}
as opposed to;
function doSomeOp(targetIndex) {
for(var i = targetIndex; i < someArray.length; i++) {
someArray[i + 5] = 42;
}
}
Is there any advantage to define the loop parameter to simpler values like "0", "targetIndex", "array.length" and then define business logic when using that loop parameter?