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

One more check #6928

Closed
wants to merge 3 commits into from
Closed

One more check #6928

wants to merge 3 commits into from

Conversation

Will-I4M
Copy link
Contributor

@Will-I4M Will-I4M commented Oct 8, 2016

Without this check It's possible to have a "notice undefined indice xxx "-> thit error is preventing me from saving an attribute in my current installation.

Without this check It's possible to have a notice undefined indice xxx -> that error is preventing me from saving an attribute in my existing config.
@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Oct 8, 2016

CLA assistant check
All committers have signed the CLA.

if (isset($optionsArray['delete']) && $optionsArray['delete'][$optionId] == 1) {
unset($optionsArray['value'][$optionId]);
if (isset($optionsArray['delete']) && isset($optionsArray['delete'][$optionId])) {
if ($optionsArray['delete'][$optionId]== 1) {
Copy link
Contributor

@orlangur orlangur Oct 10, 2016

Choose a reason for hiding this comment

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

A simpler condition

if (!empty($optionsArray['delete'][$optionId])) {
    unset($optionsArray['value'][$optionId]);
}

will do the trick as well.

Copy link
Contributor Author

@Will-I4M Will-I4M Oct 10, 2016

Choose a reason for hiding this comment

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

empty() will consume more cpu cycle, and is not equivalent to isset. the purpose of this check is to avoid the notice, and it will. empty will do one more thing, it will skip the "unset" if $optionsArray['value'][$optionId] is set to '' (not possible here if it is set) .
IMHO magento 2 is slow enough to not use "simpler condition" that is actually more expensive ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

empty() will consume more cpu cycle

Nope, it is same to isset and always faster than isset+condition. Here we avoid three conditions with only one statement.

if $optionsArray['value'][$optionId] is set to '' (not possible here if it is set) .

Do you mean $optionsArray['delete'][$optionId] ? Please provide an exact case when behavior of new and old code differs. Surely !empty and isset are not equivalent but equally suitable for this logic.

Copy link
Contributor Author

@Will-I4M Will-I4M Oct 10, 2016

Choose a reason for hiding this comment

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

Please refer to the isset and empty source code (zend_isset_isempty_dim_prop_obj_handler_SPEC_UNUSED_* etc). empty is slower than isset, especially with notset arrays (~30%, and around 6% with set arrays). You use a boolean operator (!), so it's equivalent to :```

if (isset($optionsArray['delete'][$optionId]) & $optionsArray['delete'][$optionId]) {
unset($optionsArray['value'][$optionId]);
}

:).

Copy link
Contributor

Choose a reason for hiding this comment

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

Exactly, and this is more effective than initial condition

if (isset($optionsArray['delete']) && isset($optionsArray['delete'][$optionId]) && $optionsArray['delete'][$optionId] == 1)

and much shorter ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The PR has been updated with this optimisation.
Thanks !

if ($optionsArray['delete'][$optionId]== 1) {
unset($optionsArray['value'][$optionId]);
}
if (isset($optionsArray['delete'][$optionId]) & $optionsArray['delete'][$optionId]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

And why not use empty? :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here, theoretically the binary operator is only used if the first condition has been validated. But if you insist for using "empty", with another boolean operator, it is possible, but just slower.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think such nanoseconds really matter. I usually prefer empty to avoid notices and double conditions, like with isset. For the sake of readability ;)

Whatever variant you choose, could you please squash your changes into one commit and make a force push?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure !
It's not a double condition (&&) but a binary operator, like the "!" you use with empty.
Nano second, your right, but it's more than nothing, I think the code is readable :)

Copy link
Contributor Author

@Will-I4M Will-I4M Oct 13, 2016

Choose a reason for hiding this comment

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

Do you need I create a new simplified pull request ?

Copy link
Contributor

Choose a reason for hiding this comment

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

By double condition I mean a doubled $optionsArray['delete'][$optionId] expression. So, as to me !empty variant is more readable as it is two times shorter.

No need for new PR, just force push into this branch with clean commit history.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry don't know how to remove the old patch, clean the history or force push.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see... I don't know a way to force push via GitHub web UI as well. Let's leave it as is then.

@vrann vrann self-assigned this Mar 21, 2017
@vrann vrann added this to the March 2017 milestone Mar 21, 2017
@vrann
Copy link
Contributor

vrann commented Mar 21, 2017

@Will-I4M This is the duplicate of the #6707, thus closing this one.
Thank you for the contribution and participation in the Magento community.

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.

5 participants