0

I am working on a ChildModelListSheetHandler, that has a childModelListDict.

As what you're probably thinking, it maps the foreign keys that are the IDs of the parent models, to the list of child models associated with that parent model.

I start working on a derived class thereof: RateCardChildModelListHandler. So far, so good.

I get to the part where I have to implement how to write it into a row. This is where I start squirming...

The code is clean for the most part, except for writing the foreign key. It looks like:

 @Override
 protected void fillInRow(Row row, MembershipFeeStructureModel record) {
 Cell primaryKeyCell = row.getCell(this.primaryKeyCellIdx),
 foreignKeyCell = row.getCell(this.foreignKeyCellIdx),
 startDateCell = row.getCell(2),
 endDateCell = row.getCell(3),
 enrollmentStartDateCell = row.getCell(4),
 enrollmentEndDateCell = row.getCell(5),
 annualAmountCell = row.getCell(6),
 annualDiscountCell = row.getCell(7);
 
 if (SMDSpreadsheetUtils.IsRowEmpty(row)) { 
 primaryKeyCell = row.createCell(this.primaryKeyCellIdx);
 foreignKeyCell = row.createCell(this.foreignKeyCellIdx);
 startDateCell = row.createCell(2);
 endDateCell = row.createCell(3);
 enrollmentStartDateCell = row.createCell(4);
 enrollmentEndDateCell = row.createCell(5);
 annualAmountCell = row.createCell(6);
 annualDiscountCell = row.createCell(7);
 }
 
 primaryKeyCell.setCellValue(record.getId());
 // this line right here...
 foreignKeyCell.setCellValue((int)this.childModelListDict.find { int key, List<MembershipFeeStructureModel> value ->
 return (value.find { MembershipFeeStructureModel childModel -> return childModel.getId() == record.getId() } != null)
 }.key);
 startDateCell.setCellValue(SMDDateUtils.ToDateString(record.getStartDate()));
 endDateCell.setCellValue(SMDDateUtils.ToDateString(record.getEndDate()));
 enrollmentStartDateCell.setCellValue(SMDDateUtils.ToDateString(record.getEnrollmentStartDate()));
 enrollmentEndDateCell.setCellValue(SMDDateUtils.ToDateString(record.getEnrollmentEndDate()));
 annualAmountCell.setCellValue(record.getAnnualFee());
 annualDiscountCell.setCellValue(record.getDiscountAmount());
 }

Is this necessarily a code smell, to have to query the dictionary for a value condition, or am I worrying for nothing?

asked May 10, 2023 at 6:29
3
  • 5
    To quote Donald Knuth: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." - but does this code fall into the 97% or 3%? Commented May 10, 2023 at 7:06
  • 1
    What specific problem do you have with this code? "Worrying" is not a problem we can solve for you. Commented May 10, 2023 at 13:16
  • 1
    To be honest, @BenCottrell already answered your question. There is no point in worrying about performance. Measure first. Then assess whether this is a valid worry. Commented May 10, 2023 at 15:51

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.