no_leading_underscores_for_local_identifiers 규칙 — 로컬 식별자의 앞 밑줄 피하기

no_leading_underscores_for_local_identifiers 규칙 — 로컬 식별자의 앞 밑줄 피하기

Dart의 밑줄은 멤버나 최상위 선언의 private을 표현하는 역할을 해요. 그래서 private이 아닌 로컬 변수나 매개변수에 밑줄을 쓰면 읽는 사람을 혼란스럽게 만들어요. 이 린트 규칙은 그런 이름을 쓰지 않도록 도와줘요.

출처: no_leading_underscores_for_local_identifiers

본문

private이 아닌 로컬 식별자에는 앞 밑줄을 쓰지 마세요.

Dart는 식별자 앞의 밑줄을 사용해서 멤버와 최상위 선언이 private이라는 것을 표시해요. 그래서 개발자들은 앞 밑줄을 보면 "private"이라고 생각하게 돼요. 그런데 로컬 변수나 매개변수에는 "private"이라는 개념이 없어요. 이름이 밑줄로 시작하면 읽는 사람에게 혼란스러운 신호를 보내요. 그런 혼란을 피하려면 이런 이름에 앞 밑줄을 쓰지 마세요.

예외: 사용되지 않는 매개변수는 _, __, ___ 처럼 이름을 지을 수 있어요. 콜백에서 값을 받긴 하지만 쓸 일이 없을 때 흔히 쓰는 방식이에요. 값이 사용되지 않는다는 걸 밑줄로만 이루어진 이름으로 표현하는 게 관용적인 방법이에요.

좋지 않은 예:

void print(String _name) {
  var _size = _name.length;
  ...
}

좋은 예:

void print(String name) {
  var size = name.length;
  ...
}

괜찮은 예:

[1,2,3].map((_) => print('Hello'));

활성화하기

no_leading_underscores_for_local_identifiers 규칙을 활성화하려면 analysis_options.yaml 파일의 linter > rules 아래에 no_leading_underscores_for_local_identifiers를 추가하면 돼요:

linter:
  rules:
    - no_leading_underscores_for_local_identifiers

린트 규칙을 YAML 맵 문법으로 설정하고 있다면, linter > rules 아래에 no_leading_underscores_for_local_identifiers: true를 추가하면 돼요:

linter:
  rules:
    no_leading_underscores_for_local_identifiers: true

더 알아보기