[wp-trac] [WordPress Trac] #58891: Simplify logic in `wp_get_loading_optimization_attributes()`
WordPress Trac
noreply at wordpress.org
Mon Jul 24 18:49:09 UTC 2023
#58891: Simplify logic in `wp_get_loading_optimization_attributes()`
-------------------------+--------------------------
Reporter: flixos90 | Owner: flixos90
Type: enhancement | Status: assigned
Priority: normal | Milestone: 6.4
Component: Media | Version:
Severity: normal | Resolution:
Keywords: needs-patch | Focuses: performance
-------------------------+--------------------------
Description changed by flixos90:
Old description:
> The `wp_get_loading_optimization_attributes()` function, which was
> introduced in 6.3 (see #58235) but inherited most of its logic from the
> now deprecated `wp_get_loading_attr_default()` function introduced in
> 5.5, contains quite complex and hard-to-follow logic. While early returns
> are usually a good pattern, with the excessive mix of individual if-
> clauses and early returns, the function is hard to follow and thus
> complicated to fully understand, which makes it challenging for
> maintenance.
>
> In order to decentralize the knowledge and understanding from just the
> few people that were involved in its implementation so far, this ticket
> aims to rewrite the function using a more coherent approach, which avoids
> most of the early returns and potentially uses a `switch` clause for the
> different context values.
>
> While refactoring on its own is generally discouraged in WordPress core,
> this refactoring will benefit, simplify, or even enable several other
> tickets.
>
> It should however not be implemented as part of any of those tickets,
> since that could easily lead to oversights due to the complex logic. The
> goal of this ticket should be to refactor the logic in a way that all
> existing tests pass without any modifications. Coverage is quite
> extensive on this function (at least that!), so this is a good framework
> to follow here.
New description:
The `wp_get_loading_optimization_attributes()` function, which was
introduced in 6.3 (see #58235) but inherited most of its logic from the
now deprecated `wp_get_loading_attr_default()` function introduced in 5.5,
contains quite complex and hard-to-follow logic. While early returns are
usually a good pattern, with the excessive mix of individual if-clauses
and early returns, the function is hard to follow and thus complicated to
fully understand, which makes it challenging for maintenance.
In order to decentralize the knowledge and understanding from just the few
people that were involved in its implementation so far, this ticket aims
to rewrite the function using a more coherent approach, which avoids most
of the early returns and potentially uses a `switch` clause for the
different context values.
While refactoring on its own is generally discouraged in WordPress core,
this refactoring will benefit, simplify, or even enable several other
tickets:
* #58892
* #58893
* #58894
It should however not be implemented as part of any of those tickets,
since that could easily lead to oversights due to the complex logic. The
goal of this ticket should be to refactor the logic in a way that all
existing tests pass without any modifications. Coverage is quite extensive
on this function (at least that!), so this is a good framework to follow
here.
--
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58891#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list