Skip to content

Suggestion: Increases readability of PageableExecutionUtils code #3103

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

Closed
erie0210 opened this issue Jun 6, 2024 · 6 comments
Closed

Suggestion: Increases readability of PageableExecutionUtils code #3103

erie0210 opened this issue Jun 6, 2024 · 6 comments
Assignees
Labels
type: task A general task

Comments

@erie0210
Copy link
Contributor

erie0210 commented Jun 6, 2024

I noticed PageableExecutionUtils code could potentially benefit from refactoring. I found it too complex due to multiple branches of if statements. I think these adjustments could improve readability of the codebase. I am open to any feedback or suggestions you might have. I would be happy to open a pull request with these changes if needed. Please let me know your thoughts.

Here's the suggested code. All tests passed.

Thank you.

AS_IS

public abstract class PageableExecutionUtils {

	private PageableExecutionUtils() {}

	public static <T> Page<T> getPage(List<T> content, Pageable pageable, LongSupplier totalSupplier) {

		Assert.notNull(content, "Content must not be null");
		Assert.notNull(pageable, "Pageable must not be null");
		Assert.notNull(totalSupplier, "TotalSupplier must not be null");

		if (pageable.isUnpaged() || pageable.getOffset() == 0) {

			if (pageable.isUnpaged() || pageable.getPageSize() > content.size()) {
				return new PageImpl<>(content, pageable, content.size());
			}

			return new PageImpl<>(content, pageable, totalSupplier.getAsLong());
		}

		if (content.size() != 0 && pageable.getPageSize() > content.size()) {
			return new PageImpl<>(content, pageable, pageable.getOffset() + content.size());
		}

		return new PageImpl<>(content, pageable, totalSupplier.getAsLong());
	}
}

TO_BE

public abstract class PageableExecutionUtils {

	private PageableExecutionUtils() {}

	public static <T> Page<T> getPage(List<T> content, Pageable pageable, LongSupplier totalSupplier) {

		Assert.notNull(content, "Content must not be null");
		Assert.notNull(pageable, "Pageable must not be null");
		Assert.notNull(totalSupplier, "TotalSupplier must not be null");

		if (pageable.isUnpaged() || isFirstPageWithLessThanOneFullPage(content, pageable)) {
			return new PageImpl<>(content, pageable, content.size());
		}

		if (isSubsequentPageWithLessThanOneFullPage(content, pageable)) {
			return new PageImpl<>(content, pageable, pageable.getOffset() + content.size());
		}

		return new PageImpl<>(content, pageable, totalSupplier.getAsLong());
	}

	private static <T> boolean isFirstPageWithLessThanOneFullPage(List<T> content, Pageable pageable) {
		return pageable.getOffset() == 0 && pageable.getPageSize() > content.size();
	}

	private static <T> boolean isSubsequentPageWithLessThanOneFullPage(List<T> content, Pageable pageable) {
		return pageable.getOffset() != 0 && !content.isEmpty() && pageable.getPageSize() > content.size();
	}
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 6, 2024
@mp911de
Copy link
Member

mp911de commented Jun 7, 2024

Readability is subjective. I find isSubsequentPageWithLessThanOneFullPage hard to parse although it makes the concept explicit. It's always a balance between tradeoffs.

@erie0210
Copy link
Contributor Author

erie0210 commented Jun 7, 2024

Thank you for your feedback on the naming of the method isSubsequentPageWithLessThanOneFullPage. I understand the importance of balancing explicitness and readability in method names. I took the current name directly from the test code to maintain consistency and clarity regarding its functionality. However, I agree that the name can be quite lengthy and difficult to parse.

If you think it's worth refactoring, I could try changing the name. @mp911de

@schauder
Copy link
Contributor

If you think it's worth refactoring, I could try changing the name. @mp911de

In general a refactoring would be a good thing.

Maybe the solution for good names comes with splitting the methods differently, like separating the question first/subsequent Page from the question of full/partial page.

@schauder schauder added type: enhancement A general enhancement type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Jun 17, 2024
@schauder
Copy link
Contributor

Do you want to come up with a PR?

@erie0210
Copy link
Contributor Author

Sure, thank you! I'll work on the PR and let you know. @schauder

erie0210 added a commit to erie0210/spring-data-commons that referenced this issue Jun 24, 2024
@erie0210
Copy link
Contributor Author

Submitted PR for the issue above. Your feedback is appreciated! :) Thank you! @schauder

schauder added a commit that referenced this issue Jul 1, 2024
Removing `isSubsequentPage`. It is another name for !isFirstPage suggesting a different meaning which isn't there.

Extracting `if (isFirstPage(pageable)) {`, further structuring the decision tree.

See #3103
Original pull request #3113
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants