isCredit is the common boolean value. Please pay attantion that we put the same values, (but crossing) on debet and credit side depends on boolean
.setDebetSide(new DebetSideDto()
.setPayerINN(isCredit ? record.getInnContractor() : record.getClientInn())
.setPayerName(isCredit ? record.getNameContractor() : record.getClientName())
.setCreditSide(new CreditSideDto()
.setReceiverINN(isCredit ? record.getClientInn() : record.getInnContractor())
.setReceiverName(isCredit ? record.getClientName() : record.getNameContractor())
Try and avoid double calls, so set additional variables in an
ifconstruct, then call both functions using these variables:This has the following advantages:
As always it sacrifices some conciseness for readability; usually ternary operations are removed to enhance readability.
One critique here is that
debitSideDtowas probably designed to contain an invalid state; it is required to call setters to get to a valid state. It's probably better to assign required values upon construction. In that case the constructor needs to be called within theifstatement. The variable doesn't have to be assigned any value before theif; the compiler is fine as long as it is assigned a value - it may even be declaredfinal:Alternatively you could create variables for
payerName,payerInn,receiverNameandreceiverInnrather than variables for the Dto's of course. That would not remove the invalid states of the Dto's but it would localize that particular issue. On the other hand I'd prefer fewer variables, so I chose to use the Dto's to do the heavy lifting.I've added some comments though as without these variables the roles are not very clear.
This is a localized change of course. In practice I would be wondering why such a reversal of roles is required at this late a stage. I'd be asking questions about the general design (it might be OK, but to me it is a bit of a red flag).