2013-03-20 101 views
0

我正在爲我的學校課程構建一個計算項目,並且我有一個相當大的if聲明。當我運行這個程序時,它一點都不慢,但是我只是想知道寫大的if語句是否被認爲是不好的。Long if statement

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 
+1

向我們展示 - 如果我們看不到代碼,我們不知道您的意思。 – Oded 2013-03-20 19:54:29

+0

是的,我用來創建long if語句,但是我發現它們很難調試,如果語句有問題,您不知道問題出在哪裏,那麼if語句很大就是不好的形式 – 2013-03-20 19:58:34

+0

取決於它的易理解性是。不喜歡他們自己太容易迷路。 – 2013-03-20 20:04:04

回答

0

UPDATE

我收回,你可以用這個代替ALL你的代碼:

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 

海量如果塊被巨大的令人難以接受的。一般來說,如果可能,最好將它們分解出來。例如,你會經常看到沿

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

提供了一些有關重構主題的偉大鏈接。

+0

非常感謝你:) – 2013-03-20 22:45:42

0

這很難看,但正確的縮進可以減輕這一點。這也可能表明你是代碼需要重構。作爲一個例子,很長if語句可以這樣寫:

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

    ... 

} 

此外,一定要採取短路布爾語句的優勢,把最容易首先評估表達。

+0

VB甚至有短路嗎? – ThiefMaster 2013-03-20 19:57:43

+2

@ThiefMaster - 相當於'&&'是'AndAlso'而不是'And'。而且「短路」,而不管第一個結果如何,都將評估這兩個表達式。 – keyboardP 2013-03-20 19:59:22

+0

那麼我已經使用了循環增加的東西的價值,而不是寫出相同的如果陳述了7次,個人,我覺得很容易找到我想編輯的位,所以我只是想知道它是否可以接受,它似乎不是使用switch語句的正確位置。 – 2013-03-20 20:00:34

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#的男孩,我要讓你填寫它的其餘部分... 我我會在一個數組中的單選按鈕和那麼這只是一個改變所有這一切的問題,如果不然就會想出一些類似於循環的開始和結束的東西,並且在你懷疑它們之後。

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

看着它,你可以開始工作,並與幾條數學線。可能會挑戰你的老師,但我不知道你有多遠。當你用五行代碼給出時,他們可能會產生懷疑,如果我爲你做這件事,你將無法學會nuffin。

程序員規則#21 - 我們不喜歡做很多打字。