Lines

Verze:

17. 06. 2025

Zodpovědná osoba:

Dominik Šlechta

Line Length (Max 80 Characters)

  • Rule: Lines of code SHOULD NOT be longer than 80 characters.

  • Reason: Keeping lines within 80 characters enhances code readability and maintainability. It helps developers easily view the code in various editors without horizontal scrolling.

  • Action for long lines: If a line exceeds 80 characters, it SHOULD be split into multiple lines of no more than 80 characters each. Alternatively, a comment can be added explaining why the line exceeds the limit.

Example of Splitting a Line:

// Good example: Split a long line into shorter lines
$someLongVariableName = someFunctionThatReturnsAVeryLongString(
    'This is a very long string that needs to be split into multiple lines',
    'to comply with the 80 characters per line rule.'
);

No Trailing Whitespace

  • Rule: There SHOULD NOT be whitespace at the end of lines.

  • Reason: Trailing whitespace can cause issues with version control (unintended diffs) and is generally considered sloppy code practice.

Example:

// Correct: No trailing spaces
$var = 'value';

// Incorrect: Trailing spaces at the end of the line
$var = 'value' ;     

Blank Lines for Readability

  • Rule: Blank lines MAY be added to improve readability, especially to separate related blocks of code.

  • Reason: Blank lines help separate logical sections of code, making it easier to understand the structure and flow.

  • However, blank lines SHOULD NOT be used where explicitly forbidden (e.g., in certain compact coding styles or frameworks).

One Statement Per Line

  • Rule: There MUST NOT be more than one statement per line.

  • Reason: Multiple statements on a single line can make code hard to read, debug, and maintain.

  • Forbidden Example: 

    // Forbidden: More than one statement per line
    $var = $a; $foo = $b;
    

Correct Example:

// Correct: One statement per line
$var = $a;
$foo = $b;

Summary:

  • Keep lines ≤ 80 characters.

  • No trailing whitespace at the end of lines.

  • Blank lines are allowed for readability (except where forbidden).

  • One statement per line – no multiple statements on the same line.