1.2.787. No Boolean As Default

Default values should always be set up with a constant name.

Class constants and constants improve readability when calling the methods or comparing values and statuses.

<?php

const CASE_INSENSITIVE = true;
const CASE_SENSITIVE = false;

function foo($case_insensitive = true) {
    // doSomething()
}

// Readable code
foo(CASE_INSENSITIVE);
foo(CASE_SENSITIVE);


// unreadable code  : is true case insensitive or case sensitive ?
foo(true);
foo(false);

?>

See also FlagArgument and Clean code: The curse of a boolean parameter.

1.2.787.1. Suggestions

  • Use constants or class constants to give value to a boolean literal

  • When constants have been defined, use them when calling the code

  • Split the method into two methods, one for each case

1.2.787.2. Specs

Short name

Functions/NoBooleanAsDefault

Rulesets

All, Analyze

Exakat since

0.10.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

boolean, default-value

Examples

OpenConf

Available in

Entreprise Edition, Exakat Cloud