最佳实践:方法中的输出参数与复杂返回类型

发布于 2024-10-13 09:04:58 字数 1932 浏览 11 评论 0原文

使用复杂返回类型:

Public Type TimeType
    hours As Integer
    minutes As Integer
End Type

Public Function ParseTimeField(time As String) As TimeType
    Dim timeObject As TimeType
    Dim amountOfDigitHours As Integer

    If time = "" Then time = "0"

    If HasHoursAndMinutesParts(time) Then
        amountOfDigitHours = GetAmountOfDigitHours(time)
        timeObject.hours = CInt(Left(time, amountOfDigitHours))
        timeObject.minutes = CInt(Right(time, 2))
    Else
        timeObject.hours = 0
        timeObject.minutes = CInt(time)
    End If

    ParseTimeField = timeObject
End Function

Private Function HasHoursAndMinutesParts(time As String) As Boolean
    HasHoursAndMinutesParts = Len(time) > 2
End Function

Private Function GetAmountOfDigitHours(time As String) As Integer
    GetAmountOfDigitHours = Len(time) - 2
End Function

调用:

Dim timeObj As TimeType
timeObj = ParseTimeField(strTime)

OR 使用输出参数:

Public Function ParseTimeField(time As String, ByRef hours As Integer, ByRef minutes As Integer)
    Dim timeObject As TimeType
    Dim amountOfDigitHours As Integer

    If time = "" Then time = "0"

    If HasHoursAndMinutesParts(time) Then
        amountOfDigitHours = GetAmountOfDigitHours(time)
        hours = CInt(Left(time, amountOfDigitHours))
        minutes = CInt(Right(time, 2))
    Else
        hours = 0
        minutes = CInt(time)
    End If

    ParseTimeField = timeObject
End Function

Private Function HasHoursAndMinutesParts(time As String) As Boolean
    HasHoursAndMinutesParts = Len(time) > 2
End Function

Private Function GetAmountOfDigitHours(time As String) As Integer
    GetAmountOfDigitHours = Len(time) - 2
End Function

调用:

Dim hours As Integer
Dim minutes As Integer

Call ParseTimeField(strTime, hours, minutes)

顺便说一句,这是 VB6 代码 =)

Using complex return type:

Public Type TimeType
    hours As Integer
    minutes As Integer
End Type

Public Function ParseTimeField(time As String) As TimeType
    Dim timeObject As TimeType
    Dim amountOfDigitHours As Integer

    If time = "" Then time = "0"

    If HasHoursAndMinutesParts(time) Then
        amountOfDigitHours = GetAmountOfDigitHours(time)
        timeObject.hours = CInt(Left(time, amountOfDigitHours))
        timeObject.minutes = CInt(Right(time, 2))
    Else
        timeObject.hours = 0
        timeObject.minutes = CInt(time)
    End If

    ParseTimeField = timeObject
End Function

Private Function HasHoursAndMinutesParts(time As String) As Boolean
    HasHoursAndMinutesParts = Len(time) > 2
End Function

Private Function GetAmountOfDigitHours(time As String) As Integer
    GetAmountOfDigitHours = Len(time) - 2
End Function

Call:

Dim timeObj As TimeType
timeObj = ParseTimeField(strTime)

OR using out parameters:

Public Function ParseTimeField(time As String, ByRef hours As Integer, ByRef minutes As Integer)
    Dim timeObject As TimeType
    Dim amountOfDigitHours As Integer

    If time = "" Then time = "0"

    If HasHoursAndMinutesParts(time) Then
        amountOfDigitHours = GetAmountOfDigitHours(time)
        hours = CInt(Left(time, amountOfDigitHours))
        minutes = CInt(Right(time, 2))
    Else
        hours = 0
        minutes = CInt(time)
    End If

    ParseTimeField = timeObject
End Function

Private Function HasHoursAndMinutesParts(time As String) As Boolean
    HasHoursAndMinutesParts = Len(time) > 2
End Function

Private Function GetAmountOfDigitHours(time As String) As Integer
    GetAmountOfDigitHours = Len(time) - 2
End Function

Call:

Dim hours As Integer
Dim minutes As Integer

Call ParseTimeField(strTime, hours, minutes)

BTW this is VB6 code =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果您有单一返回类型,请不要使用 out 参数来返回它。

一般来说,我发现多个 ref/out 参数是代码味道。如果您需要从方法返回数据,最好将其放在一个连贯的对象中。

If you have a single return type, do not use an out parameter to return it.

In general, I find multiple ref/out parameters to be a code smell. If you need to return data from your method, it is better if it is in one coherent object.

却一份温柔 2024-10-20 09:04:58

我有一种感觉,我们会在这个问题上看到不同的观点。不确定是否存在最佳实践。

我通常更喜欢复杂的数据类型,因为我觉得这更符合函数的原始结构,其中输出参数在签名中的输入参数之前。基本上我不喜欢out参数——它们是多余的。每当用编程语言有两种方法做一件事时,就会使不必要的复杂化(我猜我会被 Perl 狂热分子的说法杀死)。您可以使用 return 语句返回数据。时期。

这就是说,当我需要返回两个没有自然分组的参数时,我仍然发现自己经常使用 out 参数 - 即最终会出现在一个仅在该特定函数的返回值中使用的类中。

每当返回数据中有三个或更多参数时,我从不使用 out ,因为我发现调用代码过于冗长 - 需要 Dim 变量(或 C# 中的 var'em)

I have a feeling we're going to see different opinions on this matter. Not sure there exists a best practice.

I usually prefer complex datatypes because I feel that is more in line with the original structure of functions where output parameter preceed the input parameters in the signature. Basically I don't like out parameters - they're superfluous. Whenever there's two ways of doing a thing in a programming language, you complicate unneccessary (guess I'm gonna get killed by Perl-fanatics stating this). You return data with the return statement. Period.

This said, I still find myself using out parameters often when I need to return two parameteres that have no natural grouping - i.e. would end up in a class which would be used solely in the return value of this specific function.

Whenever there's three or more parameters in the return data, I never use out simply because I find the calling code to be excessive verbose - needing to Dim the variables (or var'em in C#)

我恋#小黄人 2024-10-20 09:04:58

我倾向于使用 out params 作为通用样式。很少需要实现返回 UDT 的辅助函数,但这些函数通常是模块私有的,因此我也可以将 UDT 的范围保持为模块私有。

在后一种情况下,通常会像这样使用 retval

With ParseTimeField(strTime)
    Debug.Print .hours, .minutes
End With

...并且很可能会将 TimeType 保留在私有范围内。

I tend to use out params as the universal style. Rarely need to implement helper functions that return UDT but these are usually private to the module so I can keep the scope of the UDT private to the module too.

In the latter case usually consume the retval like this

With ParseTimeField(strTime)
    Debug.Print .hours, .minutes
End With

... and most probably would keep TimeType with private scope.

不知所踪 2024-10-20 09:04:58

绝对是见仁见智的问题。我确实时不时地使用 ByRef 参数,特别是对于需要成功/失败类型场景的实用函数。例如,.net 框架中的 TryParse 函数正是执行此操作。您的参数化函数可能如下所示:

Public Function ParseTimeField(time As String, ByRef hours As Integer, ByRef minutes As Integer) As Boolean
    'Do stuff'
    ParseTimeField = True 'or false depending on output success'
End Sub

意味着您可以这样调用它:

Dim hours As Integer
Dim mins as Integer
If ParseTimeField(time, hours, mins) = True Then
    'It worked'
End If

但是,随着事情开始变得更加复杂,并且您实际上返回业务项而不是执行逻辑命令,那么更需要一个单独的类和返回类型。它还使得调用和返回更容易维护。

Definately a matter of opinion. I do use ByRef parameters from time to time, especially for utility functions which require a success/fail type scenario. For example, the TryParse functions in the .net framework do exactly this. Your parametrised function could look like:

Public Function ParseTimeField(time As String, ByRef hours As Integer, ByRef minutes As Integer) As Boolean
    'Do stuff'
    ParseTimeField = True 'or false depending on output success'
End Sub

Meaning you can call it like:

Dim hours As Integer
Dim mins as Integer
If ParseTimeField(time, hours, mins) = True Then
    'It worked'
End If

However as things start to get more complicated, and you're actually returning business items as opposed to doing logical commands, then a separate class and a return type is more desirable. It also makes calling AND returning easier to maintain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文