Home
FOR loop end conditions should not be hard - coded
Description
This rule states that the end conditions of a FOR loop should not be hard-coded. This means that the end conditions should be determined dynamically based on the data being processed by the loop. Hard-coding end conditions can lead to errors and inefficiencies in the code. It is better to use variables or other expressions to set the end conditions so that the loop can be more flexible and adaptable to different data sets.
Key Benefits
- Flexibility: FOR loop end conditions can be changed without hard-coding.
- Readability: FOR loop end conditions can be easily understood without having to read the code.
- Efficiency: FOR loop end conditions can be modified quickly and easily, reducing development time.
- Consistency: FOR loop end conditions can be applied consistently across multiple applications.
Non-compliant Code Example
function string TestFunctionCall (int cnt)
integer i
for i = 1 to 10 //Non compliant code (FOR loop end condition value is hard-coded)
boxes[i].Checked = NOT boxes[i].Checked
next
return "
end function
Compliant Code Example
function string TestFunctionCall (int cnt)
integer i
for i = 1 to cnt //Compliant code
boxes[i].Checked = NOT boxes[i].Checked
next
return "
end function