From 8bf3cb38a235c947ff16f160e484baea17cb6443 Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Mon, 23 Oct 2017 11:24:46 -0700 Subject: [PATCH 1/2] chore: add guidance on getters/setters to coding standards --- CODING_STANDARDS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CODING_STANDARDS.md b/CODING_STANDARDS.md index 46a16b6b31c7..3153bd086c64 100644 --- a/CODING_STANDARDS.md +++ b/CODING_STANDARDS.md @@ -154,6 +154,12 @@ 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 +* Always use a `readonly` property instead of a getter (with no setter) when possible. +* Avoid long getters and setters. If the logic of an accessor would take more than three lines, +introduce a new method to contain the logic. + #### JsDoc comments All public APIs must have user-facing comments. These are extracted and shown in the documentation From 826ffb28d66bdb1eb163125b18d1a15ecae6fafa Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Mon, 23 Oct 2017 14:23:19 -0700 Subject: [PATCH 2/2] Update CODING_STANDARDS.md Expand with more points --- CODING_STANDARDS.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/CODING_STANDARDS.md b/CODING_STANDARDS.md index 3153bd086c64..e0b9da487df4 100644 --- a/CODING_STANDARDS.md +++ b/CODING_STANDARDS.md @@ -156,9 +156,21 @@ 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. -* Avoid long getters and setters. If the logic of an accessor would take more than three lines, -introduce a new method to contain the logic. + ```ts + /** YES */ + readonly active: boolean; + + /** NO */ + get active(): boolean { + // Using a getter solely to make the property read-only. + return this._active; + } + ``` #### JsDoc comments