forked from angular/angular.io
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstyle-guide.jade
1979 lines (1552 loc) · 54.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 2 Style Guide
## Purpose
If you are looking for an opinionated style guide for syntax, conventions, and structuring Angular applications, then step right in.
The purpose of this style guide is to provide guidance on building Angular applications by showing the conventions we use and, more importantly, why we choose them.
.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, we 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 we 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. (e.g. 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. [Application Structure](#application-structure)
1. [Components](#components)
1. [Directives](#directives)
1. [Services](#services)
1. [Data Services](#data-services)
1. [Lifecycle Hooks](#lifecycle-hooks)
1. [Routing](#routing)
1. [Appendix](#appendix)
.l-main-section
:marked
## Single Responsibility
We apply the [Single Responsibility Principle](https:\/\/en.wikipedia.org/wiki/Single_responsibility_principle) to all Components, Services, and other symbols we create. This helps make our app cleaner, easier to read and maintain, and more testable.
### Rule of One
<a id="01-01"></a>
#### Style 01-01
.s-rule.do
:marked
**Do** define one thing (e.g. 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 Component 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
Better to redistribute the component and supporting activities into their own dedicated files.
+makeTabs(
`style-guide/ts/01-01/app/main.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`,
'',
`app/main.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
### Small Functions
<a id="01-02"></a>
#### Style 01-02
.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
### General Naming Guidelines
<a id="02-01"></a>
#### Style 02-01
.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 us find our 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
### Separate File Names with Dots and Dashes
<a id="02-02"></a>
#### Style 02-02
.s-rule.do
:marked
**Do** use dashes to separate words.
.s-rule.do
:marked
**Do** use dots to separate the descriptive name from the type.
.s-rule.do
:marked
**Do** use consistent 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 suffixes for the types including `*.service.ts`, `*.component.ts`, `*.pipe.ts`. Invent other suffixes where desired, but take care in having too many.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify what is in the file.
.s-why
:marked
**Why?** Provides a consistent way to quickly find a specific file using an editor or IDE's fuzzy search techniques.
.s-why.s-why-last
:marked
**Why?** Provides pattern matching for any automated tasks.
a(href="#toc") Back to top
.l-main-section
:marked
### Components and Directives
<a id="02-03"></a>
#### Style 02-03
.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 symbols. Match the name of the symbol to the naming of the file.
.s-rule.do
:marked
**Do** append the symbol name with the suffix that it represents.
.s-why
:marked
**Why?** Provides a consistent way to quickly identify and reference assets.
.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?** The `Component` suffix is more commonly used and is more explicitly descriptive.
- 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
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### Service Names
<a id="02-04"></a>
#### Style 02-04
.s-rule.do
:marked
**Do** use consistent names for all services named after their feature.
.s-rule.do
:marked
**Do** use upper camel case for services.
.s-rule.do
:marked
**Do** suffix services with `Service` when it is not clear what they are (e.g. when they are nouns).
.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
### Bootstrapping
<a id="02-05"></a>
#### Style 02-05
.s-rule.do
:marked
**Do** put bootstrapping and platform logic for the app in a file named `main.ts`.
.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.
a(href="#toc") Back to top
.l-main-section
:marked
### Directive Selectors
<a id="02-06"></a>
#### Style 02-06
.s-rule.do
:marked
**Do** Use lower camel case for naming the selectors of our 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 2 HTML parser is case sensitive and will recognize lower camel case.
a(href="#toc") Back to top
.l-main-section
:marked
### Custom Prefix for Components
<a id="02-07"></a>
#### Style 02-07
.s-rule.do
:marked
**Do** use a custom prefix for the selector of our components. 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 name collisions.
.s-why
:marked
**Why?** Makes it easier to promote and share our feature in other apps.
.s-why.s-why-last
:marked
**Why?** Our Components and elements are easily identified.
+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
### Custom Prefix for Directives
<a id="02-08"></a>
#### Style 02-08
.s-rule.do
:marked
**Do** use a custom prefix for the selector of our directives (for instance below we use the prefix `toh` from **T**our **o**f **H**eroes).
.s-why
:marked
**Why?** Prevents name collisions.
.s-why.s-why-last
:marked
**Why?** Our 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
### Pipe Names
<a id="02-09"></a>
#### Style 02-09
.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
### Unit Test File Names
<a id="02-10"></a>
#### Style 02-10
.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
### End to End Test File Names
<a id="02-11"></a>
#### Style 02-11
.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
## Coding Conventions
Have consistent set of coding, naming, and whitespace conventions.
.l-main-section
:marked
### Classes
<a id="03-01"></a>
#### Style 03-01
.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. We often use upper camel case to indicate a constructable asset.
+makeExample('style-guide/ts/03-01/app/shared/exception.service.avoid.ts', 'example', 'app/shared/exception.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-01/app/shared/exception.service.ts', 'example', 'app/shared/exception.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### Constants
<a id="03-02"></a>
#### Style 03-02
.s-rule.do
:marked
**Do** use uppercase with underscores when naming constants.
.s-why
:marked
**Why?** Follows conventional thinking for constants.
.s-why.s-why-last
:marked
**Why?** Constants can easily be identified.
+makeExample('style-guide/ts/03-02/app/shared/data.service.avoid.ts', 'example', 'app/shared/data.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-02/app/shared/data.service.ts', 'example', 'app/shared/data.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### Interfaces
<a id="03-03"></a>
#### Style 03-03
.s-rule.do
:marked
**Do** name an interface using upper camel case.
.s-rule.do
:marked
**Consider** naming an interface without an `I` prefix.
.s-why.s-why-last
:marked
**Why?** When we use types, we can often simply use the class as the type.
+makeExample('style-guide/ts/03-03/app/shared/hero-collector.service.avoid.ts', 'example', 'app/shared/hero-collector.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-03/app/shared/hero-collector.service.ts', 'example', 'app/shared/hero-collector.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### Properties and Methods
<a id="03-04"></a>
#### Style 03-04
.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/shared/toast/toast.service.avoid.ts', 'example', 'app/shared/toast/toast.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-04/app/shared/toast/toast.service.ts', 'example', 'app/shared/toast/toast.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### Import Destructuring Spacing
<a id="03-05"></a>
#### Style 03-05
.s-rule.do
:marked
**Do** leave one whitespace character inside of the `import` statements' curly braces when destructuring.
.s-why.s-why-last
:marked
**Why?** Whitespace makes it easier to read the imports.
+makeExample('style-guide/ts/03-05/app/+heroes/shared/hero.service.avoid.ts', 'example', 'app/+heroes/shared/hero.service.ts')(avoid=1)
:marked
+makeExample('style-guide/ts/03-05/app/+heroes/shared/hero.service.ts', 'example', 'app/+heroes/shared/hero.service.ts')
:marked
a(href="#toc") Back to top
.l-main-section
:marked
### Import Line Spacing
<a id="03-06"></a>
#### Style 03-06
.s-rule.do
:marked
**Do** leave one empty line between third party imports and imports of code we created.
.s-rule.do
:marked
**Do** list import lines alphabetized by the module.
.s-rule.do
:marked
**Do** list destructured imported assets alphabetically.
.s-why
:marked
**Why?** The empty line makes it easy to read and locate imports.
.s-why.s-why-last
:marked
**Why?** Alphabetizing makes it easier to read and locate imports.
+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
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 content is 1 feature per file. Each component, service, and pipe is in its own file. All 3rd party vendor scripts are stored in another folder and not in the `app` folder. We didn't write them and we don't want them cluttering our app. Use the naming conventions for files in this guide.
a(href="#toc") Back to top
.l-main-section
:marked
### LIFT
<a id="04-01"></a>
#### Style 04-01
.s-rule.do
:marked
**Do** structure the app such that we can `L`ocate our code quickly, `I`dentify the code at a glance, keep the `F`lattest structure we 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. Another way to check our app structure is to ask ourselves: How quickly can we open and work in all of the related files for a feature?
a(href="#toc") Back to top
.l-main-section
:marked
### Locate
<a id="04-02"></a>
#### Style 04-02
.s-rule.do
:marked
**Do** make locating our code intuitive, simple and fast.
.s-why.s-why-last
:marked
**Why?** We find this to be super important for a project. If we cannot find the files we need to work on quickly, we will not be able to work as efficiently as possible, and the structure will need to change. We may not know the file name or where its related files are, so putting them in the most intuitive locations and near each other saves a ton of time. A descriptive folder structure can help with this.
a(href="#toc") Back to top
.l-main-section
:marked
### Identify
<a id="04-03"></a>
#### Style 04-03
.s-rule.do
:marked
**Do** name the file such that we instantly know what it contains and represents.
.s-rule.do
:marked
**Do** be descriptive with file names and keep the contents of the file to exactly one component.
.s-rule.avoid
:marked
**Avoid** files with multiple components, multiple services, or a mixture.
.s-why.s-why-last
:marked
**Why?** We spend less time hunting and pecking for code, and become more efficient. If this means we want longer file names, then so be it.
.l-sub-section
:marked
There are deviations of the 1 per file rule when we have a set of very small features that are all related to each other, as they are still easily identifiable.
a(href="#toc") Back to top
.l-main-section
:marked
### Flat
<a id="04-04"></a>
#### Style 04-04
.s-rule.do
:marked
**Do** keep a flat folder structure as long as possible.
.s-rule.consider
:marked
**Consider** creating folders when we get to seven or more files.
.s-why.s-why-last
:marked
**Why?** Nobody wants to search seven levels of folders to find a file. In a folder structure there is no hard and fast number rule, but when a folder has seven to ten files, that may be time to create subfolders. We base it on our comfort level. Use a flatter structure until there is an obvious value (to help the rest of LIFT) in creating a new folder.
a(href="#toc") Back to top
.l-main-section
:marked
### T-DRY (Try to be DRY)
<a id="04-05"></a>
#### Style 04-05
.s-rule.do
:marked
**Do** be DRY (Don't Repeat Yourself)
.s-rule.avoid
:marked
**Avoid** being so DRY that we sacrifice readability.
.s-why.s-why-last
:marked
**Why?** Being DRY is important, but not crucial if it sacrifices the others in LIFT, which is why we call it T-DRY. We don’t want to type `hero-view.component.html` for a view because, well, it’s obviously a view. If it is not obvious or by convention, then we name it.
a(href="#toc") Back to top
.l-main-section
:marked
### Overall Structural Guidelines
<a id="04-06"></a>
#### Style 04-06
.s-rule.do
:marked
**Do** start small but keep in mind where the app is heading down the road.
.s-rule.do
:marked
**Do** have a near term view of implementation and a long term vision.
.s-rule.do
:marked
**Do** put all of the app's code in a folder named `app`.
.s-rule.consider
:marked
**Consider** creating a folder for each component including its `.ts`, `.html`, `.css` and `.spec` file.
.s-why
:marked
**Why?** Helps us keep the app structure small and easy to maintain in the early stages, while being easy to evolve as the app grows.
.s-why.s-why-last
:marked
**Why?** Components often have four files (e.g. `*.html`, `*.css`, `*.ts`, and `*.spec.ts`) and can clutter a folder quickly.
.example-title Overall Folder and File Structure
.filetree
.file src
.children
.file app
.children
.file +heroes
.children
.file hero
.children
.file hero.component.ts|html|css|spec.ts
.file index.ts
.file hero-list
.children
.file hero-list.component.ts|html|css|spec.ts
.file index.ts
.file shared
.children
.file hero.model.ts
.file hero.service.ts|spec.ts
.file index.ts
.file heroes.component.ts|html|css|spec.ts
.file index.ts
.file shared
.children
.file ...
.file app.component.ts|html|css|spec.ts
.file main.ts
.file index.html
.file ...
:marked
.l-sub-section
:marked
While we prefer our Components to be in their own dedicated folder, another option for small apps is to keep Components flat (not in a dedicated folder). This adds up to four files to the existing folder, but also reduces the folder nesting. Be consistent.
a(href="#toc") Back to top
.l-main-section
:marked
### Shared Folder
<a id="04-07"></a>
#### Style 04-07
.s-rule.do
:marked
**Do** put all shared files within a component feature in a `shared` folder.
.s-rule.consider
:marked
**Consider** creating a folder for each component including its `.ts`, `.html`, `.css` and `.spec` file.
.s-why
:marked
**Why?** Separates shared files from the components within a feature.
.s-why.s-why-last
:marked
**Why?** Makes it easier to locate shared files within a component feature.