Skip to content

♻️ Remove broken reference to this inside static class method#29695

Closed
kristoferbaxter wants to merge 1 commit intoampproject:masterfrom
kristoferbaxter:remove-this-static
Closed

♻️ Remove broken reference to this inside static class method#29695
kristoferbaxter wants to merge 1 commit intoampproject:masterfrom
kristoferbaxter:remove-this-static

Conversation

@kristoferbaxter
Copy link
Copy Markdown
Contributor

This reference doesn't exist within a static class.

@amp-owners-bot
Copy link
Copy Markdown

amp-owners-bot bot commented Aug 5, 2020

Hey @Jiaming-X, @jeffkaufman! These files were changed:

extensions/amp-ad-network-adsense-impl/0.1/responsive-state.js

*/
static createIfResponsive(element) {
if (
!this.isContainerWidth_ &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While this PR doesn't change behavior, do we know what the original intent was?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should be changed to an instance method, and the intent is to check this.isContainerWidth_ on the instance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My take is the intent is not clear.

I read this as a constructor replacement either returning null or a new class instance based on some conditions. One of these conditions is not possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Additionally, refactoring this to no longer be static is significantly altering it and callers usage.

When @Jiaming-X has a chance to review, it would be good to learn what they prefer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Lastly, there is no test covering this usage.

If there is an intent for this code to branch the logic within the static method, lets also add a test covering it.

Copy link
Copy Markdown
Member

@samouri samouri Aug 5, 2020

Choose a reason for hiding this comment

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

+1, intent isn't clear here. I'm fine with submitting this as a fix to unblock esm/strict modes. But we should also follow up to figure out what intent was.

*/
static createIfResponsive(element) {
if (
!this.isContainerWidth_ &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should be changed to an instance method, and the intent is to check this.isContainerWidth_ on the instance.

@kristoferbaxter
Copy link
Copy Markdown
Contributor Author

The only times this static method is called are...

  1. https://github.com/ampproject/amphtml/blob/master/extensions/amp-ad-network-adsense-impl/0.1/amp-ad-network-adsense-impl.js#L131

This is within the AmpAdNetworkAdsenseImpl constructor, where there is no reference to a previous ResponsiveState meaning the value cannot be set.

  1. https://github.com/ampproject/amphtml/blob/master/extensions/amp-ad-network-adsense-impl/0.1/responsive-state.js#L134 via upgradeToResponsive_, where the data-auto-format value is set before invoking, which I believe causes the other branch of this logic to be tested.

Overall I find the code a bit confusing to read, but perhaps someone more knowledgeable of its intended use can tell us what the original intent was since its impossible for a static class member to read a value from an instance.

Copy link
Copy Markdown
Member

@samouri samouri left a comment

Choose a reason for hiding this comment

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

Approving with the caveat that we should also follow up to make sure its WAI.

@jridgewell
Copy link
Copy Markdown
Contributor

My reading is that isContainerWidth_ is being used as a "already initialized on this instance" value, but I'm also unsure. /cc @calebcordry

@calebcordry
Copy link
Copy Markdown
Member

I think there are two ways the ad can be upgraded. One is to full viewport width, and the other (for desktop) is to container width. I think that this check was intended to not upgrade to full width if it was already determined to be container width, but the way it is written I don't think the check is actually doing anything.

Jiaming should be able to confirm tomorrow (London time zone).

@Jiaming-X
Copy link
Copy Markdown
Member

Hey everyone, just to provide more context here:
The handover implementation for Ad Size Optimization was mixed with ResponsiveState instance methods and static methods for modifying element to support multiple flows -- (mobile + desktop), (2 types of full-width responsive tags, non responsive tag),

The original flow of Ad Size Optimization used maybeUpgradeToResponsive to upgrade element to full width for both mobile + desktop(previously broken) traffics. It created broken user experience like https://buganizer.corp.google.com/issues/150752818

To fix these issues, I added one more subflow convertToContainerWidth_
inside maybeUpgradeToResponsive for the desktop users with full-width responsive tag without breaking the existing flows and changing the ad size differently.

For isContainerWidth_, it is used to differentiate the desktop users with full-width responsive tag in various places like:
1 alignToViewport to identify if the request is for desktop traffics and adjust the CSS style differently.
2 isValidElement for the verification check
3 buildCallback which calls attemptToMatchResponsiveHeight API to actually change the width to viewport width, and adjust height.
4. convertToContainerWidth_ starts the new sub flow
With the changes, the build/unit tests/testing in local mode worked for the test cases.

To move forward, the sub flow convertToContainerWidth_ might need different implementation. To unblock you from the new build, we could disable to flow first and remove the this.isContainerWidth_ usage from static methods. I can change the implementation later in a separate thread. For my case, it will impact 0.3% of the traffic. https://screenshot.googleplex.com/OAtWpn9fvpt

WDYT?

Related links:
Original bug: https://buganizer.corp.google.com/issues/150752818
github changes: #28565
github link: #28403

@kristoferbaxter
Copy link
Copy Markdown
Contributor Author

I'm going to close this issue and defer to @Jiaming-X's PR. Thanks for the input everyone!

As a followup, we should invest in better test coverage of this space so refactoring can begin.

@kristoferbaxter kristoferbaxter deleted the remove-this-static branch August 6, 2020 17:39
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