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

Documentation: Collection Principles #174

Merged
merged 6 commits into from
Aug 6, 2021
Merged

Conversation

AlexandruGG
Copy link
Collaborator

@AlexandruGG AlexandruGG commented Aug 4, 2021

This PR adds a new section in the documentation to describe a few principles that are at the core of this package.

Also:

  • Update docs version in preparation for the release (5.0.0)

Related to #106 (comment).

@AlexandruGG
Copy link
Collaborator Author

@drupol hey! There are a few more things which I want to add here, but feel free to have a read of what's there so far and let me know what you think 😁

@drupol
Copy link
Collaborator

drupol commented Aug 4, 2021

Heya!

I'll make a global feedback at the end, but from what I've seen so far, it's great !

Have a great night

docs/pages/principles.rst Outdated Show resolved Hide resolved
docs/pages/principles.rst Show resolved Hide resolved

Sometimes you might want your ``Collection`` object to behave eagerly.
This is a legitimate need when you want to trigger side-effects, like persisting
entities to a database or sending a batch of emails.
Copy link
Collaborator

Choose a reason for hiding this comment

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

haha ! People I work with are using Collection for doing things like that. As I'm doing the code reviews most of the time, I always ask them to use a regular foreach loop instead.
Why?
Because I ultimately think that using Collection for doing tasks with side-effects is not the proper way to use this tool.
According to me, if you use Collection without using it's return value, then you're using it wrong.
I'm definitely aware that technically it will work, but semantically, it's worse than using a plain old foreach (or alike) loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm I get what you mean, I guess I'm just not a big fan of loops in general 😅 .

I see Collection::apply as a replacement for a separate foreach loop (ofc you have to use squash as well), because to me doing apply + foreach seems redundant.

In Rust, iterators have a for_each method which basically acts like apply but is not lazy, which means it can be a substitute for a loop when you want to do some side effects, especially at the end of a chain of other transformations: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3bcb45d0c7f99d3a91e98e65cf87c371

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This being said, I'll add a note that normal iteration via a foreach loop should be preferred for triggering side-effects

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok I see, thanks for the Rust example.

Maybe add a note in this direction:

Collection is a helper for making transformations to input data. Despite the fact that you can technically do side-effects in some operations(due to the fact that you can pass a custom Closure that can alter variables out of its scope) , it's better to avoid using the library for such endeavor, but rather to get the return value of a transformation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@drupol thank you for the suggestion, I've updated the copy now.

I'm still conflicted about the apply operation: maybe I'm looking at it the wrong way, but what is it's purpose if not to replace a foreach loop? I see it as the equivalent of a loop and similar to Laravel Collection's each method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There's no conflict, no worries. Apply has been made for that, but I don't advise to use it at all, that's it :)

@drupol
Copy link
Collaborator

drupol commented Aug 6, 2021

I'm wondering if we should have a word about performance. WDYT ?

I also updated the upcoming changelog and added a sentence about this new documentation page.

@AlexandruGG
Copy link
Collaborator Author

I'm wondering if we should have a word about performance. WDYT ?

I also updated the upcoming changelog and added a sentence about this new documentation page.

What are you thinking to mention regarding performace?

@drupol
Copy link
Collaborator

drupol commented Aug 6, 2021

I'm wondering if we should have a word about performance. WDYT ?
I also updated the upcoming changelog and added a sentence about this new documentation page.

What are you thinking to mention regarding performace?

That sometimes it might be slower using Collection than using simple loops and stuff, but using Collection add a semantical value to the code. And despite the fact that it might be slower sometimes, it adds a huge value in terms of readability and maintainability in the long term.

@AlexandruGG
Copy link
Collaborator Author

AlexandruGG commented Aug 6, 2021

I'm wondering if we should have a word about performance. WDYT ?
I also updated the upcoming changelog and added a sentence about this new documentation page.

What are you thinking to mention regarding performace?

That sometimes it might be slower using Collection than using simple loops and stuff, but using Collection add a semantical value to the code. And despite the fact that it might be slower sometimes, it adds a huge value in terms of readability and maintainability in the long term.

Hmm I'm not sure if this is the best place for this I think. But this gave me an idea for a separate section I can add in the future, where this would be a great mention. This section would compare how to do the same thing:

  1. the "imperative" way
  2. using Collection or Operations

$this::fromIterable([$a])
Collection::fromIterable([$a])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

bad copy-paste here 😅

@AlexandruGG AlexandruGG marked this pull request as ready for review August 6, 2021 07:25
@AlexandruGG
Copy link
Collaborator Author

@drupol I added the remaining sections I wanted now. Have a read through and let me know if it makes sense and any feedback you might have 😁

->contains('d'); // false

$result = Collection::fromIterable(range('a', 'c'))
->contains('a', 'z'); // true
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about adding an example with a AND and OR using filter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

in addition to this or instead of this example?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In addition 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done!

@AlexandruGG
Copy link
Collaborator Author

@drupol anything else on this? Otherwise it's ready from my side 🚀

@drupol drupol enabled auto-merge (squash) August 6, 2021 09:31
Copy link
Collaborator

@drupol drupol left a comment

Choose a reason for hiding this comment

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

All good !

@drupol drupol merged commit b3ac92d into master Aug 6, 2021
@drupol drupol deleted the feature/collection-principles branch August 6, 2021 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants