Home

CASE clauses should not have too many lines

Description

    The "CASE clauses should not have too many lines" PowerBuilder code rule states that when writing code using the CASE statement, the number of lines within each clause should be kept to a minimum. This helps to improve the readability of the code, makes it easier to debug, and reduces the chances of introducing errors. Additionally, it is recommended that the CASE statement be used only when absolutely necessary, and that alternative approaches such as IF/ELSE statements be used instead when possible. This helps to keep the code concise and easier to maintain.

Key Benefits

  • Easier to debug : CASE clauses should not have too many lines as it can be difficult to debug when there are too many lines.
  • Better performance : Having fewer lines in a CASE clause can lead to better performance as it reduces the amount of processing the SQL server needs to do.
  • More readable code : Having fewer lines in a CASE clause makes the code more readable and easier to maintain.

 

Non-compliant Code Example

public function integer of_getminmaxpoints (windowobject awo_control[], ref integer ai_min_x, ref integer ai_min_y, ref integer ai_max_x, ref integer ai_max_y);
oval				loval_cntrl
line				ln_cntrl

integer			li_x, li_y, li_width, li_height, li_temp
integer			li_upperbound
integer			li_cnt

//Determine position of the right most and bottom most control.
For li_cnt = 1 to li_upperbound
	If IsValid(awo_control[li_cnt]) Then
		Choose Case of_TypeOf(awo_control[li_cnt])
			Case LINE //Non compliant code (When clause should contains lines less then 6)
				ln_cntrl = awo_control[li_cnt]
				li_x = ln_cntrl.BeginX
				li_y = ln_cntrl.BeginY
				li_width = ln_cntrl.EndX
				li_height = ln_cntrl.EndY
				//Correct for lines that may have the End points 
				//before to the Begin points.
				If li_width >= li_x Then
					li_width = li_width - li_x
				Else
					li_temp = li_x
					li_x = li_width
					li_width = li_temp - li_x
				End If	
				If li_height >= li_y Then
					li_height = li_height - li_y
				Else
					li_temp = li_y
					li_y = li_height
					li_height = li_temp - li_y
				End If
			Case OVAL
				loval_cntrl = awo_control[li_cnt]
				li_x = loval_cntrl.X
				li_y = loval_cntrl.Y
				li_width = loval_cntrl.Width
				li_height = loval_cntrl.Height		
			Case Else
				//An unknown control type has been encountered
				Return -1
		End Choose
		
	End If
Next


Return 1

end function

Compliant Code Example

public function integer of_getminmaxpoints (windowobject awo_control[], ref integer ai_min_x, ref integer ai_min_y, ref integer ai_max_x, ref integer ai_max_y);
oval				loval_cntrl
line				ln_cntrl

integer			li_x, li_y, li_width, li_height, li_temp
integer			li_upperbound
integer			li_cnt


//Determine position of the right most and bottom most control.
For li_cnt = 1 to li_upperbound
	If IsValid(awo_control[li_cnt]) Then
		Choose Case of_TypeOf(awo_control[li_cnt])
			Case LINE //compliant code 
				ln_cntrl = awo_control[li_cnt]
				li_width = calWidth(ln_cntrl,li_x,li_y,li_width,li_height)
				li_height = calHeight(ln_cntrl,li_x,li_y,li_width,li_height)
			Case OVAL
				loval_cntrl = awo_control[li_cnt]
				li_x = loval_cntrl.X
				li_y = loval_cntrl.Y
				li_width = loval_cntrl.Width
				li_height = loval_cntrl.Height		
			Case Else
				//An unknown control type has been encountered
				Return -1
		End Choose
		
	End If
Next


Return 1

end function

private integer calHeight(line ln_cntrl,integer li_x,integer li_y,integer li_width,integer li_height)
		
				li_x = ln_cntrl.BeginX
				li_y = ln_cntrl.BeginY
				li_height = ln_cntrl.EndY
				//Correct for lines that may have the End points 
				//before to the Begin points.
				If li_height >= li_y Then
					li_height = li_height - li_y
				Else
					li_temp = li_y
					li_y = li_height
					li_height = li_temp - li_y
				End If
			return li_height;
end function

private integer calWidth(line ln_cntrl,integer li_x,integer li_y,integer li_width,integer li_height)
				li_x = ln_cntrl.BeginX
				li_y = ln_cntrl.BeginY
				li_width = ln_cntrl.EndX
				//Correct for lines that may have the End points 
				//before to the Begin points.
				If li_width >= li_x Then
					li_width = li_width - li_x
				Else
					li_temp = li_x
					li_x = li_width
					li_width = li_temp - li_x
				End If	
			return li_width;
end function

Visual Expert 2024
 VEPBRULE66