Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 0664a27

Browse files
committed
docs(style-guide): spell const variables in lower camel case
Changes previous guidance on const which insisted on UPPER_SNAKE_CASE
1 parent 2c5f0db commit 0664a27

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { Component } from '@angular/core';
22

3-
import { HEROES_URL, VILLAINS_URL } from './shared';
3+
import { heroesUrl, mockHeroes, VILLAINS_URL } from './shared';
44

55
@Component({
66
selector: 'sg-app',
77
template: `
88
<div>Heroes url: {{heroesUrl}}</div>
99
<div>Villains url: {{villainsUrl}}</div>
10-
`,
10+
11+
<h4>Mock Heroes</h4>
12+
<div *ngFor="let hero of heroes">{{hero}}</div>
13+
`
1114
})
1215
export class AppComponent {
13-
heroesUrl = HEROES_URL;
14-
villainsUrl = VILLAINS_URL;
16+
heroes = mockHeroes; // prefer
17+
heroesUrl = heroesUrl; // prefer
18+
villainsUrl = VILLAINS_URL; // tolerate
1519
}

public/docs/_examples/style-guide/ts/03-02/app/shared/data.service.avoid.ts

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// #docregion
2-
// #docregion example
3-
export const HEROES_URL = 'api/heroes';
4-
export const VILLAINS_URL = 'api/villains';
5-
// #enddocregion example
2+
export const mockHeroes = ['Sam', 'Jill']; // prefer
3+
export const heroesUrl = 'api/heroes'; // prefer
4+
export const VILLAINS_URL = 'api/villains'; // tolerate

public/docs/ts/latest/guide/style-guide.jade

+29-6
Original file line numberDiff line numberDiff line change
@@ -651,20 +651,43 @@ a(href="#toc") Back to top
651651

652652
.s-rule.do
653653
:marked
654-
**Do** use uppercase with underscores when naming constants.
654+
**Do** declare variables with `const` if their values should not change during the application lifetime.
655655

656656
.s-why
657657
:marked
658-
**Why?** Follows conventional thinking for constants.
658+
**Why?** Conveys to readers that the value is invariant.
659659

660660
.s-why.s-why-last
661661
:marked
662-
**Why?** Constants can easily be identified.
662+
TypeScript helps enforce that intent by requiring immediate initialization and by
663+
preventing subsequent re-assignment.
664+
665+
.s-rule.consider
666+
:marked
667+
**Consider** spelling `const` variables in lower camel case.
663668

664-
+makeExample('style-guide/ts/03-02/app/shared/data.service.avoid.ts', 'example', 'app/shared/data.service.ts')(avoid=1)
665-
:marked
669+
.s-why
670+
:marked
671+
**Why?** lower camel case variable names (`heroRoutes`) are easier to read and understand
672+
than the traditional UPPER_SNAKE_CASE names (`HERO_ROUTES`).
673+
674+
.s-why.s-why-last
675+
:marked
676+
**Why?** The tradition of naming constants in UPPER_SNAKE_CASE reflects
677+
an era before the modern IDEs that quickly reveal the `const` declaration.
678+
TypeScript itself prevents accidental reassignment.
679+
680+
.s-rule.do
681+
:marked
682+
**Do** tolerate _existing_ `const` variables that are spelled in UPPER_SNAKE_CASE.
683+
684+
.s-why.s-why-last
685+
:marked
686+
**Why?** Although we recommend creating _new_ constants in lower camel case,
687+
the tradition of UPPER_SNAKE_CASE remains popular and pervasive,
688+
especially in third party modules.
666689

667-
+makeExample('style-guide/ts/03-02/app/shared/data.service.ts', 'example', 'app/shared/data.service.ts')
690+
+makeExample('style-guide/ts/03-02/app/shared/data.service.ts', '', 'app/shared/data.service.ts')
668691
:marked
669692

670693
a(href="#toc") Back to top

0 commit comments

Comments
 (0)