对于这个 VB.NET 代码,这是正确的格式吗(为了 grokkability)?

Is this the correct formatting (for grokkability) for this VB.NET code?

我不熟悉 VB.NET 并且,为了尝试理解遗留代码(格式草率)我正在更好地维护,我将其输入缩进机 here .

但这是结果的一部分:

currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS = New ADODB.Recordset
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
Category = "New Business"
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 
    if Not IsNewBusiness
    Category = "Existing Business"
End If
Response.Write("<!-- IsNewBusiness after NOT adoRS.EOF check = " & CStr(IsNewBusiness) & " -->")
End If
adoRS.Close()

If Request.Form.Item("Action") = "Save" Then
Response.Write("<!-- Made it into the Action =Save block -->")
Unit = Request.Form.Item("Unit")
. . .

这样对吗?在我看来更应该是这样的:

currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS = New ADODB.Recordset
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
Category = "New Business"
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 
    if Not IsNewBusiness
            Category = "Existing Business"
    End If
    Response.Write("<!-- IsNewBusiness after NOT adoRS.EOF check = " & CStr(IsNewBusiness) & " -->")
End If
adoRS.Close()

If Request.Form.Item("Action") = "Save" Then
    Response.Write("<!-- Made it into the Action =Save block -->")
    Unit = Request.Form.Item("Unit")
    . . .

我知道这只是格式化,它不会导致代码的工作方式有任何不同,但是正确格式化它会对我有很大帮助(特别是 "if"s 和 "endif"s等排队。

只要有"If"就一定有相应的"End If"对吗?

假设是这样,这个缩进代码更加混乱:

If Not adoRS.EOF Then
CustomerChk = adoRS.Fields.Item(0).Value
adoRS.Close()
If CustomerChk <> CustNo Then

...不应该是:

If Not adoRS.EOF Then
    CustomerChk = adoRS.Fields.Item(0).Value
    adoRS.Close()
    If CustomerChk <> CustNo Then

?否则,它看起来像第一个 if 块结束时没有任何明确的说明。

关于您的 VB.NET 个问题...

1) 这个[代码缩进更新]对吗?

我觉得还行。我可能会在 if 代码块前后插入空行,但这只是我个人的喜好,而不是 VB.NET 要求。

2) 任何时候有一个 "If" 就一定有一个相应的 "End If" 对吗?

通常这是惯例,但这不是 VB.NET 要求。 VB.NET 允许单行 "If" 语句,例如此 MSDN page.

中引用的以下行
' If A > 10, execute the three colon-separated statements in the order
' that they appear
If A > 10 Then A = A + 1 : B = B + A : C = C + B