0

我正在为我的学校课程构建一个计算项目,并且我有一个相当大的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
4

3 回答 3

0

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

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

    ...

}

此外,请务必利用短路的布尔语句并将最容易评估的表达式放在首位。

于 2013-03-20T19:56:18.917 回答
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.

这看起来没什么大不了的,但如果你开始砍掉这样的东西,你就会得到真正的东西。

注意可能已经填满了 VB,因为我是一个 C# 男孩,我会让你填写它的其余部分......我我会把这些单选按钮放在一个数组中,然后它就只是一个改变所有 if 和 elseing 的问题,以便在你对它们进行 sussed 之后提出循环的开始和结束之类的东西。

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

看着它,你可以从几行数学开始计算。不过可能会挑战你的老师,我不知道你的距离有多远。当你在大约五行代码中给出这个时,他们可能会怀疑,如果我为你做,你就不会学习 nuffin'。

程序员规则 #21 - 我们不喜欢大量打字。

于 2013-03-20T20:46:43.840 回答
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 块非常不受欢迎。一般来说,如果可能的话,最好将它们排除在外。例如,你会经常看到类似的东西

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

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

于 2013-03-20T21:41:42.697 回答