We are currently in the process of putting into place stricter code style conventions for our Java code, so it is appropriate that we start setting down similar conventions for our Typescript code. This issue is for proposing a set of such conventions.
This proposal assumes these policies build on the conventions set out for Angular projects (e.g. file naming conventions, class naming conventions, etc.). Exceptions to conventions are expected (but rare) to improve readability.
Spacing
Indentation and Line Length
Standard indentation is 4 spaces; indentation must not include TAB (Ctrl-I) characters.
Avoid statements longer than 120 characters. Longer statements should be either continued onto another line with line-breaks that optimize readability or otherwise broken into multiple, shorter statements.
The last character of a source file should be a new-line (i.e. "return") character.
Classes, Functions, and Decorators
Each decorator of a class, function, or variable (e.g. @Component({...) should begin on a new line.
Decorators should be considered conceptually part of signature line they decorate: there should be no blank line between a decorator block and it class of function signature.
When a decorator includes with an object argument (e.g. {...}), each property of the object should appear on
a separate line and be indented by the standard amount (4); the closing object brace ({) and parenthesis (() should appear on its own line.
This example illustrates the above three recommendations:
When a function signature fits on one line, the opening brace ({) should appear on the same line as the signature. In this case, it is recommended that a blank line be inserted after after the brace and before the first line of the body for better readability.
When a function signature does not fit on a single line:
continue signature on multiple lines, breaking after the comma following an argument
subsequent argument lines should be indented to the column that the first argument appears in.
the closing function parenthesis should appear on the same line as the last argument
the opening function body brace should appear on its own line, aligned with the start of the function signature.
the return type may appear on the same line as the last argument or on its own line; in the latter case, it should be indented four spaces from the start of the function signature
When a class signature fits on one line, the opening brace should appear on the same line as the signature. In this case, it is recommended that a blank line be inserted after after the brace and before the first line of the body for better readability.
Comments and Documentation
The term "in-line documentation" here is intended to to refer to comment blocks that could be extracted and converted into human-readable API documentation (like with javadoc for Java code). We do not currently use a documentation extractor with our Typescript code; however, we may in the future. Nevertheless, extractable documentation markup is on can be highly readable and provides conventions for indicating what is being described.
In-line class, interface, function, and variable documentation should follow the Java in-line documentation conventions, using /** ... */ comment blocks.
In-line class, interface, function, and variable documentation should appear immediately above both the signature and any decorators; the comment opener, /**, should be indented to align with the start of the class, interface, function, or variable.
This example illustrates the above two recommendations:
/**
* a data structure describing a file in the cart. A CartEntryData object, in effect, is a NerdmComp
* that *must* have the filePath property and is expected to have some additional
* data cart-specific properties.
*/
export interface DataCartItem {
/**
* a local identifier for resource. This must not start with an underscore as this is reserved.
*/
resId? : string;
In-line function documentation is recommended for any function marked with the public modifier.
In-line class documentation is recommended for any exported class, interface or function.
In-line variable documentation is recommended for properties of an exported interface.
In-line class documentation is highly recommended for any exported component class (i.e., marked with the @Component decorator). It should briefly summarize what visually appears in the component when it is rendered.
Example Component documentation:
/**
* A component for displaying access to landing page tools in a menu.
*
* Items include:
* * links to the different sections of the landing page
* * links to view or export metadata
* * information about usage (like Citation information in a pop-up)
* * links for searching for similar resources
*/
@Component({
selector: 'tools-menu',
template: ;tools-menu.html',
styleUrls: ['./toolmenu.component.css']
})
export class ToolMenuComponent implements OnChanges {
We are currently in the process of putting into place stricter code style conventions for our Java code, so it is appropriate that we start setting down similar conventions for our Typescript code. This issue is for proposing a set of such conventions.
This proposal assumes these policies build on the conventions set out for Angular projects (e.g. file naming conventions, class naming conventions, etc.). Exceptions to conventions are expected (but rare) to improve readability.
Spacing
Indentation and Line Length
Classes, Functions, and Decorators
@Component({...
) should begin on a new line.{...}
), each property of the object should appear on a separate line and be indented by the standard amount (4); the closing object brace ({
) and parenthesis ((
) should appear on its own line.This example illustrates the above three recommendations:
{
) should appear on the same line as the signature. In this case, it is recommended that a blank line be inserted after after the brace and before the first line of the body for better readability.Multi-line function signature:
Comments and Documentation
The term "in-line documentation" here is intended to to refer to comment blocks that could be extracted and converted into human-readable API documentation (like with
javadoc
for Java code). We do not currently use a documentation extractor with our Typescript code; however, we may in the future. Nevertheless, extractable documentation markup is on can be highly readable and provides conventions for indicating what is being described./** ... */
comment blocks./**
, should be indented to align with the start of the class, interface, function, or variable.This example illustrates the above two recommendations:
public
modifier.@Component
decorator). It should briefly summarize what visually appears in the component when it is rendered.Example Component documentation: