Summary: PHP code style of savane
                 Project: Savannah Administration
            Submitted by: peterdd
            Submitted on: Wed 19 Jun 2019 09:02:37 PM CEST
                Category: Savannah website
                Priority: 5 - Normal
                Severity: 3 - Normal
                  Status: None
             Assigned to: None
        Originator Email: 
        Operating System: None
             Open/Closed: Open
         Discussion Lock: Any



Coding styles are different between programming languages, different between
companies, also different between communities of a programming language by
countries sometimes, and even change over time.

Netherless I want to point out some rules that SHOULD be applied to the PHP
code of savane when changing a file or writing a new .php file:

* Do not put any spaces between a function call and the opening parenthesis.
  It makes reading and searching sourcetrees easier (a grep -rin 'time(' would
not match 'Time to say goodbye.', but any variant of time( Time( tIME( ... 

This was the vote result for PSR-2:

    no: 22


* Always put the body following an *if* structure within curly brackets, even
if it just contains a single expression!
  Not putting curly brackets there is like laying hidden mines and provokes
hard to find bugs for any later person.
  I don't know why people still keep doing that evil shortcut. Maybe it is
from C-coders that used it 40 years ago to optimize their compiler that
couldn't optimize {} out when a single command follows? I don't know.

There is https://www.php.net/manual/en/control-structures.if.php , but that
guys are C-coders too. 
  For single expression if's there is the ternary conditional operator (but I
find it easier write and read read 'if ($x){ expression } else { expression }'
  'if ($x) ? expression : expression;'

This was the vote result for PSR-2:

    no: 3
    yes: 19

Quite clearly.

* Please optionally put . in PHP comments only if it is *a full sentence*. I
even prefer not to put a . even for a single sentense in comment, but that is
not important.

* Do not indent curly brackets at nowhere spaces. Either align it 
  * vertical with the if/while/switch/for/..  
  * opening curly bracket at same line, ending bracket vertical aligned with

The other whole bunch of coding styles are debatable, but these 4 are IMHO
important to follow.


* Only change code that matches the commit message. Stick to an existing
coding style of a file. Keeps the changes to git commits small and makes
reviews/git history/git blame/ easier.


There is also PSR-2 https://www.php-fig.org/psr/psr-2/, but I would not fully
agree to that standard (See the quite diverting voting results at

I disagree to that PSR-2 rule:

    tab: 7 (I prefer this as a user of also dumb/basic editors, navigation
    2 spaces: 1
    4 spaces: 14

The other rules of PSR-2 are either ok or I do not care of the flavor and can
adapt to it.


Reply to this item at:


  Message sent via Savannah

Reply via email to