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

[Form] Refactor DefaultChoiceListFactory #29731

Closed
wants to merge 3 commits into from

Conversation

vudaltsov
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

In this PR I refactored a really messy DefaultChoiceListFactory code.
Ping @HeahDude, @xabbuh.

@vudaltsov
Copy link
Contributor Author

vudaltsov commented Dec 31, 2018

Blackfire comparison for a form with 3 different choices.

image

So, it became slower a bit because of increased number of method calls basically. Is it too much for a DX improvement?

@nicolas-grekas nicolas-grekas added this to the next milestone Jan 3, 2019
fabpot added a commit that referenced this pull request Jan 4, 2019
This PR was merged into the 3.4 branch.

Discussion
----------

[Form] Fix FormDefaultChoiceListFactory test

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29731 (review)
| License       | MIT
| Doc PR        | n/a

`$groupBy` is either `null` or `callable`. Passing `array()` is wrong.

Commits
-------

b8f6390 Fixed groupBy argument value in DefaultChoiceListFactoryTest
@vudaltsov
Copy link
Contributor Author

Rebased

@stof
Copy link
Member

stof commented Jan 25, 2019

So, it became slower a bit because of increased number of method calls basically. Is it too much for a DX improvement?

What kind of DX improvement is this PR doing ? Developers don't care about the internal implementation of the factory. They use the factory when developing their forms, they don't re-implement it.

@vudaltsov
Copy link
Contributor Author

I meant Symfony developer's DX. I started working on #5136 when I came across the DefaultChoiceListFactory. It took me about 2 hours to get, what's going on there.

@vudaltsov
Copy link
Contributor Author

Thank you @stof , I improved the phpdoc

@stof
Copy link
Member

stof commented Jan 25, 2019

I meant Symfony developer's DX. I started working on #5136 when I came across the DefaultChoiceListFactory. It took me about 2 hours to get, what's going on there.

But does your refactoring change that at all ? The building of the list is still exactly the same, but moved in a different class AFAICT.

@vudaltsov
Copy link
Contributor Author

@stof, I certainly didn't change the algorithm, but I claim that I improved the readability:

  1. Stateful builder instead of stateless static factory methods. The builder incapsulates the logic of constructing a view object and delegates small tasks to private methods.
  2. Less arguments. static DefaultChoiceListFactory::addChoiceView used to have 9 arguments which is almost impossible to keep in mind. New ChoiceListViewBuilder::addView has only 5 args which is still bad but almost two times easier to process in mind. That's because we still have to pass arrays of choices by reference due to the recursive nature of the building process.
  3. Better method names. Old addChoiceViewsGroupedBy & addChoiceViewGroupedBy vs new addViewsGroupedByCallable & addViewsFromStructuredValues.
  4. Label, index, attr, preferred choices are now resolved in separate methods which improves readability a lot because of early return statements instead of elseif structures.
  5. Optimization: removing empty groups views used to happen for any $groupBy type (https://github.com/symfony/symfony/pull/29731/files#diff-ca1123eff9e0aa207aaec667aa16c8e8L99), now it happens only for callable $groupBy (https://github.com/symfony/symfony/pull/29731/files#diff-dbdec4be38173e7941f77b7b9292313eR111), because for structured choices we never create empty groups. I also moved it to a separate method to avoid code duplication.

@vudaltsov
Copy link
Contributor Author

@nicolas-grekas , @xabbuh , @stof , @HeahDude , should I close this? Are my points above not convincing?

@xabbuh
Copy link
Member

xabbuh commented Feb 9, 2019

Please don't close it yet. I would like to have a look at the details first. I simply didn't have the time to do that yet. Sorry.

@fabpot
Copy link
Member

fabpot commented Feb 21, 2019

I'm not convinced, and it makes merging older branches more difficult (not even talking about the performance here).

@HeahDude
Copy link
Contributor

HeahDude commented Mar 12, 2019

Thank you @vudaltsov, I'm sorry but I also tend to agree, that the benefit is too small so it is worth it.

👎 too on this one.

@vudaltsov
Copy link
Contributor Author

Okay, no problem. I finally also agree with all you. My change does not really change anything :)

@vudaltsov vudaltsov closed this Mar 13, 2019
@vudaltsov vudaltsov deleted the choice-list-view-builder branch March 13, 2019 08:54
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
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.

None yet

7 participants