[wp-trac] [WordPress Trac] #55662: Eliminating magic numbers
WordPress Trac
noreply at wordpress.org
Tue May 3 07:49:31 UTC 2022
#55662: Eliminating magic numbers
------------------------------+-----------------------------
Reporter: Cybr | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version:
Severity: normal | Keywords:
Focuses: coding-standards |
------------------------------+-----------------------------
Magic numbers in code are unique values with unexplainable meaning. These
numbers can be confusing to the reader. To learn more, see:
https://en.wikipedia.org/wiki/Magic_number_(programming)
Using this example:
`$max_filesize = 5 * 1024 * 1024`;
A common way to eliminate those numbers is by using either comments or
constants. Example:
`$max_filesize = 5 * 1024 * 1024; // 5 MB`
`$max_filesize = 5 * MB_IN_BYTES;`
WordPress counts at least 20 instances of using `1024` in PHP, either
meaning filesize, screen, or image dimensions. Using constant definitions
would instantly differentiate their meaning when browsing the code.
Another example is `fileupload_maxk`, which defaults to `1500`. If we
define that size using a constant, we could easily update all related
instances without having to inspect those manually: simply by updating the
constant value. This cohesiveness aids in reducing regressions.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/55662>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list