What is the best way to reduce cyclic complexity when checking data?

I am currently working on a web application that receives a significant amount of data from a database that can return null results. When going through cyclic complexity for an application, a number of functions weigh between 10-30. In most cases, most functions with a large number have many lines, similar to the following:

If Not oraData.IsDBNull(4) Then row("Field") = oraData.GetString(4) 

What leads me to my question, what is the best way to try to reduce these numbers? Right now I'm looking at the fact that most of the features are below 10.

+6
code-metrics cyclomatic-complexity data-processing
source share
5 answers

How 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 the cyclic complexity of your functions.

+2
source share

First question: why did you hang yourself on CC? This is a tool for evaluating how dense the code is, and the rule of thumb should be "not too large cc".

It probably hits all of these “IFs” and raises that number - so reduce the number of ifs by calling the wrapper function, which extracts data from a result set that processes zero or changes the query so that it does not return nulls.

Keep in mind that zeros provide information and are not worthless. For example, a Republican or a Democrat? using null does not mean any choice.

+2
source share

Split 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 expand the signature of the procedure so that "oraData" and "row" can be passed as parameters.

+2
source share

Have you seen this question ? He asks something like this (but I think at a more basic level) ... but then that means the answers here may not be very useful.

I definitely agree with the other suggestions here: if there are duplicate statements that can be neatly packed into functions / procedures, this could be one approach if you are not just shifting CC around. I'm not sure that you have grown too much if you go from one proc with CC from 35 to 3 procs with CCs 15, 10 and 10. (This is not a bad first step, but ideally you can simplify something in a larger area to reduce the total CC in this area of ​​your system.)

+1
source share

You can reorganize if into a separate utility function to reduce your CC. To process various types of databases (string, int, etc.) A number of functions or a function based on the differentiation of types may be required.

However, I would say that any solution will lead to a decrease in the number of supported or readable codes (i.e. you can degrade other indicators!), And if the QA allows it to pass in accordance with this justification.

0
source share

All Articles