有没有更好的方法来编写此代码。尝试尝试3次或成功

时间:2019-04-06 15:35:23

标签: vb.net

基本上,我想尝试3次。如果失败,请重试。如果可以,我继续前进。

如果我出3次后仍然失败,则

    For i = 1 To 3
        Do
            Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
            If json = "" Then
                Exit Do
            End If

            jtok1 = JObject.Parse(json)
            If jtok1.Item("success").ToString = "False" Then
                Exit Do
            End If
        Loop While False
        Exit For
    Next

我不想使用goto。所以这就是我的方法。

我能想到的另一种方法是将成功作为布尔变量并采取相应的行动

   Dim jtok1 = New JObject
    Dim success As Boolean = False

    For i = 1 To 3
        success = True
        Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
        If json = "" Then
            success = False
        End If

        If success Then
            jtok1 = JObject.Parse(json)
            If jtok1.Item("success").ToString = "False" Then
                success = False
            End If
        End If

        If success Then
            Exit For
        End If
    Next

    If success = False Then
        Return
    End If

更多的专业程序员怎么做?

2 个答案:

答案 0 :(得分:2)

反转逻辑。使用<>代替=似乎可以使代码更简洁明了。

Dim success as Boolean = False
For i = 1 To 3
    Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
    ' If we get something proceed to parsing it else start again the loop
    If json <> "" Then ' or better if Not String.IsNullOrEmpty(json) Then
        jtok1 = JObject.Parse(json)
        ' if the parsing is not false, mission accomplished, else re-loop
        If jtok1.Item("success").ToString <> "False" Then
            success = True
            Exit For
        End If
    End If
Next

' here you can test for success or not....

答案 1 :(得分:2)

我发现最好使用一个函数,而不是围绕每个需要它的代码块编写重试逻辑。验证重试功能的有效性之后,您就不必担心该逻辑的正确实现了。您的书面代码意图也显而易见。

''' <summary>
''' Executes a delegate function until success or maxAttempts reached
''' </summary>
''' <param name="codeToRetry">a boolean function delegate containing the code to execute.  Return true if code completed successfully</param>
''' <param name="maxAttempts">maximum number of times <paramref name="codeToRetry"/> is executed </param>
''' <returns></returns>
Public Shared Function RetryCode(codeToRetry As Func(Of Boolean), maxAttempts As Int32) As Boolean
    Dim attempts As Int32 = 0
    Dim ret As Boolean = False
    Do While (Not ret) AndAlso (attempts < maxAttempts)
        attempts += 1
        ret = codeToRetry()
    Loop
    Return ret
End Function

重写原始代码如下所示。我相信我的检索逻辑正确无误,但请确认它符合您的需求。

Sub DemoRetryCode()
    Dim jtok1 As New JObject
    Dim json As String
    Dim success As Boolean = RetryCode(Function()
                                        Dim downloadSuccess As Boolean
                                        json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
                                        If Not String.IsNullOrWhiteSpace(json) Then
                                            jtok1 = JObject.Parse(json)
                                            downloadSuccess= jtok1.Item("success").ToString <> "False"
                                        End If
                                        Return downloadSuccess
                                      End Function, 3)
End Sub

编译器将发出闭合代码,以包装局部变量jsonjtok1和委托,以使看起来好像将本地传递给委托函数/从委托函数返回。