-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@AllArgsConstructor Is Dangerous! #2094
Comments
This is a matter of perception and preference. Once you know the order of
the fields changes the order of the constructor arguments it's actually
quite useful to be able to reorder the arguments as you wish.
If you want to create your objects in a way that does not depend on the
order of the fields, use `@Builder`
…On Wed, Apr 10, 2019 at 2:53 PM R. Matt McCann ***@***.***> wrote:
The Problem
@AllArgsConstructor <https://github.com/AllArgsConstructor> seemingly
generates the order of constructor arguments based on the position of
fields within the actual source file. If one has two fields with the same
type, the seemingly innocuous change of switching the declaration order
will produce compile-able, but buggy code as the constructor arg order is
now changed.
The Solution
Generate arg order using something more predictable, such as alphabetical
ordering; or better yet, deprecate @AllArgsConstructor
<https://github.com/AllArgsConstructor> :)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2094>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAKCRcymbAke8U_H-AN1mCLUaIDfiSI9ks5vfd7LgaJpZM4cm7Ox>
.
--
"Don't only practice your art, but force your way into it's secrets, for it
and knowledge can raise men to the divine."
-- Ludwig von Beethoven
|
I think it's implemented this way on purpose. It is reasonable to let the parameter order follow the order of field declarations. |
There's no easy answer on this topic, but I'm in the camp that this feature is dangerous. This essentially shifts compile time errors into runtime bugs. I avoided scripting languages like NodeJs, PHP, Ruby on Rails because I don't want runtime headaches. I'm an experienced developer, so personally, this feature is not dangerous to me. It's in consideration to an ecosystem that includes millions of junior developers as well as enterprises that already struggle enough with code quality. This annotation is simply a loaded gun.. |
Hi, as a junior developer, I've been struggling with the @AllArgsConstructor and @builder problem. I know that using @AllArgsConstructor may cause issues with reordering arguments or fields. While reading through the official @builder documentation, I came across this part "If a class is annotated, then a package-private constructor is generated with all fields as arguments (as if @AllArgsConstructor(access = AccessLevel.PACKAGE) is present on the class)." |
ˋ@builderˋ needs some constructor with all arguments internally to actually create the resulting object. Yet - as the builder is generated together with the constructor any changes in the parameter order of the fields and thus the constructor are automatically considered and respected. So the generated builder is always in sync with the constructor. |
You can use |
The Problem
@AllArgsConstructor seemingly generates the order of constructor arguments based on the position of fields within the actual source file. If one has two fields with the same type, the seemingly innocuous change of switching the declaration order will produce compile-able, but buggy code as the constructor arg order is now changed.
The Solution
Generate arg order using something more predictable, such as alphabetical ordering; or better yet, deprecate @AllArgsConstructor :)
The text was updated successfully, but these errors were encountered: