Search code examples
javadesign-patternsgetter-setter

How can i reduce cyclomatic complexity from a model


i need to reduce cyclomatic complexity of this model because it has 26, this is a normal class for setters and getters

public class DetailRecord {

    private int lengthReference1;
    private int lengthReference2;
    private int lengthPayersNit;
    private int lengthTransactionAmount;
    private String recordType;
    private String payersNit;
    private String payersName;
    private String payersAccountBank;
    private String accountNumberToBeDebited;
    private String transactionType;
    private String transactionAmount;
    private String referenceOne;
    private String referenceTwo;
    private String expirationDateOrApplicationDate;
    private String billedPeriods;
    private String cycle;
    private String reserved;
    private String validationNit;
    private String encabezadoTotal;

    public DetailRecord() {
        lengthReference1 = 30;
        lengthReference2 = 30;
        lengthPayersNit = 13;
        lengthTransactionAmount = 17;
        recordType = "6";
        payersName = "                    ";
    }

    public int getLengthReference1() {
        return lengthReference1;
    }

    public int getLengthReference2() {
        return lengthReference2;
    }

    public int getLengthPayersNit() {
        return lengthPayersNit;
    }

    public int getLengthTransactionAmount() {
        return lengthTransactionAmount;
    }

    public String getRecordType() {
        return recordType;
    }

    public String getPayersName() {
        return payersName;
    }

    public String getPayersAccountBank() {
        return payersAccountBank;
    }
}

How can i reduce the cyclomatic complexity? Using a builder perhaps or what else can I do? Abstract the class or maybe create a interface?


Solution

  • i have reduce the ciclomatyc complexity using factory method,

    public class DetailRecord extends StringManager {
    private final String RECORD_TYPE = "06";
    private String userMainReference;
    private String secondaryReferenceOfTheUser;
    private static final String BILLER_PERIODS = "00";
    private String cycle;
    private String mainServiceValue;
    private static final String BILLED_SERVICE_CODE_BY_ADDITIONAL_COMPANY = "             ";
    private static final String ADDITIONAL_SERVICE_VALUE = "              ";
    private String expirationDate;
    private String collectingBankId;
    private String targetAccountNumber;
    private String billerAccountType;
    private String noBillerId;
    private String billerName;
    private static final String SOURCE_BANK_CODE = "   ";
    private static final String RESERVED = "                        ";
    private static final int lengthOfUserMainReference = 48;
    private static final int lengthOfTheSecondaryReferenceOfTheUser = 30;
    private static final int lengthOfTheCycle = 3;
    private static final int lengthOfTheMainServiceValue = 14;
    private static final int lengthOfTheCollectingBankId = 8;
    private static final int lengthOfTargetAccountNumber = 17;
    private static final int lengthOfNoBillerId = 10;
    private static final int lengthOfBillerName = 22;
    
    
    public DetailRecord(String userMainReference, String secondaryReferenceOfTheUser, String cycle, String mainServiceValue, String expirationDate, String collectingBankId, String targetAccountNumber, String billerAccountType, String noBillerId, String billerName) {
        this.userMainReference = userMainReference;
        this.secondaryReferenceOfTheUser = secondaryReferenceOfTheUser;
        this.cycle = cycle;
        this.mainServiceValue = mainServiceValue;
        this.expirationDate = expirationDate;
        this.collectingBankId = collectingBankId;
        this.targetAccountNumber = targetAccountNumber;
        this.billerAccountType = billerAccountType;
        this.noBillerId = noBillerId;
        this.billerName = billerName;
    }
    
    public static DetailRecord createWith(String userMainReference, String secondaryReferenceOfTheUser, String cycle, String mainServiceValue, String expirationDate, String collectingBankId, String targetAccountNumber, String billerAccountType, String noBillerId, String billerName) {
        return new DetailRecord(userMainReference, secondaryReferenceOfTheUser, cycle, mainServiceValue, expirationDate, collectingBankId, targetAccountNumber, billerAccountType, noBillerId, billerName);
    }
    
    @Override
    public String toString() {
        return new StringBuilder()
                .append(RECORD_TYPE)
                .append(fillInWithZerosLeftPad(userMainReference, lengthOfUserMainReference))
                .append(fillInWithBlanksLeftPad(secondaryReferenceOfTheUser, lengthOfTheSecondaryReferenceOfTheUser))
                .append(BILLER_PERIODS)
                .append(fillInWithBlanksLeftPad(cycle, lengthOfTheCycle))
                .append(fillInWithZerosLeftPad(mainServiceValue, lengthOfTheMainServiceValue))
                .append(BILLED_SERVICE_CODE_BY_ADDITIONAL_COMPANY)
                .append(ADDITIONAL_SERVICE_VALUE)
                .append(expirationDate)
                .append(fillInWithZerosLeftPad(collectingBankId, lengthOfTheCollectingBankId))
                .append(fillInWithBlanksLeftPad(targetAccountNumber, lengthOfTargetAccountNumber))
                .append(billerAccountType)
                .append(fillInWithBlanksLeftPad(noBillerId, lengthOfNoBillerId))
                .append(fillInWithBlanksLeftPad(billerName, lengthOfBillerName))
                .append(SOURCE_BANK_CODE)
                .append(RESERVED).toString();
    }
    

    }