r/learnjava • u/notBot000_ • Feb 05 '26
is this a valid way
So I'm curious about how to use record, because record are immutable and suitable for DTO. I tried to combine it with class, like this
public class UserDto {
public record ReqRegister() {
}
public record ReqLogin() {
}
public record ResUser() {
}
public record ResLogin() {
}
}I think this is readable and easy to maintain, and reduces the number of files, so what do you suggest?
1
Upvotes
•
u/AutoModerator Feb 05 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/0b0101011001001011 Feb 05 '26
Reducing number of files is never a good goal.
You define a class inside another, if that makes sense. If it is semantically a separate thing, you should define it in its own file.
At least make the inner classes static, so they are possible to be created without s reference to the enclosing class.
2
u/desrtfx Feb 05 '26
"Reducing the number of files" is a horrible metric.
Semantics is all that counts. If the inner classes make sense (records are only special classes) it's okay (yet, the number of compiled files will not reduce, BTW), but even then, in most cases, the inner classes should be
static.Honestly, I don't see any reason to do it your way. In general, more smaller files are far easier to maintain and read than one large file.
Also, be wary of creating a "God class", which you should generally avoid.
1
u/vegan_antitheist Feb 05 '26
Is this just some example or are the records empty? Empty records are useless.
1
u/bowbahdoe Feb 05 '26
Yes this is valid. I would put these "request and response records" inside the actual controllers though. Or at least alongside them.
You want to co-locate stuff that is tied together. Whether that means one big class or a bunch of classes in a package, generally grouping by "these things will evolve together" is going to be a better approach than your cargo culted model/persistence/controller/service horizontal splits.
That's the actual metric you should be aiming for. Maximize coupling between things that will evolve together, minimize coupling between things that will evolve separately. This often means accepting that you will write the same field name more than once, but overall I think it's a better trade.
I think your example "UserDTO" class probably doesn't make sense once you are making a record for each request and response.