Home

String literals should not be duplicated

Description

    The "String literals should not be duplicated" PowerBuilder code rule states that when writing code, any string literals should be written only once. This means that when writing code, any strings that are used multiple times should be stored in a variable and the variable should be used instead of the literal string. This helps to make the code more efficient and maintainable, as any changes to the string can be done in one place instead of multiple places.

Key Benefits

  • Reduced complexity: By not duplicating String literals, code complexity is reduced as it eliminates the need to keep track of multiple copies of the same String.
  • Improved readability: By avoiding duplicated String literals, code is easier to read and understand.
  • More efficient memory usage: By not duplicating String literals, memory usage is improved as the JVM can reuse the same String literal in multiple places.

 

Non-compliant Code Example

function string TestFunctionCall (int cnt)

smallint @count  

if cnt < 1 then
	messagebox('SuccessTest','less than 1 found ') //Non compliant code (String literals duplicated below)
end if

messagebox('SuccessTest','test finished') //Non compliant code (String literals duplicated above)
@count = 1 


return "

end function

Compliant Code Example

function string TestFunctionCall (int cnt)

constant string MSG_Title = 'SuccessTest'

smallint @count  

if cnt < 1 then
	messagebox(MSG_Title,'less than 1 found ') //Compliant code
end if

messagebox(MSG_Title,'test finished') //Compliant code
@count = 1 

return "

end function
Visual Expert 2024
 VEPBRULE36