如果声明很长

时间:2013-03-20 19:53:47

标签: vb.net if-statement

我正在为我的学校课程建立一个计算项目,我有一个相当大的if声明。当我运行程序时它根本不是很慢,但我只是想知道编写大型if语句是否被认为是坏的。

    For BookingNumber = 0 To intBookingCount - 1
            If intStartPeriod(BookingNumber) = 1 And strComputerRoom(BookingNumber) = strCR Then
                radRadioButton1.Enabled = False
                If intLength(BookingNumber) = 1 Then
                ElseIf intLength(BookingNumber) = 2 Then
                    radRadioButton2.Enabled = False
                ElseIf intLength(BookingNumber) = 3 Then
                    radRadioButton2.Enabled = False
                    radRadioButton3.Enabled = False
                ElseIf intLength(BookingNumber) = 4 Then
                    radRadioButton2.Enabled = False
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                ElseIf intLength(BookingNumber) = 5 Then
                    radRadioButton2.Enabled = False
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                ElseIf intLength(BookingNumber) = 6 Then
                    radRadioButton2.Enabled = False
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                ElseIf intLength(BookingNumber) = 7 Then
                    radRadioButton2.Enabled = False
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                    radRadioButton7.Enabled = False
                End If
            ElseIf intStartPeriod(BookingNumber) = 2 And strComputerRoom(BookingNumber) = strCR Then
                radRadioButton2.Enabled = False
                If intLength(BookingNumber) = 2 Then
                    radRadioButton3.Enabled = False
                ElseIf intLength(BookingNumber) = 3 Then
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                ElseIf intLength(BookingNumber) = 4 Then
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                ElseIf intLength(BookingNumber) = 5 Then
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                ElseIf intLength(BookingNumber) = 6 Then
                    radRadioButton3.Enabled = False
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                    radRadioButton7.Enabled = False
                End If
            ElseIf intStartPeriod(BookingNumber) = 3 And strComputerRoom(BookingNumber) = strCR Then
                radRadioButton3.Enabled = False
                If intLength(BookingNumber) = 2 Then
                    radRadioButton4.Enabled = False
                ElseIf intLength(BookingNumber) = 3 Then
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                ElseIf intLength(BookingNumber) = 4 Then
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                ElseIf intLength(BookingNumber) = 5 Then
                    radRadioButton4.Enabled = False
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                    radRadioButton7.Enabled = False
                End If
            ElseIf intStartPeriod(BookingNumber) = 4 And strComputerRoom(BookingNumber) = strCR Then
                radRadioButton4.Enabled = False
                If intLength(BookingNumber) = 2 Then
                    radRadioButton5.Enabled = False
                ElseIf intLength(BookingNumber) = 3 Then
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                ElseIf intLength(BookingNumber) = 4 Then
                    radRadioButton5.Enabled = False
                    radRadioButton6.Enabled = False
                    radRadioButton7.Enabled = False
                End If
            ElseIf intStartPeriod(BookingNumber) = 5 And strComputerRoom(BookingNumber) = strCR Then
                radRadioButton5.Enabled = False
                If intLength(BookingNumber) = 2 Then
                    radRadioButton6.Enabled = False
                ElseIf intLength(BookingNumber) = 3 Then
                    radRadioButton6.Enabled = False
                    radRadioButton7.Enabled = False
                End If
            ElseIf intStartPeriod(BookingNumber) = 6 And strComputerRoom(BookingNumber) = strCR Then
                radRadioButton6.Enabled = False
                If intLength(BookingNumber) = 2 Then
                    radRadioButton7.Enabled = False
                End If
            ElseIf intStartPeriod(BookingNumber) = 7 And strComputerRoom(BookingNumber) = strCR Then
                radRadioButton7.Enabled = False
            End If
        Next
    Next
End Function

3 个答案:

答案 0 :(得分:0)

很难阅读,但适当的缩进可以减轻这种情况。这也可能表明您的代码需要进行一些重构。例如,真正的长if语句可以这样写:

if (condition == A && condition == C 
        && (someCondition >= 1000 || someOtherCondition != C) 
        || someThirdCondition == D) {

    ...

}

另外,一定要利用短路的布尔语句,并最简单地评估表达式。

答案 1 :(得分:0)

For BookingNumber = 0 To intBookingCount - 1
  bookingNumberLength = intLength(BookingNumber)
  If strComputerRoom(BookingNumber) = strCR then
    Select Case intStartPeriod(BookingNumber)
      Case 1
        radRadioButton1.Enabled = False
        Select Case bookingNumberLength
          Case 2
            radRadioButton2.Enabled = False
          Case 3
            radRadioButton2.Enabled = False
            radRadioButton3.Enabled = False
          Case 4
            radRadioButton2.Enabled = False
            radRadioButton3.Enabled = False
            radRadioButton4.Enabled = False
          Case 5
            radRadioButton2.Enabled = False
            radRadioButton3.Enabled = False
            radRadioButton4.Enabled = False
            radRadioButton5.Enabled = False
          Case 6
            radRadioButton2.Enabled = False
            radRadioButton3.Enabled = False
            radRadioButton4.Enabled = False
            radRadioButton5.Enabled = False
            radRadioButton6.Enabled = False
          Case 7
            radRadioButton2.Enabled = False
            radRadioButton3.Enabled = False
            radRadioButton4.Enabled = False
            radRadioButton5.Enabled = False
            radRadioButton6.Enabled = False
            radRadioButton7.Enabled = False
         End Select
      Case 2
      Case 3
      Case 4
      Case 5
      Case 6
        radRadioButton6.Enabled = False
        If intLength(BookingNumber) = 2 Then
          radRadioButton7.Enabled = False
        End If
      Case 7
        radRadioButton7.Enabled = False
    End Select 
  End If
Next

除非strComputerRoom(BookingNumber)= strCR,否则在执行此操作时,请查看简化操作,除非strComputerRoom(BookingNumber)= strCR。

这看起来不是什么大不了的事,但如果你开始砍掉这样的东西,你就会得到很多东西。

注意可能已经填满了VB,因为我是C#男孩,我会让你填写剩下的... 我将把这些单选按钮放在一个数组中,然后这只是一个改变所有内容的问题,如果还有其他东西,比如循环的开始和结束以及你怀疑它们之后。

For rb = rbStart To rbEnd
  MyButtons[rb].Enabled = False
Next

看着它,你可以开始并使用几行数学。虽然可能会挑战你的老师,但我不知道你有多远。当你在大约五行代码中给出这个时,他们可能会产生怀疑,如果我为你做的话,你就不会学习nuffin。

程序员规则#21 - 我们不喜欢做很多打字。

答案 2 :(得分:0)

<强>更新

我取回它,您可以用以下代码替换所有代码:

For BookingNumber = 0 To intBookingCount - 1

    If intStartPeriod(BookingNumber) = 1 Then radRadioButton1.Enabled = False
    If intStartPeriod(BookingNumber) = 2 Then radRadioButton2.Enabled = False
    If intStartPeriod(BookingNumber) = 3 Then radRadioButton3.Enabled = False
    If intStartPeriod(BookingNumber) = 4 Then radRadioButton4.Enabled = False
    If intStartPeriod(BookingNumber) = 5 Then radRadioButton5.Enabled = False
    If intStartPeriod(BookingNumber) = 6 Then radRadioButton6.Enabled = False
    If intStartPeriod(BookingNumber) = 7 Then radRadioButton7.Enabled = False

    Dim total = intStartPeriod(BookingNumber) + intLength(BookingNumber)
    If total >= 3 Then radRadioButton2.Enabled = False
    If total >= 4 Then radRadioButton3.Enabled = False
    If total >= 5 Then radRadioButton4.Enabled = False
    If total >= 6 Then radRadioButton5.Enabled = False
    If total >= 7 Then radRadioButton6.Enabled = False
    If total >= 8 Then radRadioButton7.Enabled = False

Next

巨大的if-blocks非常不受欢迎。一般来说,如果可能的话,最好将它们排除在外。例如,您经常会看到

的内容
Sub DoStuff()
    If conditionA Then
        DoSomeStuff()
        If conditionB Then
            DoMoreStuff()
        End If
    End If
End Sub

这可以重构为:

Sub DoStuff()
    If Not conditionA Then Return

    DoSomeStuff()

    If Not conditionB Then Return

    DoMoreStuff()
End Sub

我回答了一个类似的(不一样的)问题并得到了这个非常有用的答案:
https://softwareengineering.stackexchange.com/a/174977/57863

与之相关:
https://softwareengineering.stackexchange.com/a/172910/57863

关于重构的主题提供了一些很棒的链接。