[wp-trac] [WordPress Trac] #59777: WP admin : Use Yoda condition

WordPress Trac noreply at wordpress.org
Tue Oct 31 16:51:08 UTC 2023


#59777: WP admin : Use Yoda condition
----------------------------+-------------------------------
 Reporter:  ashokrd2013     |       Owner:  (none)
     Type:  enhancement     |      Status:  new
 Priority:  normal          |   Milestone:  Awaiting Review
Component:  Administration  |     Version:  trunk
 Severity:  normal          |  Resolution:
 Keywords:  has-patch       |     Focuses:  coding-standards
----------------------------+-------------------------------

Comment (by ashokrd2013):

 The two expressions {{{count( $linkcheck ) === 0}}} and {{{0 === count(
 $linkcheck )}}} are equivalent in terms of functionality, but the second
 expression is generally considered to be more performant. This is because
 the PHP compiler can optimize the second expression by eliminating the
 need to call the count() function.

 The count() function is a built-in PHP function that returns the number of
 elements in an array. When the compiler encounters the expression count(
 $linkcheck ), it needs to call the count() function to determine the
 number of elements in the array. However, when the compiler encounters the
 expression 0 === count( $linkcheck ), it can optimize the expression by
 eliminating the need to call the count() function. This is because the
 compiler knows that the value 0 is always equal to the number of elements
 in an empty array.

 In addition, the second expression is also more readable and easier to
 maintain. This is because it is more explicit about the intent of the
 code. The second expression clearly states that the value of the variable
 $linkcheck is empty.

 Therefore, it is generally recommended to use the expression {{{0 ===
 count( $linkcheck )}}} instead of the expression {{{count( $linkcheck )
 === 0}}} when checking if an array is empty.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59777#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list