Right now I'm working on a web application that receives a significant amount of data from a database that has a potential to return null results. When going through the cyclomatic complexity for the application a number of functions are weighing in between 10 - 30. For the most part the majority of the functions with the high numbers have a lot of lines similar to the following:
If Not oraData.IsDBNull(4) Then row("Field") = oraData.GetString(4)
Which leads me to my question, what is the best way to go about trying to bring these numbers down? Right now I'm looking at having the majority of the functions below 10.
Avoid use of switch/case statements in your code. Use Factory or Strategy design patterns instead. Complexity of 8 (1 for each CASE and 1 for method itself). Refactoring using Factory design pattern and complexity changed to 1.
The size of your functions and procedures is almost all that matters when you have to manage program complexity. Limit your functions and methods to a minimum. If they're getting too big, find a technique to break them up into smaller pieces. Remember, code simplicity is fundamental to a well-written, clean code.
For most routines, a cyclomatic complexity below 4 is considered good; a cyclomatic complexity between 5 and 7 is considered medium complexity, between 8 and 10 is high complexity, and above that is extreme complexity.
What about using Extension Methods.
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.
Decompose into functions, perhaps something like this:
//Object Pascal
procedure UpdateIfNotNull( const fldName: String; fldIndex : integer );
begin
if oraData.IsDBNull( fldIndex ) then
row( fldName ) := oraData.GetString(fldIndex);
end;
Of course you can extend the procedures signature so that "oraData" and "row" can passed as parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With