Imports System.Runtime.CompilerServices
Module Extensions
<Extension()> _
Public Function TryGetString(ByVal row As IDataRecord, i As Integer) As String
If row.IsDBNull(i) Then
Return null
End If
Return row.GetString(i);
End Function
End Module
Imports System.Runtime.CompilerServices
Module Extensions
<Extension()> _
Public Function TryGetString(ByVal row As IDataRecord, i As Integer) As String
If row.IsDBNull(i) Then
Return null
End If
Return row.GetString(i);
End Function
End Module
Then you can simply write:
row("Field") = oraData.TryGetString(4)
This reads smoothly and reduces cyclomatic complexity on your functions.
The first question is: Why are you "hung" up on CC ? It's a tool to evaluate how dense the code is and a rule of thumb should be "not too high of a cc number".
It's probably hitting all those "IF"s and bringing up that number - so reduce the number of ifs by calling a wrap function that extracts the data from the result set which handles the null or change the query so it doesn't return nulls.
Keep in mind that nulls do provide information and are not useless. For example Republican or Democrat ? using null says neither choice.
我绝对同意这里的其他建议:如果有重复的语句可以整齐地打包到函数/过程中,那么这可能是一种可以采取的方法,只要您不只是改变 CC 即可。 如果你从一个 CC 为 35 的 proc 转移到三个 CC 为 15、10 和 10 的 proc,我不确定你是否获得了太多。(这不是一个糟糕的第一步,但理想情况下你能够在更大范围内简化某些内容,以减少系统该区域的总 CC。)
Did you see this question? He's asking something similar (but I think at a more basic level) ... but then that means the answers there may not be much help here.
I would definitely agree with the other suggestions here: if there are repeated statements that can be neatly packaged into functions/procedures, that might be one approach to take, as long as you're not just shifting CC around. I'm not sure you've gained too much if you move from one proc with a CC of 35 to three procs with CCs of 15, 10, and 10. (It's not a bad first step, but ideally you'd be able to simplify something on a larger scope to reduce total CC in that area of your system.)
It is possible to refactor the if into a separate utility function to reduce your CC. A number of functions or a function relying on type differentiation might be required to handle the different database types ( string, int etc ..)
However, I would argue that any solution would result in less maintainable or readable code (i.e. you might worsen other metrics!) and would as QA allow it to pass according to this justification.
使用扩展方法怎么样。
然后你可以简单地写:
这可以流畅地读取并降低函数的圈复杂度。
What about using Extension Methods.
Then you can simply write:
This reads smoothly and reduces cyclomatic complexity on your functions.