Skip to content
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

Improve condition for more readability #2859

Merged
merged 5 commits into from
Oct 16, 2024

Conversation

ngocnhan-tran1996
Copy link
Contributor

@ngocnhan-tran1996 ngocnhan-tran1996 commented Oct 15, 2024

I read code in package org.springframework.amqp.core and I think some codebase conditions are too complex and we can make it simple for readability purpose.

That's why I create draft PR and I wonder whether I can continue to do this, at least in package org.springframework.amqp.core. Please let me know your thoughts

if (adminArgs.length > 0 && !(adminArgs.length == 1 && adminArgs[0] == null)) {
admins.addAll(Arrays.asList(adminArgs));
}
this.declaringAdmins = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not OK. You are breaking an atomicity on the property and we may and up with a ConcurrentModificationException from somewhere.

Another my concern that I don't see a reason in so complex logic here.
The declaringAdmins property must be final for better performance.

The code of this method should be as simple as:

public void setAdminsThatShouldDeclare(Object... adminArgs) {
		Assert.notNull(adminArgs, "'admins' must be provided");
		Assert.noNullElements(adminArgs, "'admins' cannot contain null elements");
		this.declaringAdmins.addAll(Arrays.asList(adminArgs));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's more concise but it will break testcase. Will we have any plan to improve codebase in next version? If not, I am not a big fan of else/else-if condition and I will try to contribute that reduce else/else-if condition for more readability

public void testAddRemove() {
Queue queue = new Queue("foo");
ConnectionFactory cf = mock(ConnectionFactory.class);
RabbitAdmin admin1 = new RabbitAdmin(cf);
RabbitAdmin admin2 = new RabbitAdmin(cf);
queue.setAdminsThatShouldDeclare(admin1, admin2);
assertThat(queue.getDeclaringAdmins()).hasSize(2);
queue.setAdminsThatShouldDeclare(admin1);
assertThat(queue.getDeclaringAdmins()).hasSize(1);
queue.setAdminsThatShouldDeclare(new Object[] {null});
assertThat(queue.getDeclaringAdmins()).hasSize(0);
queue.setAdminsThatShouldDeclare(admin1, admin2);
assertThat(queue.getDeclaringAdmins()).hasSize(2);
queue.setAdminsThatShouldDeclare();
assertThat(queue.getDeclaringAdmins()).hasSize(0);
queue.setAdminsThatShouldDeclare(admin1, admin2);
assertThat(queue.getDeclaringAdmins()).hasSize(2);
queue.setAdminsThatShouldDeclare((AmqpAdmin) null);
assertThat(queue.getDeclaringAdmins()).hasSize(0);
queue.setAdminsThatShouldDeclare(admin1, admin2);
assertThat(queue.getDeclaringAdmins()).hasSize(2);
queue.setAdminsThatShouldDeclare((Object[]) null);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is fine to keep then if for null check to avoid a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this changed file takes more your energy reviewing, so I will revert it and create a new PR if we have a plan for breaking change in next version.

In this PR, I will try to improve if-else without breaking change and equals methods for performance in module spring-amqp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Sounds like a plan.

Copy link
Member

@artembilan artembilan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK with me.
Do you plan to add more changes?
Or we are good to turn this into a real PR and merge?
Thanks

@ngocnhan-tran1996
Copy link
Contributor Author

I changed more files and I think it's ready to turn into a real PR.

@ngocnhan-tran1996 ngocnhan-tran1996 marked this pull request as ready for review October 15, 2024 18:23
@artembilan artembilan added this to the 3.2.0-RC1 milestone Oct 16, 2024
@artembilan artembilan merged commit d87f268 into spring-projects:main Oct 16, 2024
3 checks passed
@artembilan
Copy link
Member

@ngocnhan-tran1996 ,

thank you for contribution; looking forward for more!

@ngocnhan-tran1996 ngocnhan-tran1996 deleted the improve-condition branch October 17, 2024 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants