매개변수 3개 이상은 X - mapper 객체로 감싸서 거기서 꺼내 올 수 있게끔 만든다.
변수명, 엘리먼트명, 함수명 잘 기능될수있게 지어주기.
계층화 분리, 로직분리 신경쓰기.
다른 개발자들이 코드만 봐도 어떤 플로우인지 알아보기쉽게끔 작성하기.
import { Input, Output, EventEmitter } from '@angular/core';
export class HospitalCrmTargetTypeForm {
@Input() formValue = {};
@Output() onChange = new EventEmitter<{ value: string; key: string }>();
handleChange(event: Event) {
const { value, name: key } = event.target as HTMLInputElement;
this.onChange.emit({ value, key });
}
constructor() {}
}
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-hospital-crm-form-section',
templateUrl: './hospital-crm-form-section.component.html',
styleUrls: ['./hospital-crm-form-section.component.scss']
})
export class HospitalCrmFormSectionComponent implements OnInit {
@Input() tooltip = '';
@Input() index = 1;
@Input() title = '';
@Input() isBlocked = false;
@Input() blockMessage = '';
sectionIndex = '';
constructor() {}
ngOnInit() {
this.sectionIndex = String(this.index).padStart(2, '0');
}
}
//HTML
<section class="form-section">
<div class="form-section-title">
<b class="form-section-point"> {{ sectionIndex }}</b>
<h2>{{ title }}</h2>
<i *ngIf="tooltip" class="icon icon-question-mark adjust" [ngbTooltip]="tooltip" placement="bottom"></i>
<span *ngIf="isBlocked" class="form-section-sub">{{ blockMessage }}</span>
</div>
<div class="form-section-content">
<div *ngIf="isBlocked" class="form-section-dimmed"></div>
<ng-content></ng-content>
</div>
</section>
간단한 Text Wrapping의 경우 변하지 않는 값이면 상수, 변하는 값이면 객체에 key-value로 등록해놓기.
export const AGE_DATE_SUFFIX = {
YEAR: '세',
MONTH: '개월'
};
//HTML
<button class="ha-dropdown-toggle form-control" ngbDropdownToggle>
{{ formValue.startAge }}{{ AGE_DATE_SUFFIX[formValue.ageDateType] }}
</button>
로직과 관계없거나 어쩔수 없이 이번만 써야하는 변수/함수앞에는 언더바 스코어를 붙여준다.
변수에 값을 할당할때도 삼항연산자를 활용해 할당하는 걸 활용하면 좋다.
리턴대신 값을 바로 넣어야하는 경우 & trick을 써주면 좋다
handleChangeSymptom(id: string) {
this.symptoms$.pipe(take(1)).subscribe(symptoms => {
const index = symptoms.findIndex(({ _id: symptomsId }) => symptomsId === id);
this.scheduleForm.patchValue({
useSymptom: id && index >= 0,
...(id && index >= 0 && { symptom: symptoms[index].name })
});
});
}
굳이 안써도 되어도 이게 디폴트로 들어가는 값이라는 걸 명시해주기 위해 상수를 따로 만들어서 빼기
and trick, or trick 잘 활용해 변수에 값 할당하기
const newObj = ageType === 'SELECT' && { ageDateType, startAge: Number(startAge) || 0, endAge: Number(endAge) || 0 }
export const FILTER_TYPE = [
{ value: 'ALL', label: '전체' },
{ value: 'SELECT', label: '선택' }
];
export const BASIC_FILTER_TYPE = FILTER_TYPE[0].value;
Short-Circuiting
How to Shorten JavaScript Conditionals with Short-Circuiting * Reed Barger