最佳实践:方法中的输出参数与复杂返回类型
使用复杂返回类型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.我有一种感觉,我们会在这个问题上看到不同的观点。不确定是否存在最佳实践。
我通常更喜欢复杂的数据类型,因为我觉得这更符合函数的原始结构,其中输出参数在签名中的输入参数之前。基本上我不喜欢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#)
我倾向于使用 out params 作为通用样式。很少需要实现返回 UDT 的辅助函数,但这些函数通常是模块私有的,因此我也可以将 UDT 的范围保持为模块私有。
在后一种情况下,通常会像这样使用 retval
...并且很可能会将
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
... and most probably would keep
TimeType
with private scope.绝对是见仁见智的问题。我确实时不时地使用
ByRef
参数,特别是对于需要成功/失败类型场景的实用函数。例如,.net 框架中的TryParse
函数正是执行此操作。您的参数化函数可能如下所示:意味着您可以这样调用它:
但是,随着事情开始变得更加复杂,并且您实际上返回业务项而不是执行逻辑命令,那么更需要一个单独的类和返回类型。它还使得调用和返回更容易维护。
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, theTryParse
functions in the .net framework do exactly this. Your parametrised function could look like:Meaning you can call it like:
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.