From 70aa3c2fa684f3915e013f3f0dd34e7e1cbd83b9 Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Tue, 21 Nov 2017 09:02:14 -0800 Subject: [PATCH] chore: add guidance on getters/setters to coding standards (#7977) --- CODING_STANDARDS.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/CODING_STANDARDS.md b/CODING_STANDARDS.md index 46a16b6b31c..e0b9da487df 100644 --- a/CODING_STANDARDS.md +++ b/CODING_STANDARDS.md @@ -154,6 +154,24 @@ be part of the user-facing API. This typically applies to symbols used in templa Additionally, the `@docs-private` JsDoc annotation can be used to hide any symbol from the public API docs. + +#### Getters and Setters +* Avoid long or complex getters and setters. If the logic of an accessor would take more than +three lines, introduce a new method to contain the logic. +* A getter should immediately precede its corresponding setter. +* Decorators such as `@Input` should be applied to the getter and not the setter. +* Always use a `readonly` property instead of a getter (with no setter) when possible. + ```ts + /** YES */ + readonly active: boolean; + + /** NO */ + get active(): boolean { + // Using a getter solely to make the property read-only. + return this._active; + } + ``` + #### JsDoc comments All public APIs must have user-facing comments. These are extracted and shown in the documentation