Possible optimized record construction in order to use as DTO

Roel Meeuws r.j.meeuws at gmail.com
Thu Jan 11 12:48:15 UTC 2024


Dear all,

This is my first message to this list, so I hope I am not proposing
something which has already been discussed before. But here goes.

In several projects at different companies I have seen the use of big bean
classes with lots of data inside and getters, setters, toString, equals,
hashcode added. In some projects the boilerplate was mitigated with Lombok,
but of course recent JDKs provide the notion of Records.

Now records are great data carriers with much less boilerplate, however,
there is a particular problem when constructing them for very big sets.
Consider a dto representing a line in some EU legal obligation report (of
which there are many) that may heve hundreds of fields. When constructing
such an object there is no idea of what parameter is being filled in in the
code that is constructing the object

e.g.

record IrritatinglyBigEUReportLine(
  long processingId;
  ZonedDateTime reportTime;
  String firstName;
  String lastName;
  String leiCode;
  String legalReportingId;
  BigDecimal riskAmount;
  BigDecimal mifidRiskAmount;
  BigDecimal mifid2FinancialRiskAmount;
  BigDecimal mifid2SomeOtherAmount;
  ......
) {}

now let's construct this:
var line = new IrritatinglyBigEUReportLine(
  12345,
  ZonedDateTime.now(),
  "John",
  "Doe",
  "529900T8BM49AURSDO55",
  BigDecimal.valueOf("100.0"),
  BigDecimal.valueOf("100.0"),
  BigDecimal.valueOf("100.0"),
  BigDecimal.valueOf("100.0"), // anyone knows what this value is here?
  ...
);

// also this will give a compile error in my IDE, which parameter is
missing now?

Could we introduce a better way of initialization like the following, which
is like the `with`-er syntax, I have read Brian Goetz writing about.
var line = new IrritatingLuBigEUReportLine with {
  processingId =12345;
  reportTime = ZonedDateTime.now();
  firstName = "John";
  lastName = "Doe";
  leiCode = "529900T8BM49AURSDO55";
  legalReportingId = "ERE43434452ERERTTT";
  riskAmount = BigDecimal.valueOf("100.0");
  mifidRiskAmount = BigDecimal.valueOf("100.0");
  mifid2FinancialRiskAmount = BigDecimal.valueOf("100.0");
  mifid2SomeOtherAmount = BigDecimal.valueOf("100.0");
  ...
};

Roel

--------------------------------------------
Roel Meeuws
Email: r.j.meeuws at gmail.com
Mob. phone: +31 (0)6 10 82 44 01
--------------------------------------------

<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.www.avg.com
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240111/075b1d02/attachment.htm>


More information about the amber-dev mailing list