Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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

private void initComparisonDatatable() {
    List<BrandProfitAndLoss> brandProfitAndLossComparisonDatatable = new ArrayList<BrandProfitAndLoss>();
    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() <= 0 && brandActionForm.getBrandProfitLossComparisonList().size() > 0){
        for(int i = 0; i < 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 < brandActionForm.getBrandProfitLossList().size(); i++){
            brandProfitAndLossCurrentYear = brandActionForm.getBrandProfitLossList().get(i);
            brandProfitAndLossComparisonDatatable.add(brandProfitAndLossCurrentYear);
            if(brandActionForm.getBrandProfitLossComparisonList().size() > 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);
}
share|improve this question
2  
What is this method supposed to do in plain English? Method name doesn't help. –  abuzittin gillifirca Jun 26 '13 at 7:41
add comment

1 Answer

up vote 5 down vote accepted

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() <= 0 instead of list.isEmpty() and list.size() > 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<BrandProfitAndLoss> comparisonDatatable = listForDatatable(
                brandActionForm.getBrandProfitLossComparisonList(), 
                brandActionForm.getBrandProfitLossList(), 
                brandActionForm.getDateMode());

        brandActionForm.setBrandProfitLossComparisonListForDatatable(comparisonDatatable);
    }

    private static List<BrandProfitAndLoss> listForDatatable(
            List<BrandProfitAndLoss> brandProfitLossComparisonList,
            List<BrandProfitAndLoss> brandProfitLossList, Object dateMode) {
        List<BrandProfitAndLoss> comparisonDatatable = new ArrayList<BrandProfitAndLoss>();

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

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

            BrandProfitAndLoss ofCurrentYear = (i < 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;
    }
share|improve this answer
    
Thanks for help. It's very necessary for me –  user25815 Jun 26 '13 at 9:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.