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?
- 
 5To 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%?Ben Cottrell– Ben Cottrell2023年05月10日 07:06:24 +00:00Commented May 10, 2023 at 7:06
 - 
 1What specific problem do you have with this code? "Worrying" is not a problem we can solve for you.Greg Burghardt– Greg Burghardt2023年05月10日 13:16:26 +00:00Commented May 10, 2023 at 13:16
 - 
 1To 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.Greg Burghardt– Greg Burghardt2023年05月10日 15:51:29 +00:00Commented May 10, 2023 at 15:51