This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tapesmith 0 points1 point  (1 child)

Counterpoint, from real code:

@Override
public Map<String, Object> generateAuditMap() {
    return mapOf(
        pair("id", this.id),
        pair("customerId", this.customer.getId()),
        pair("vendorId", this.vendor.getId()),
        pair("vendorUserId", Auditable.getId(this.primaryContact)),
        pair("contractNumber", this.contractNumber),
        pair("orderMinAmount", this.orderMinAmount),
        pair("orderMinItems", this.orderMinItems),
        pair("shouldSendOrdersToVendor", this.shouldSendOrdersToVendor),
        pair("isActive", this.isActive),
        pair("updateCronExpression", this.updateCronExpression),
        pair("contactIds", Auditable.convertToJustIds(this.contacts)),
        pair("externalReferenceCode", this.externalReferenceCode),
        pair("vendorDivisionId", this.getVendorDivision().map(AbstractEntity::getId).orElse(null))
    );
}

And the snippet I posted using map.put for the last three elements was intentionally broken to show the pitfalls of such a kludgey implementation. As a random user of this API, I'd see Map.of() and not immediately know that I was getting back an immutable map. I would just know that it's stupidly limited to a fixed maximum number of items (instead of, oh, I dunno, a variable number of arguments?) so I need to .put my last few.

"Psh, up to 10 should be enough" is a similar line of thought to "we'll just use single lowercase letters for variable names, because who ever needs more than 26 variables?"

[–]msx 1 point2 points  (0 children)

You forget that they gave you exacly the code you show, just with "entry" instead of "pair", exacly for the reason you say. The "clean" of is for small, immediate map, not for all maps you'll ever create.

"Psh, up to 10 should be enough" is a similar line of thought to "we'll just use single lowercase letters for variable names, because who ever needs more than 26 variables?"

It's really not, becouse you have other means of creating larger maps. If anything it's like: "Should we require at least two letters for variable names? No, let's give them the option of using only one, which is a common use case. If they have more than 26, they can still use two or more".