How deep is too Deep?

 

 

You may create a nested if statement which is nested quite deeply.

 

If blnSalaried = False Then

          If intYearsEmployed > 5 Then

                   If sngMonthSales > 200000 Then

                             If sngLastMonthSales > 200000 Then

                                      sngCommission = sngMonthSales * .10

                             Else

sngCommission = sngMonthSales * .08

                             End If

                   Else

sngCommission = sngMonthSales * .06

                   End If

          Else

sngCommission = sngMonthSales * .04

          End If

Else

sngCommission = sngMonthSales * .05

End If

 

Different languages may have different limits to how deep you may nest an If statement without generating an error but it is generally accepted that an If statement nested more than three deep may be too difficult to follow and should be avoided. In other words the example above is an example of poor programming practices. The following is an alternative way to code the same logic.

 

If blnSalaried = True  Then

          sngCommission = sngMonthSales * .05

ElseIf intYearsEmployed <= 5 Then

sngCommission = sngMonthSales * .04

ElseIf sngMonthSales > 200000 Then

          If sngLastMonthSales > 200000 Then

                   sngCommission = sngMonthSales * .10

          Else

sngCommission = sngMonthSales * .8

          End If

Else

sngCommission = sngMonthSales * .6

End If