Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-74990

Policy: Ban the use of `and`, and `or`

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 4.1
    • None
    • Policy
    • None

    Description

      Policy

      The following binary operators must not be used:

      • and
      • or

      The following standard binary operators may be used instead:

      • &&
      • ||

      To put it bluntly, these operators are nasty, and have counter-intuitive behaviours.
      Whilst they have a use-case, their use leads to unclear code and bugs, and they do not behave as 90% of people would expect without RTFMing.

      To demonstrate:

      $example = false or true or true or true; // $example = false
      $example = (false or true or true or true); // $example = true
      if (false or true or true or true) {
          // Truthy result
      }
      

      This has the potential to cause issues where developers are not 100% familiar with the operators, for example during a refactor.

      Given the following code:

      if (some_long_named_function() or some_other_long_named_function()) {
          // Do something.
      }
      

      If it is later refactored, for example to add a third parameter, and the author continues with the use of the or operator, it could easily be modifed to:

      $dosomething = some_long_named_function() or some_other_long_named_function() or something_else();
       
      if ($dosomething) {
          // Do something.
      }
      

      However the behaviour here is different and, if the first test returns false, then the conditional will no longer be entered because $dosomething takes the first value, which is now false.

      I would strongly suggest that we ban the use of both the and, and or operators to prevent mistakes of this kind.

      Voting options

      The following options are suggested for voting:

      Option A

      Ban the use of the and and or keywords in favour of && and || and apply the following change to the coding style:

      Proposed wording
        1. A new Operators section

      ### Operators
       
      The following binary operators **must not** be used:
      - `and`
      - `or`
       
      The following standard binary operators _may_ be used instead:
      - `&&`
      - `||`
      

        1. A reference to this should be added to the Control statements section:

      :::important

      Please note that the the `and`, and `or` operators *must not* be used. The `&&` and `||` operators should be used in their place.

      See [Operators](#operators) for more information.

      :::

      Option B

      Do not ban them.

      Attachments

        Issue Links

          Activity

            People

              dobedobedoh Andrew Lyons
              dobedobedoh Andrew Lyons
              Adrian Greeve, Andrew Lyons, Eloy Lafuente (stronk7), Juan Leyva, Jun Pataleta, Sander Bangma
              Votes:
              7 Vote for this issue
              Watchers:
              17 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: