Skip to content

Conversation

ronfroy
Copy link
Contributor

@ronfroy ronfroy commented Sep 13, 2018

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

@ronfroy ronfroy changed the title remove dead code and useless else [OptionsResolver] remove dead code and useless else Sep 13, 2018
@@ -526,11 +526,11 @@ public function addAllowedValues($option, $allowedValues)
}

if (!isset($this->allowedValues[$option])) {
$this->allowedValues[$option] = $allowedValues;
Copy link
Member

Choose a reason for hiding this comment

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

If we make this change in master, which uses modern PHP versions, we could even simplify it more:

-    if (!isset($this->allowedValues[$option])) {
-        $this->allowedValues[$option] = $allowedValues;
-    } else {
-        $this->allowedValues[$option] = array_merge($this->allowedValues[$option], $allowedValues);
-    }

+    $this->allowedValues[$option] = array_merge($this->allowedValues[$option] ?? array(), $allowedValues);

Copy link
Contributor

Choose a reason for hiding this comment

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

Those if/else constructs aren't useless: they prevent a unnecessary call to array_merge, which is a costly function, even though the damage should be limited when it is called with an empty array as argument.
As such, these changes don't do anything valuable, they just reduce the number of lines of code at the expense of performance, so they should be left untouched IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@skalpa it is a really minor cost.

Copy link
Member

Choose a reason for hiding this comment

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

a minor cost repeated hundreds or thousands times when building complex forms with many fields becomes major

Copy link
Contributor

@linaori linaori Sep 14, 2018

Choose a reason for hiding this comment

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

I agree with stof on this one. Perhaps it would be a good idea to add a comment in the code as of why this was done, instead of always calling array_merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

}

$this->allowedValues[$option] = array_merge($this->allowedValues[$option], $allowedValues);
Copy link
Member

Choose a reason for hiding this comment

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

Also, we can remove the array check above and use (array) $allowedValues instead, just like addAllowedTypes() method.

@skalpa
Copy link
Contributor

skalpa commented Sep 13, 2018

@yceruto I understand that the performance cost I discussed is minor. However your changes don't add any functionality and some would argue that you are just changing code for the sake of changing code.
Removing the dead code is useful, but I believe that there is no point in fixing code that isn't broken to replace it with a version that is, even marginally, worse.

What do you think was wrong with the if/else statements ?

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Sep 17, 2018
@nicolas-grekas nicolas-grekas changed the base branch from master to 3.4 September 17, 2018 17:29
@nicolas-grekas
Copy link
Member

Thank you @ronfroy.

@nicolas-grekas nicolas-grekas merged commit 0c1484b into symfony:3.4 Sep 17, 2018
nicolas-grekas added a commit that referenced this pull request Sep 17, 2018
…froy)

This PR was submitted for the master branch but it was squashed and merged into the 3.4 branch instead (closes #28458).

Discussion
----------

[OptionsResolver] remove dead code and useless else

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

<!--
remove dead method and useless else
-->

Commits
-------

0c1484b [OptionsResolver] remove dead code and useless else
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.

8 participants