3. Additional formatting standards
Anonymous function calls
When you declare an anonymous function as an argument in a function call, indent the body of the function by four spaces from the left edge of the statement or function keyword
to increase readability.
End of file
The last line in a file must end with a single linefeed(LF) character(i.e. an empty line).
This reduces the quantity of the changed lines in a diff and makes code safer in file concatenation processes.
Indentation in Magento code uses four spaces.
Tabs are not allowed as indentation.
Indent wrapped lines two spaces or left-aligned to the expression above.
Max line length
The recommended max line length is 80 characters.
Source code lines must not exceed 120 characters.
Comment lines that contain an example command or a literal URL longer than 120 characters may exceed the 120 character limit for ease of cutting and pasting.
Do not be concerned about header guards that exceed 120 characters.
Line termination follows the UNIX text file convention.
Lines must end with a single linefeed(LF) character represented as ordinal 10 or hexadecimal (0x0A).
Do not use carriage the Mac OS convention of carriage returns(CR or 0x0D) or the carriage return-linefeed combination(CRLF or 0x0D and 0x0A) standard for Windows OS.
Multi-line string literals
Use string concatenation for multi-line string literals:
Use sparingly and in general where required by the syntax and semantics.
Never use parentheses for:
- Unary operators (e.g. delete, typeof, and void)
- After keywords such as return, throw
- For case, in, or new, and others keywords like them.
Use braces with all multi-line blocks. May only omit braces if the entire block can be written in one line and improves readability.
Always put semicolons as statement terminators.
The following code examples show the dangers of missing semicolons:
Since there is semicolon to end the first statement, the first function returns 42 and the script interprets 42 as a function. When the script tries to call 42 as a function with the second function as a parameter, an error occurs.
A ‘no such property in undefined’ error appears during runtime when the script tries to call x[ffVersion][isIE]().
The script calls die unless resultOfOperation)_ is NaN and assigns THING_TO_EAT the result of die().
In each of the examples above, a function declaration, or an object, or an array literal is used inside a statement.
Use single quotes instead of double quotes for consistency.
This is helpful when creating strings that include HTML: