Format Checker
Programs are meant to be read by humans and only incidentally for computers to execute.
— H. Abelson and G. Sussman
Structure and Interpretation of Computer Programs
Background
Most programming languages promote a uniform style for formatting code, either with style guide documents (e.g. Python’s PEP 8) or utilities (aka “linters”).
THT goes a step further. It has a Format Checker that identifies issues at compile time.
The format rules were collected from the most commonly used style guides for comparable languages (typically Java & JavaScript).
Benefits
- Clean, consistent code is easier to read & understand.
- Clean code is more enjoyable to work with.
- Clean code is less prone to bugs.
- Sharing code online is easier.
- Novices will learn best practices quicker.
- Teams will spend less time debating what rules to follow.
- No extra steps in the build process to setup and configure.
Using conventions frees your brain from the mundane aspects of programming, which offer little payback.
— Steve McConnell, Code Complete
Formatting Rules
General
- No lines over 100 characters long. Exceptions are multi-line strings and template functions.
- UNIX newlines. Line endings should be set to
LF
(UNIX). NotCRLF
(Windows). - UTF-8. Files should be saved as UTF-8 encoded.
Tab Characters
TAB characters are interpreted by THT to be 4 spaces.
However, we recommend you configure your editor to insert 4 spaces for your Tab key (aka “soft tabs”), if it doesn’t already do so by default.
Whitespace
Indentation
Lines inside multiline braces of {}
, []
, or ()
should be indented.
Recommended: 4 spaces
// ✖ No $nums = [ 'Do', 'Re', 'Mi' ]; // ✔ Yes $nums = [ 'Do', 'Re', 'Mi' ];
Infix operators + = == &&
Space before & after: YES
$a = $b + 1; // ✔ Yes $a = $b+1; // ✖ No $a=$b+1; // ✖ No if $isGood && $isOk { ... } // ✔ Yes if $isGood&&$isOk { ... } // ✖ No
Prefix operators ! -
Spaces after: NO
$isNormal = !$isAdmin; // ✔ Yes $isNormal = ! $isAdmin; // ✖ No if !$isAdmin { ... } // ✔ Yes if ! $isAdmin { ... } // ✖ No $a = -23; // ✔ Yes $a = - 23; // ✖ No
Semicolons ;
Space before: NO
Limit: Only one statement per line.
$foo = 1; // ✔ Yes $foo = 1 ; // ✖ No $foo = 1; $bar = 2; // ✖ No
Commas ,
Space before: NO
Space after: YES
doSomething($a, $b, $c); // ✔ Yes doSomething($a,$b,$c); // ✖ No
Parentheses ( )
Inside padding: NO
doSomething($myVar); // ✔ Yes doSomething( $myVar ); // ✖ No $a = $b / ($c / $d); // ✔ Yes $a = $b / ( $c / $d ); // ✖ No
Square braces [ ]
Inside padding: NO
$a = [1, 2, 3]; // ✔ Yes $a = [ 1, 2, 3 ]; // ✖ No $user['userId']; // ✔ Yes $user[ 'userId' ]; // ✖ No
Curly braces { }
Inside padding: YES
$a = { foo: 1 }; // ✔ Yes $a = {foo: 1}; // ✖ No if $isOk { return $a; } // ✔ Yes if $isOk {return $a;} // ✖ No
Colons :
Space before: NO
Space after: YES
$a = { foo: 1 }; // ✔ Yes $a = { foo : 1 }; // ✖ No $a = { foo:1 }; // ✖ No
Function argument list ( )
Space before: NO
Space after: YES
F foo($myVar) { // ✔ Yes F foo($myVar){ // ✖ No F foo ($myVar) // ✖ No
Open braces {
Space before: YES
Same line: YES
Next line: NO (but allowed)
// ✔ Yes F main() { // ... } // ✖ No - no space before { F main(){ // ... } // ✖ No - but allowed F main() { // ... }
Closing braces } ]
Next Line: YES
// ✔ Yes $nums = [ 111, 222, ]; // ✖ No $nums = [ 111, 222 ];
Parser Rules
Assignment inside conditional: NO
// ✖ No if $line = readLine() { ... } // ✔ Yes $line = $readLine(); if $line { ... }
Nested ternary expressions: NO
// ✖ No $a = check1 ? action1() : check2 ? action2() : action3(); // ✔ Yes - if/else $a = ''; if check1 { $a = action1(); } else if check2 { $a = $action2(); } else { $a = $action3(); } // ✔ Yes - match with one-liner syntax $a = ''; match { check1: $a = action1(); check2: $a = action2(); default: $a = action3(); }
Names
Module and class names are strict UpperCamelCase. This includes acronyms.
User XmlReader XmlHttpRequest
All other names (variables, functions, bare map keys) are strict camelCase. This includes acronyms.
user firstName userId userHasIphone httpResponse
HTML Templates
These are additional rules for markup within HTML Template Functions.
Tag Names
Uppercase: NO
// ✖ No <SPAN> // ✔ Yes <span>
Parameters
Quoted: YES
// ✖ No <div class=my-class> // ✔ Yes <div class="my-class">
Space around equals: NO
// ✖ No <div class = "my-class"> // ✔ Yes <div class="my-class">