Initialize comparison datatable This code doesn't look too good.  Could anyone help me format it?

private void initComparisonDatatable() {
    List&lt;BrandProfitAndLoss&gt; brandProfitAndLossComparisonDatatable = new ArrayList&lt;BrandProfitAndLoss&gt;();
    BrandProfitAndLoss brandProfitAndLossCurrentYear = new BrandProfitAndLoss();
    BrandProfitAndLoss brandProfitAndLossPreviousYear = new BrandProfitAndLoss();
    // if the selected year is null and the previous year is not null
    if(brandActionForm.getBrandProfitLossList().size() &lt;= 0 &amp;&amp; brandActionForm.getBrandProfitLossComparisonList().size() &gt; 0){
        for(int i = 0; i &lt; brandActionForm.getBrandProfitLossComparisonList().size(); i++){
            brandProfitAndLossPreviousYear = brandActionForm.getBrandProfitLossComparisonList().get(i);
            brandProfitAndLossCurrentYear = new BrandProfitAndLoss(brandActionForm.getBrandProfitLossComparisonList().get(i).getDate(), brandActionForm.getDateMode(), Consts.CURRENT_YEAR);
            brandProfitAndLossComparisonDatatable.add(brandProfitAndLossCurrentYear);
            brandProfitAndLossComparisonDatatable.add(brandProfitAndLossPreviousYear);
            brandProfitAndLossComparisonDatatable.add(new BrandProfitAndLoss().getAllRateComparisonRecords(brandProfitAndLossCurrentYear
                                                     , brandProfitAndLossPreviousYear));
        }
    }
    // the selected year is not null
    else {
        for(int i = 0; i &lt; brandActionForm.getBrandProfitLossList().size(); i++){
            brandProfitAndLossCurrentYear = brandActionForm.getBrandProfitLossList().get(i);
            brandProfitAndLossComparisonDatatable.add(brandProfitAndLossCurrentYear);
            if(brandActionForm.getBrandProfitLossComparisonList().size() &gt; 0){
                brandProfitAndLossPreviousYear = brandActionForm.getBrandProfitLossComparisonList().get(i);
                brandProfitAndLossComparisonDatatable.add(brandProfitAndLossPreviousYear);
                brandProfitAndLossComparisonDatatable.add(new BrandProfitAndLoss().getAllRateComparisonRecords(brandProfitAndLossCurrentYear
                                                    , brandProfitAndLossPreviousYear));
            }
            else {
                brandProfitAndLossPreviousYear = new BrandProfitAndLoss(brandActionForm.getBrandProfitLossList().get(i).getDate(), brandActionForm.getDateMode(), Consts.CURRENT_YEAR);
                brandProfitAndLossComparisonDatatable.add(brandProfitAndLossPreviousYear);
                brandProfitAndLossComparisonDatatable.add(new BrandProfitAndLoss().getAllRateComparisonRecords(brandActionForm.getBrandProfitLossList().get(i)
                                                    , brandProfitAndLossPreviousYear));
            }
        }
    }
    brandActionForm.setBrandProfitLossComparisonListForDatatable(brandProfitAndLossComparisonDatatable);
}
Some problems that could be seen without actually understanding code:


Unhelpful and possibly misleading comments: // the selected year is not null
Repeating type in variable name : brandProfitAndLossCurrentYear
Using ad hoc code instead of using appropriate library methods: list.size() &lt;= 0 instead of list.isEmpty() and list.size() &gt; 0 instead of !list.isEmpty()
Long method doing more than one thing: initComparisonDatatable retrieve data from form, calculate something based using said data, modify form using calculated data
Feature Envy: Since initComparisonDatatable acts on brandActionForm and uses only data from brandActionForm it belongs to brandActionForm.
Repetitive code: body of the three loops are repeated with minor variations.


There are probable design issues, but those cannot be addressed properly without further clarification. For example new BrandProfitAndLoss().getAllRateComparisonRecords suggests BrandProfitAndLoss has some data access code in it, therefore should be moved elsewhere; it probably is a class method instead of an instance method; and since it is a class method it probably has dependency issues, I can smell an evil singleton from here.

How refactored code could look like, (since loops and conditional statements have been changed these will definitely break something, so do not try to use it in production without proper unit testing):

    private void initComparisonDatatable() {
        List&lt;BrandProfitAndLoss&gt; comparisonDatatable = listForDatatable(
                brandActionForm.getBrandProfitLossComparisonList(), 
                brandActionForm.getBrandProfitLossList(), 
                brandActionForm.getDateMode());

        brandActionForm.setBrandProfitLossComparisonListForDatatable(comparisonDatatable);
    }

    private static List&lt;BrandProfitAndLoss&gt; listForDatatable(
            List&lt;BrandProfitAndLoss&gt; brandProfitLossComparisonList,
            List&lt;BrandProfitAndLoss&gt; brandProfitLossList, Object dateMode) {
        List&lt;BrandProfitAndLoss&gt; comparisonDatatable = new ArrayList&lt;BrandProfitAndLoss&gt;();

        int prevYearSize = brandProfitLossComparisonList.size();
        int currentYearSize = brandProfitLossList.size();
        int size = Math.max(prevYearSize, currentYearSize);

        for (int i = 0; i &lt; size; i++) {
            BrandProfitAndLoss ofPreviousYear = (i &lt; prevYearSize) 
                    ? brandProfitLossComparisonList.get(i) 
                    : null;

            BrandProfitAndLoss ofCurrentYear = (i &lt; currentYearSize) 
                    ? brandProfitLossList.get(i) 
                    : null;

            if (ofPreviousYear == null) {
                ofPreviousYear = new BrandProfitAndLoss(
                        ofCurrentYear.getDate(), dateMode, Consts.CURRENT_YEAR);
            }

            if (ofCurrentYear == null) {
                ofCurrentYear = new BrandProfitAndLoss(
                        ofPreviousYear.getDate(), dateMode, Consts.CURRENT_YEAR);
            }

            comparisonDatatable.add(ofCurrentYear);
            comparisonDatatable.add(ofPreviousYear);
            comparisonDatatable
                    .add(new BrandProfitAndLoss().getAllRateComparisonRecords(
                            ofCurrentYear, ofPreviousYear));
        }

        return comparisonDatatable;
    }