forked from angular/angular.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle-guide.jade
2207 lines (1741 loc) · 64.4 KB
/
style-guide.jade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
include ../_util-fns
:marked
Welcome to the Angular Style Guide
## Purpose
Looking for an opinionated guide to Angular syntax, conventions, and application structure?
Step right in!
This style guide presents our preferred conventions and, as importantly, explains why.
.l-main-section
:marked
## Style vocabulary
Each guideline describes either a good or bad practice, and all have a consistent presentation.
The wording of each guideline indicates how strong the recommendation is.
.s-rule.do
:marked
**Do** is one that should always be followed.
_Always_ might be a bit too strong of a word.
Guidelines that literally should always be followed are extremely rare.
On the other hand, you need a really unusual case for breaking a *Do* guideline.
.s-rule.consider
:marked
**Consider** guidelines should generally be followed.
If you fully understand the meaning behind the guideline and have a good reason to deviate, then do so. Please strive to be consistent.
.s-rule.avoid
:marked
**Avoid** indicates something you should almost never do. Code examples to *avoid* have an unmistakeable red header.
.l-main-section
:marked
## File structure conventions
Some code examples display a file that has one or more similarly named companion files.
For example, `hero.component.ts` and `hero.component.html`.
The guideline will use the shortcut `hero.component.ts|html|css|spec` to represent those various files. Using this shortcut makes this guide's file structures easier to read and more terse.
.l-main-section
a(id='toc')
:marked
## Table of contents
1. [Single responsibility](#single-responsibility)
1. [Naming](#naming)
1. [Coding conventions](#coding-conventions)
1. [App structure and Angular modules](#application-structure-and-angular-modules)
1. [Components](#components)
1. [Directives](#directives)
1. [Services](#services)
1. [Data services](#data-services)
1. [Lifecycle hooks](#lifecycle-hooks)
1. [Appendix](#appendix)
.l-main-section
:marked
## Single responsibility
Apply the
<a href="https://wikipedia.org/wiki/Single_responsibility_principle" target="_blank"><i>Single Responsibility Principle</i> (SPR)</a>
to all components, services, and other symbols.
This helps make the app cleaner, easier to read and maintain, and more testable.
### <a id="01-01"></a>_Rule of One_
#### <a href="#01-01">Style 01-01</a>
.s-rule.do
:marked
**Do** define one thing, such as a service or component, per file.
.s-rule.consider
:marked
**Consider** limiting files to 400 lines of code.
.s-why
:marked
**Why?** One component per file makes it far easier to read, maintain, and avoid
collisions with teams in source control.
.s-why
:marked
**Why?** One component per file avoids hidden bugs that often arise when combining components in a file where they may share variables, create unwanted closures, or unwanted coupling with dependencies.
.s-why.s-why-last
:marked
**Why?** A single component can be the default export for its file which facilitates lazy loading with the router.
:marked
The key is to make the code more reusable, easier to read, and less mistake prone.
The following *negative* example defines the `AppComponent`, bootstraps the app, defines the `Hero` model object, and loads heroes from the server ... all in the same file. *Don't do this*.
+makeExample('style-guide/ts/01-01/app/heroes/hero.component.avoid.ts', '', 'app/heroes/hero.component.ts')(avoid=1)
:marked
It is a better practice to redistribute the component and its
supporting classes into their own, dedicated files.
+makeTabs(
`style-guide/ts/01-01/main.ts,
style-guide/ts/01-01/app/app.module.ts,
style-guide/ts/01-01/app/app.component.ts,
style-guide/ts/01-01/app/heroes/heroes.component.ts,
style-guide/ts/01-01/app/heroes/shared/hero.service.ts,
style-guide/ts/01-01/app/heroes/shared/hero.model.ts,
style-guide/ts/01-01/app/heroes/shared/mock-heroes.ts`,
'',
`main.ts,
app/app.module.ts,
app/app.component.ts,
app/heroes/heroes.component.ts,
app/heroes/shared/hero.service.ts,
app/heroes/shared/hero.model.ts,
app/heroes/shared/mock-heroes.ts`)
:marked
As the app grows, this rule becomes even more important.
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="01-02"></a>Small functions
#### <a href="#01-02">Style 01-02</a>
.s-rule.do
:marked
**Do** define small functions
.s-rule.consider
:marked
**Consider** limiting to no more than 75 lines.
.s-why
:marked
**Why?** Small functions are easier to test, especially when they do one thing and serve one purpose.
.s-why
:marked
**Why?** Small functions promote reuse.
.s-why
:marked
**Why?** Small functions are easier to read.
.s-why
:marked
**Why?** Small functions are easier to maintain.
.s-why.s-why-last
:marked
**Why?** Small functions help avoid hidden bugs that come with large functions that share variables with external scope, create unwanted closures, or unwanted coupling with dependencies.
a(href="#toc") Back to top
.l-main-section
:marked
## Naming
Naming conventions are hugely important to maintainability and readability. This guide recommends naming conventions for the file name and the symbol name.
.l-main-section
:marked
### <a id="02-01"></a>General Naming Guidelines
#### <a href="#02-01">Style 02-01</a>
.s-rule.do
:marked
**Do** use consistent names for all symbols.
.s-rule.do
:marked
**Do** follow a pattern that describes the symbol's feature then its type. The recommended pattern is `feature.type.ts`.
.s-why
:marked
**Why?** Naming conventions help provide a consistent way to find content at a glance. Consistency within the project is vital. Consistency with a team is important. Consistency across a company provides tremendous efficiency.
.s-why
:marked
**Why?** The naming conventions should simply help find desited code faster and make it easier to understand.
.s-why.s-why-last
:marked
**Why?** Names of folders and files should clearly convey their intent. For example, `app/heroes/hero-list.component.ts` may contain a component that manages a list of heroes.
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-02"></a>Separate file names with dots and dashes
#### <a href="#02-02">Style 02-02</a>
.s-rule.do
:marked
**Do** use dashes to separate words in the descriptive name.
.s-rule.do
:marked
**Do** use dots to separate the descriptive name from the type.
.s-rule.do
:marked
**Do** use consistent type names for all components following a pattern that describes the component's feature then its type. A recommended pattern is `feature.type.ts`.
.s-rule.do
:marked
**Do** use conventional type names including `.service`, `.component`, `.pipe`, `.module`, and `.directive`.
Invent additional type names if you must but take care not to create too many.
.s-why
:marked
**Why?** Type names provide a consistent way to quickly identify what is in the file.
.s-why
:marked
**Why?** Type names make it easy to find a specific file type using an editor or IDE's fuzzy search techniques.
.s-why
:marked
**Why?** Unabbreviated type names such as `.service` are descriptive and unambiguous.
Abbreviations such as `.srv`, `.svc`, and `.serv` can be confusing.
.s-why.s-why-last
:marked
**Why?** Type names provide pattern matching for any automated tasks.
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-03"></a>Symbols and file names
#### <a href="#02-03">Style 02-03</a>
.s-rule.do
:marked
**Do** use consistent names for all assets named after what they represent.
.s-rule.do
:marked
**Do** use upper camel case for class names.
.s-rule.do
:marked
**Do** match the name of the symbol to the name of the file.
.s-rule.do
:marked
**Do** append the symbol name with the conventional suffix (such as `Component`,
`Directive`, `Module`, `Pipe`, or `Service`) for a thing of that type.
.s-rule.do
:marked
**Do** give the filename the conventional suffix (such as `.component.ts`, `.directive.ts`,
`.module.ts`, `.pipe.ts`, or `.service.ts`) for a file of that type.
.s-why
:marked
**Why?** Consistent conventions make it easy to quickly identify
and reference assets of different types.
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@Component({ ... })
export class AppComponent { }
td
:marked
app.component.ts
tr(style=top)
td
code-example.
@Component({ ... })
export class HeroesComponent { }
td
:marked
heroes.component.ts
tr(style=top)
td
code-example.
@Component({ ... })
export class HeroListComponent { }
td
:marked
hero-list.component.ts
tr(style=top)
td
code-example.
@Component({ ... })
export class HeroDetailComponent { }
td
:marked
hero-detail.component.ts
tr(style=top)
td
code-example.
@Directive({ ... })
export class ValidationDirective { }
td
:marked
validation.directive.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class AppModule
td
:marked
app.module.ts
tr(style=top)
td
code-example.
@Pipe({ name: 'initCaps' })
export class InitCapsPipe implements PipeTransform { }
td
:marked
init-caps.pipe.ts
tr(style=top)
td
code-example.
@Injectable()
export class UserProfileService { }
td
:marked
user-profile.service.ts
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-04"></a>Service names
#### <a href="#02-04">Style 02-04</a>
.s-rule.do
:marked
**Do** use consistent names for all services named after their feature.
.s-rule.do
:marked
**Do** suffix a service class name with Service.
For example, something that gets data or heroes
should be called a `DataService` or a `HeroService`.
A few terms are unambiguously services. They typically
indicate agency by ending in "er". You may prefer to name
a service that logs messages `Logger` rather than `LoggerService`.
Decide if this exception is agreeable in your project.
As always, strive for consistency.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify and reference services.
.s-why
:marked
**Why?** Clear service names such as `Logger` do not require a suffix.
.s-why.s-why-last
:marked
**Why?** Service names such as `Credit` are nouns and require a suffix and should be named with a suffix when it is not obvious if it is a service or something else.
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@Injectable()
export class HeroDataService { }
td
:marked
hero-data.service.ts
tr(style=top)
td
code-example.
@Injectable()
export class CreditService { }
td
:marked
credit.service.ts
tr(style=top)
td
code-example.
@Injectable()
export class Logger { }
td
:marked
logger.service.ts
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-05"></a>Bootstrapping
#### <a href="#02-05">Style 02-05</a>
.s-rule.do
:marked
**Do** put bootstrapping and platform logic for the app in a file named `main.ts`.
.s-rule.do
:marked
**Do** include error handling in the bootstrapping logic.
.s-rule.avoid
:marked
**Avoid** putting app logic in the `main.ts`. Instead, consider placing it in a component or service.
.s-why
:marked
**Why?** Follows a consistent convention for the startup logic of an app.
.s-why.s-why-last
:marked
**Why?** Follows a familiar convention from other technology platforms.
+makeExample('style-guide/ts/02-05/main.ts', '', 'main.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-06"></a>Directive selectors
#### <a href="#02-06">Style 02-06</a>
.s-rule.do
:marked
**Do** Use lower camel case for naming the selectors of directives.
.s-why
:marked
**Why?** Keeps the names of the properties defined in the directives that are bound to the view consistent with the attribute names.
.s-why.s-why-last
:marked
**Why?** The Angular HTML parser is case sensitive and will recognize lower camel case.
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-07"></a>Custom prefix for components
#### <a href="#02-07">Style 02-07</a>
.s-rule.do
:marked
**Do** use a hyphenated, lowercase element selector value (e.g. `admin-users`).
.s-rule.do
:marked
**Do** use a custom prefix for a component selector.
For example, the prefix `toh` represents from **T**our **o**f **H**eroes and the prefix `admin` represents an admin feature area.
.s-rule.do
:marked
**Do** use a prefix that identifies the feature area or the app itself.
.s-why
:marked
**Why?** Prevents element name collisions with components in other apps and with native HTML elements.
.s-why
:marked
**Why?** Makes it easier to promote and share the component in other apps.
.s-why.s-why-last
:marked
**Why?** Components are easy to identify in the DOM.
+makeExample('style-guide/ts/02-07/app/heroes/hero.component.avoid.ts', 'example', 'app/heroes/hero.component.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/02-07/app/users/users.component.avoid.ts', 'example', 'app/users/users.component.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/02-07/app/heroes/hero.component.ts', 'example', 'app/heroes/hero.component.ts')
:marked
+makeExample('style-guide/ts/02-07/app/users/users.component.ts', 'example', 'app/users/users.component.ts')
:marked
:marked
### <a id="02-08"></a>Custom prefix for directives
#### <a href="#02-08">Style 02-08</a>
.s-rule.do
:marked
**Do** use a custom prefix for the selector of directives (e.g, the prefix `toh` from **T**our **o**f **H**eroes).
.s-rule.do
:marked
**Do** spell non-element selectors in lower camel case unless the selector is meant to match a native HTML attribute.
.s-why
:marked
**Why?** Prevents name collisions.
.s-why.s-why-last
:marked
**Why?** Directives are easily identified.
+makeExample('style-guide/ts/02-08/app/shared/validate.directive.avoid.ts', 'example', 'app/shared/validate.directive.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/02-08/app/shared/validate.directive.ts', 'example', 'app/shared/validate.directive.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-09"></a>Pipe names
#### <a href="#02-09">Style 02-09</a>
.s-rule.do
:marked
**Do** use consistent names for all pipes, named after their feature.
.s-why.s-why-last
:marked
**Why?** Provides a consistent way to quickly identify and reference pipes.
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@Pipe({ name: 'ellipsis' })
export class EllipsisPipe implements PipeTransform { }
td
:marked
ellipsis.pipe.ts
tr(style=top)
td
code-example.
@Pipe({ name: 'initCaps' })
export class InitCapsPipe implements PipeTransform { }
td
:marked
init-caps.pipe.ts
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-10"></a>Unit test file names
#### <a href="#02-10">Style 02-10</a>
.s-rule.do
:marked
**Do** name test specification files the same as the component they test.
.s-rule.do
:marked
**Do** name test specification files with a suffix of `.spec`.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify tests.
.s-why.s-why-last
:marked
**Why?** Provides pattern matching for [karma](http://karma-runner.github.io/) or other test runners.
:marked
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th Symbol Name
th File Name
tr(style=top)
td
:marked
Components
td
:marked
heroes.component.spec.ts
:marked
hero-list.component.spec.ts
:marked
hero-detail.component.spec.ts
tr(style=top)
td
:marked
Services
td
:marked
logger.service.spec.ts
:marked
hero.service.spec.ts
:marked
filter-text.service.spec.ts
tr(style=top)
td
:marked
Pipes
td
:marked
ellipsis.pipe.spec.ts
:marked
init-caps.pipe.spec.ts
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-11"></a>_End-to-End_ (E2E) test file names
#### <a href="#02-11">Style 02-11</a>
.s-rule.do
:marked
**Do** name end-to-end test specification files after the feature they test with a suffix of `.e2e-spec`.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify end-to-end tests.
.s-why.s-why-last
:marked
**Why?** Provides pattern matching for test runners and build automation.
:marked
:marked
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th Symbol Name
th File Name
tr(style=top)
td
:marked
End to End Tests
td
:marked
app.e2e-spec.ts
:marked
heroes.e2e-spec.ts
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="02-12"></a>Angular _NgModule_ names
#### <a href="#02-12">Style 02-12</a>
.s-rule.do
:marked
**Do** append the symbol name with the suffix `Module`.
.s-rule.do
:marked
**Do** give the file name the `.module.ts` extension.
.s-rule.do
:marked
**Do** name the module after the feature and folder it resides in.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify and reference modules.
.s-why
:marked
**Why?** Upper camel case is conventional for identifying objects that can be instantiated using a constructor.
.s-why.s-why-last
:marked
**Why?** Easily identifies the module as the root of the same named feature.
.s-rule.do
:marked
**Do** suffix a _RoutingModule_ class name with `RoutingModule`.
.s-rule.do
:marked
**Do** end the filename of a _RoutingModule_ with `-routing.module.ts`.
.s-why.s-why-last
:marked
**Why?** A `RoutingModule` is a module dedicated exclusively to configuring the Angular router.
A consistent class and file name convention make these modules easy to spot and verify.
- var top="vertical-align:top"
table(width="100%")
col(width="50%")
col(width="50%")
tr
th Symbol Name
th File Name
tr(style=top)
td
code-example.
@NgModule({ ... })
export class AppModule { }
td
:marked
app.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class HeroesModule { }
td
:marked
heroes.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class VillainsModule { }
td
:marked
villains.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class AppRoutingModule { }
td
:marked
app-routing.module.ts
tr(style=top)
td
code-example.
@NgModule({ ... })
export class HeroesRoutingModule { }
td
:marked
heroes-routing.module.ts
:marked
a(href="#toc") Back to top
.l-main-section
:marked
## Coding conventions
Have consistent set of coding, naming, and whitespace conventions.
.l-main-section
:marked
### <a id="03-01"></a>Classes
#### <a href="#03-01">Style 03-01</a>
.s-rule.do
:marked
**Do** use upper camel case when naming classes.
.s-why
:marked
**Why?** Follows conventional thinking for class names.
.s-why.s-why-last
:marked
**Why?** Classes can be instantiated and construct an instance.
By convention, upper camel case indicates a constructable asset.
+makeExample('style-guide/ts/03-01/app/core/exception.service.avoid.ts', 'example', 'app/shared/exception.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-01/app/core/exception.service.ts', 'example', 'app/shared/exception.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="03-02"></a>Constants
#### <a href="#03-02">Style 03-02</a>
.s-rule.do
:marked
**Do** declare variables with `const` if their values should not change during the application lifetime.
.s-why
:marked
**Why?** Conveys to readers that the value is invariant.
.s-why.s-why-last
:marked
**Why?** TypeScript helps enforce that intent by requiring immediate initialization and by
preventing subsequent re-assignment.
.s-rule.consider
:marked
**Consider** spelling `const` variables in lower camel case.
.s-why
:marked
**Why?** Lower camel case variable names (`heroRoutes`) are easier to read and understand
than the traditional UPPER_SNAKE_CASE names (`HERO_ROUTES`).
.s-why.s-why-last
:marked
**Why?** The tradition of naming constants in UPPER_SNAKE_CASE reflects
an era before the modern IDEs that quickly reveal the `const` declaration.
TypeScript itself prevents accidental reassignment.
.s-rule.do
:marked
**Do** tolerate _existing_ `const` variables that are spelled in UPPER_SNAKE_CASE.
.s-why.s-why-last
:marked
**Why?** The tradition of UPPER_SNAKE_CASE remains popular and pervasive,
especially in third party modules.
It is rarely worth the effort to change them at the risk of breaking existing code and documentation.
+makeExample('style-guide/ts/03-02/app/core/data.service.ts', '', 'app/shared/data.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="03-03"></a>Interfaces
#### <a href="#03-03">Style 03-03</a>
.s-rule.do
:marked
**Do** name an interface using upper camel case.
.s-rule.consider
:marked
**Consider** naming an interface without an `I` prefix.
.s-rule.consider
:marked
**Consider** using a class instead of an interface.
.s-why
:marked
**Why?** <a href="https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines" target="_blank">TypeScript guidelines</a>
discourage the `I` prefix.
.s-why
:marked
**Why?** A class alone is less code than a _class-plus-interface_.
.s-why
:marked
**Why?** A class can act as an interface (use `implements` instead of `extends`).
.s-why.s-why-last
:marked
**Why?** An interface-class can be a provider lookup token in Angular dependency injection.
+makeExample('style-guide/ts/03-03/app/core/hero-collector.service.avoid.ts', 'example', 'app/shared/hero-collector.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-03/app/core/hero-collector.service.ts', 'example', 'app/shared/hero-collector.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="03-04"></a>Properties and methods
#### <a href="#03-04">Style 03-04</a>
.s-rule.do
:marked
**Do** use lower camel case to name properties and methods.
.s-rule.avoid
:marked
**Avoid** prefixing private properties and methods with an underscore.
.s-why
:marked
**Why?** Follows conventional thinking for properties and methods.
.s-why
:marked
**Why?** JavaScript lacks a true private property or method.
.s-why.s-why-last
:marked
**Why?** TypeScript tooling makes it easy to identify private vs public properties and methods.
+makeExample('style-guide/ts/03-04/app/core/toast.service.avoid.ts', 'example', 'app/shared/toast.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-04/app/core/toast.service.ts', 'example', 'app/shared/toast.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="03-06"></a>Import line spacing
#### <a href="#03-06">Style 03-06</a>
.s-rule.consider
:marked
**Consider** leaving one empty line between third party imports and application imports.
.s-rule.consider
:marked
**Consider** listing import lines alphabetized by the module.
.s-rule.consider
:marked
**Consider** listing destructured imported symbols alphabetically.
.s-why
:marked
**Why?** The empty line separates _your_ stuff from _their_ stuff.
.s-why.s-why-last
:marked
**Why?** Alphabetizing makes it easier to read and locate symbols.
+makeExample('style-guide/ts/03-06/app/heroes/shared/hero.service.avoid.ts', 'example', 'app/heroes/shared/hero.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-06/app/heroes/shared/hero.service.ts', 'example', 'app/heroes/shared/hero.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
## Application structure and Angular modules
Have a near-term view of implementation and a long-term vision. Start small but keep in mind where the app is heading down the road.
All of the app's code goes in a folder named `app`.
All feature areas are in their own folder, with their own Angular module.
All content is one asset per file. Each component, service, and pipe is in its own file.
All third party vendor scripts are stored in another folder and not in the `app` folder.
You didn't write them and you don't want them cluttering `app`.
Use the naming conventions for files in this guide.
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="04-01"></a>_LIFT_
#### <a href="#04-01">Style 04-01</a>
.s-rule.do
:marked
**Do** structure the app such that you can `L`ocate code quickly,
`I`dentify the code at a glance,
keep the `F`lattest structure you can, and
`T`ry to be DRY.
.s-rule.do
:marked
**Do** define the structure to follow these four basic guidelines, listed in order of importance.
.s-why.s-why-last
:marked
**Why?** LIFT Provides a consistent structure that scales well, is modular, and makes it easier to increase developer efficiency by finding code quickly.
To confirm your intuition about a particular structure, ask:
_can I quickly open and start work in all of the related files for this feature_?
a(href="#toc") Back to top
.l-main-section
:marked
### <a id="04-02"></a>Locate
#### <a href="#04-02">Style 04-02</a>
.s-rule.do
:marked
**Do** make locating code intuitive, simple and fast.
.s-why.s-why-last
:marked