Launchpad
Launchpad
Launchpad
  • Introduction
  • General
    • Installation
    • Creating a subscriber
    • Contributing
  • Tutorials
    • Beginner documentation
      • Beginner handbook
        • Creating subscribers
        • Wiring subscribers
        • Installing local environment
        • Adding front-end
        • Building the plugin
      • Plugin life circle
        • Activation
        • Deactivation
        • Uninstall
    • Starting with the framework
    • Migrating to Launchpad
    • Notions
      • Framework concepts
        • Inversion of control
        • Subscribers
        • Dispatcher
      • Good practices
        • Hooks
          • Preventing magic constants
          • Decouple features
          • Sanitize filters output
      • Testing
        • Organize tests
  • CLI
    • Commands
    • Creating a command
  • Testing
    • Unit test
    • Fixtures
    • Integration test
  • Container
    • Architecture
    • Parameters
    • Providers
    • Auto wiring
    • Manual wiring
    • Activation/Deactivation
    • Inflectors
  • Modules
    • Definition
    • Listing
      • Action Scheduler
      • BerlinDB
      • Bus
      • Options
      • Renderer
      • Logger
      • Uninstaller
      • Filesystem
      • Front-end
      • Hook extractor
    • Creating a module
Powered by GitBook
On this page
  1. Tutorials
  2. Notions
  3. Good practices
  4. Hooks

Preventing magic constants

Magic constant are often values that the developer set in his code without apparent reason. That can be for example a value like 3.14 inside a calculus or 24 inside an interval as you can see in the following code:

$radius = 3.14 * $perimeter;
$period = 24 * 60 * 60;

As you can see even in this case it is simple we have two main problems with that code.

First it is up the programmer to guess what these values are. This is a code smell in the fact it increases the mental mapping from the programmer forcing him to remember that value.

The second issue we have this with that code is the problem of the non flexibility of the code.

In the case the value could be modified without impacting the validity of the code then the user will be fixed the developer value without anyway to modify it.

To address that issue we have two solution. In the case of the magic value that can't be modified like in this example:

$radius = 3.14 * $perimeter;

Then we can use a constant that we will share between each of its usages like this:

define('PI', 3.14);
$radius = PI * $perimeter;

In the case of the magic value that is based on a decision from the developer and could be modified like in this example:

$period = 24 * 60 * 60;

Then we can use a filter to make the value easily modifiable by the user later but also provide more information on what it is:

/**
 * Interval for the period which is by default 24hrs.
 * @param int $interval Interval for period in seconds.
 **/
$period = apply_filter('period_interval', 24 * 60 * 60);

That way we can satisfy two kinds of users:

  • The debutant who will have a default choice already made for them and so won't know about this decision which has been made for them.

  • The expert who has with the filter a possibility to adapt the plugin to his needs.

PreviousHooksNextDecouple features

Last updated 11 months ago