Skip to main content
Image

r/java


Should a request DTO be a value class (JEP 401)?
Should a request DTO be a value class (JEP 401)?

In a year or so, we'll need to recalibrate our intuition about which kind of type to use for each class. Originally, we used classes for everything. Then we got record, and now we get another keyword, value, which expands the number of options we have when defining a type.

I remember that in C#, there's always the question: "Should I use a class, a record, a readonly record, a struct, or something else"? They have some guidelines that consider not only identity semantics but also the size of objects (i.e., how many fields they have and how much memory they occupy).

So, if I just need a type to deserialize JSON from an HTTP request, should I use a value type? Or does it also depend on the number of fields? For example, if it has 15 int fields, should I still make it a value type if it is semantically a value, even if copying it could potentially be more expensive than passing a reference (assuming that's how it works)?


Advertisement: Getting holiday-ready starts now! Schedule your measurements or quotes today and we'll have your install project done before Thanksgiving.
Getting holiday-ready starts now! Schedule your measurements or quotes today and we'll have your install project done before Thanksgiving.
  • Image
    Getting holiday-ready starts now! Schedule your measurements or quotes today and we'll have your install project done before Thanksgiving.
  • Image
    Getting holiday-ready starts now! Schedule your measurements or quotes today and we'll have your install project done before Thanksgiving.
  • Image
    Getting holiday-ready starts now! Schedule your measurements or quotes today and we'll have your install project done before Thanksgiving.
  • Image
    Getting holiday-ready starts now! Schedule your measurements or quotes today and we'll have your install project done before Thanksgiving.



Killing Reflection at Build Time: Avro, Codename One and Java Code Generation (Foojay Podcast #103)
Killing Reflection at Build Time: Avro, Codename One and Java Code Generation (Foojay Podcast #103)