Home

GOTO should not be used to jump backwards

Description

    The "GOTO should not be used to jump backwards" PowerBuilder code rule states that the GOTO statement should not be used to jump backwards to a previous line of code. This is because it can create confusion and make debugging difficult. Instead, the code should be restructured to use loops, functions, and other control statements to make the code easier to read and understand.

Key Benefits

  • Eliminates Spaghetti Code: GOTO should not be used to jump backwards, which eliminates the potential for spaghetti code.
  • Improves Readability: By avoiding jumping backwards, code becomes easier to read and understand.
  • Decreases Complexity: By avoiding jumping backwards, code becomes simpler and less complex.
  • Reduces Errors: By avoiding jumping backwards, errors are less likely to occur.

 

Non-compliant Code Example

function integer calculateSteps(integer step)

integer totalSteps = 0

restart: //Non compliant code (goto jump backwards)
	
	totalSteps = totalSteps + step
	step = step - 1

	if step > 0 then
		goto restart
	end if


return totalSteps

end function

Compliant Code Example

function integer calculateSteps(integer step)

integer totalSteps = 0

do 
	totalSteps = totalSteps + step
	step = step - 1

loop while(step > 0) //Compliant code

return totalSteps

end function
Visual Expert 2024
 VEPBRULE31