Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to reduce cyclomatic complexity when validating data? [closed]

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.

like image 657
rjzii Avatar asked Oct 15 '08 13:10

rjzii


People also ask

How can cyclomatic complexity be reduced in a switch case?

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.

How can we reduce complexity?

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.

What is a good cyclomatic complexity?

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.


2 Answers

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.

like image 75
Jorge Ferreira Avatar answered Sep 22 '22 10:09

Jorge Ferreira


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.

like image 40
Barry Carr Avatar answered Sep 19 '22 10:09

Barry Carr