0

I have an Excel file, which I am reading using Apache POI APIs; which gives me control over cells/columns in my Excel.

I have around 17 columns in my Excel and these are with different datatypes. I want to populate a model object from these column values. So, I came up with something like below to populate my model object:

private WHTApps populateModel(int columnIndex, Object cellValue) {
        WHTApps whtApp = new WHTApps();
        switch (columnIndex) {
        case 0:
            whtApp.setVendorCode((int) cellValue);
            break;
        case 1:
            whtApp.setVendorName((String) cellValue);
            break;
        case 2:
            whtApp.setCountry((String) cellValue);
            break;
        case 3:
            whtApp.setApplicationReceivedFromUSDate((Date) cellValue);
            break;
        case 4:
            whtApp.setContractNumber((String) cellValue);
            break;
        case 5:
            whtApp.setContractEffectiveDate((Date) cellValue);
            break;
        case 6:
            whtApp.setContractExpirationDate((Date) cellValue);
            break;
        case 7:
            whtApp.setAutomaticRenewal((String) cellValue);
            break;
        case 8:
            whtApp.setReducedRate((int) cellValue);
            break;
        case 9:
            whtApp.setForm17((String) cellValue);
            break;
        case 10:
            whtApp.setResidencyCertIssueDate((Date) cellValue);
            break;
        case 11:
            whtApp.setTaxAuthoritySubmissionDate((Date) cellValue);
            break;
        case 12:
            whtApp.setTaxAuthorityAcceptanceDate((Date) cellValue);
            break;
        case 13:
            whtApp.setStatus((String) cellValue);
            break;
        case 14:
            whtApp.setForm17Expiration((Date) cellValue);
            break;
        case 15:
            whtApp.setComments((String) cellValue);
            break;
        case 16:
            whtApp.setRemarks((String) cellValue);
            break;
        case 17:
            whtApp.setCategory((String) cellValue);
            break;
        default:
        }

        return whtApp;
    }

But, this gives rise to a really high cyclomatic complexity value of 19.

Now how do I reduce the cyclomatic complexity as I have 17 columns which I need to populate in my model object?

Thanks for helping me out

Akshay

Akshay Lokur
  • 6,680
  • 13
  • 43
  • 62
  • 2
    [code review](http://codereview.stackexchange.com/) – Scary Wombat Aug 14 '17 at 05:35
  • I would be more concerned with how bloated and fragile the code is. There are many tools to automatically map CSV columns to POJO fields, so you don't have to worry about all this. – shmosel Aug 14 '17 at 05:35
  • OP reposted [this question on Code Review](https://codereview.stackexchange.com/questions/172931/apache-poi-and-cyclomatic-complexity-of-code/172935#172935). Perhaps, we should close it here and redirect people there. – default locale Aug 14 '17 at 10:37
  • Sure, let's track this question here: https://codereview.stackexchange.com/questions/172931/apache-poi-and-cyclomatic-complexity-of-code and close this one... – Akshay Lokur Aug 15 '17 at 02:31
  • I am not sure, how to close this one though :) – Akshay Lokur Aug 15 '17 at 02:33

0 Answers0