Skip to main content

Coding style

A list of coding style rules to be used when developing in vtenext.

File encoding: UTF-8. All files should be encoded in UTF-8
Newlines: Unix style, so \n, not \r\n or \r
Indentation size:

With TABS, set to 4 character. Don't mix spaces and tabs:

 

image.pngJwzimage.png

 

Indentation style:

C Style, so:

  • Opening brace ( { ) on the same line as its control clause (an exception can be made if the line is very long, so in this case the brace can go to a new line, for better readability)
  • Closing brace ( } ) aligned with the opening statement
  • Single space before the opening brace
  • Single space before the parenthesis of a control structure (if, for, foreach, ...)
Examples

Good

 

if ($var === '234') {
    $out = 'ok';
} elseif ($var === '567') {
    $out = 'also ok';
} else {
    $out = 'maybe ok?';
}

Bad: no space between foreach and (

 

foreach($array as $var) {
    // do something
}

 

Bad: opening brace on new line

 

if ($var == 8)
{
    // do something
}

Please note that these style rules are not strictly observed throughout the existing code, so you may find exceptions. Try to follow them, to improve the code homogeneity, but don't be too rigid in following them when evaluating existing code.

 

Line length:

We don't have a strict maximum line length. Usually up to 200 characters is ok, more than that can be hard to read on smaller screens, so better to split the line, but this is left to the developer's choice.

Also, splitting too much, for example a 300 chars line divided in 30 lines of 10 chars is no more readable than the original line. Balance readability and conciseness of code.

PHP tags:

Use standard opening tag: <?php , not the short one: <?

Avoid the closing tag ( ?> ) if there's no output.


Examples

Good: standard opening tag, no closing tag

 

<?php

$var = 'value';
// php code
 
// no closing tag here

Bad: short opening tag, closing tag

 

<? // short tag
 
$var = 'value';
// php code
 
// unnecessary
// closing tag
?>

 

Good: standard opening tag, closing tag with output

 

<?php

$var = 'value';
// php code

// output after php code
?>
<html>
<!-- html code -->
</html>
Auto formatting:
  • Core files: Do not enable any "auto formatting on save" functionality in your IDE/editor, to avoid too many changes in existing files, which will be hard to diff. 
  • Custom files: you can reformat existing files to make them compliant with our standards.
License:

When creating a new file (php, js, tpl, html, scss) to be included in the standard vtenext, use the following license at the top of the file:

 

<?php
/*************************************
* SPDX-FileCopyrightText: 2009-present Vtenext S.r.l. Società Benefit
* SPDX-License-Identifier: LicenseRef-vtenext-business-license
************************************/