Home

Loops with at most one iteration should be refactored

Description

    The "Loops with at most one iteration should be refactored" rule in PowerBuilder code states that any loops which contain only one iteration of code should be refactored in order to make the code more efficient and easier to read. This means that instead of having a loop with just one iteration, the code should be rewritten to simply execute the code block without the loop. This can help to reduce the amount of code and make the code more understandable.

Key Benefits

  • Reduced Complexity: Refactoring loops with at most one iteration reduces complexity of the code.
  • Improved Readability: Refactoring loops with at most one iteration makes the code easier to read and understand.
  • Increased Efficiency: Refactoring loops with at most one iteration improves the efficiency of the code, as it eliminates unnecessary iterations.
  • Reduced Memory Usage: Refactoring loops with at most one iteration reduces the amount of memory used by the code.

 

Non-compliant Code Example

function integer TestFunctionCall (integer cnt)

integer A = 1, B = cnt

DO WHILE A <= 15 //Non compliant code (Only one iteration will take place because of EXIT)
      A = (A + 1) * B;
      EXIT;
LOOP

return A

end function

Compliant Code Example

function integer TestFunctionCall (integer cnt)

integer A = 1, B = cnt

DO WHILE A <= 15 //Compliant code
      A = (A + 1) * B;
         IF counter > 10 THEN
		    EXIT;
	    ELSE
		    A = B;
	    END IF;
LOOP

return A

end function
function void TestFunctionCall ()
do while IsValid (lpo_parent) //Compliant code
	if lpo_parent.TypeOf() <> window! then
		lpo_parent = lpo_parent.GetParent()
	else
		exit
	end if
loop
end function
Visual Expert 2024
 VEPBRULE40