prefer_conditional_assignment: null 검사 대신 ??= 연산자를 사용하세요
prefer_conditional_assignment: null 검사 대신 ??= 연산자를 사용하세요
값이 null인지 일일이 검사하는 대신 ??= 연산자를 쓰도록 안내하는 린트 규칙이에요. 코드의 길이를 줄이고 의도를 더 명확하게 드러낼 수 있어요.
본문
설명
null인지 검사하는 대신 ??=를 사용하는 걸 선호하세요 (PREFER). Dart에는 ??= 연산자가 있으니, 해당하는 상황에서는 이걸 사용해서 코드를 더 간결하게 만드는 게 좋아요.
BAD:
String get fullName {
if (_fullName == null) {
_fullName = getFullUserName(this);
}
return _fullName;
}
GOOD:
String get fullName {
return _fullName ??= getFullUserName(this);
}
활성화 방법
prefer_conditional_assignment 규칙을 쓰려면 analysis_options.yaml 파일의 linter > rules 아래에 규칙을 추가하면 돼요.
linter:
rules:
- prefer_conditional_assignment
대신 YAML 맵(map) 문법으로 린터 규칙을 설정한다면, linter > rules 아래에 prefer_conditional_assignment: true를 추가해요.
linter:
rules:
prefer_conditional_assignment: true