Home
GOTO should not be used within loops
Description
The PowerBuilder code rule stating that "GOTO should not be used within loops" is an important guideline for developers to adhere to when writing code. This rule is designed to prevent unexpected behavior and to make the code easier to read and maintain. By avoiding the use of GOTO statements within loops, developers can ensure that their code is more organized, less prone to errors, and easier to debug. Additionally, using GOTO statements within loops can lead to unexpected behavior, which can be difficult to diagnose and fix. Therefore, it is important to avoid the use of GOTO statements within loops in order to ensure code maintainability and reliability.
Key Benefits
- Prevent Infinite Loops: GOTO should not be used within loops to prevent the code from entering an infinite loop.
- Readability: GOTO statements can make code difficult to read and understand, making it difficult to debug.
- Maintainability: GOTO statements can make code difficult to maintain, as it is difficult to trace the flow of the code.
Non-compliant Code Example
function int TestFunctionCall (int cnt)
DO WHILE cnt <= 15
IF cnt < 0 THEN
goto restart; //Non compliant code (GOTO statement is used within loop)
END IF;
cnt = cnt - 1
LOOP
messagebox('Test','testing... ')
restart:
if isvalid(cnt) and not isnull( cnt ) then
messagebox('Test','is valid found ')
end if
return cnt;
end function
Compliant Code Example
function cnt TestFunctionCall (int cnt) DO UNTIL (cnt <= 15 AND cnt >= 0) cnt = cnt - 1 LOOP messagebox('Test','testing... ') if isvalid(cnt) and not isnull( cnt ) then messagebox('Test','is valid found ') end if return cnt; end function