[Flutter] Lint : 코딩컨벤션
팀에서 여러 개발자와 함께 앱을 개발할 때 가장 먼저 결정해야 하는 것이 바로 코딩 컨벤션이다.
팀에서 결정한 규칙에 따라서 코드를 만들어야 하고, 규칙을 정했으면 개발자들은 그 규칙에 따라 코드를 만들어야 하지만
개발을 하다보면 이런저런 핑계로 잘 지켜지지 않을 때가 많다.
조금은 강제적으로 이런 규칙을 적용해야 할 때가 있고, 가능하면 자동으로 포메팅정도는 해주면 좋을 것 같다.
그래서 등장한 것이 Lint이다.
대부분의 언어에서 Lint기능을 확장 프로그램으로 지원하는데 Flutter개발에서도 당연히 Lint를 적용할 수 있다.
현재 Flutter진영에서 사용하는 Lint는 크게 두가지로 나뉘는데
커뮤니티 중심으로 만들어진 lint패키지와, Dart에서 공식적으로 출시한 lints와 이를 flutter용으로 확장한 flutter_lints가 있다.
https://pub.dev/packages/lints
lints | Dart Package
Official Dart lint rules. Defines the 'core' and 'recommended' set of lints suggested by the Dart team.
pub.dev
https://pub.dev/packages/flutter_lints
flutter_lints | Dart Package
Recommended lints for Flutter apps, packages, and plugins to encourage good coding practices.
pub.dev
lint | Dart Package
An opinionated, community-driven set of lint rules for Dart and Flutter projects. Like pedantic but stricter
pub.dev
현재 flutter에서는 공식적으로는 lints와 flutter_lints를 표준이라고 이야기하고 있지만, pub.dev사이트에서는 lint가 더 인기가 좋은 것 같다.
flutter_lints
lints패키지를 기반으로 하며, flutter용 설정이 추가로 포함되어 있다.
설정
pubspec.yaml파일에 패키지를 추가 해 보자
개발할 때만 필요한 기능이니 dev_dependencies에 추가해서 사용하자
dev_dependencies:
flutter_lints: ^1.0.4
추가적인 설정은 analysis_options.yaml 파일에 추가하면 된다.
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.
# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml
linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-optionse
lint
오래전 부터 개발을 해왔다면 Lint패키지를 이미 사용하고 있을 수 있다. 설정법은 flutter_lints와 거의 유사하다.
설정
pubspec.yaml파일에 패키지를 추가 해 보자
개발할 때만 필요한 기능이니 dev_dependencies에 추가해서 사용하자
dev_dependencies:
lint: ^1.0.0
추가적인 설정은 analysis_options.yaml 파일에 추가하면 된다.
# This file configures the analyzer to use the lint rule set from `package:lint`
# For apps, use the default set
include: package:lint/analysis_options.yaml
# Packages, that may be distributed (i.e. via pub.dev) should use the package
# version, resulting in a better pub score.
# include: package:lint/analysis_options_package.yaml
# You might want to exclude auto-generated files from dart analysis
analyzer:
exclude:
#- '**.freezed.dart'
# You can customize the lint rules set to your own liking. A list of all rules
# can be found at https://dart-lang.github.io/linter/lints/options/options.html
linter:
rules:
# Util classes are awesome!
# avoid_classes_with_only_static_members: false
# Make constructors the first thing in every class
# sort_constructors_first: true
# Choose wisely, but you don't have to
# prefer_double_quotes: true
# prefer_single_quotes: true
혼자 개발할때도 유용하고, 팀으로 개발할 때는 당연히 적용해야 하니 설정 후 자신이 표준에 가까운 코딩스타일을 가지고 있는지 확인해보자
Lint Rules : https://dart.dev/tools/linter-rules
Linter rules
Details about the Dart linter and its style rules you can choose.
dart.dev
Dart의 기본 코딩스타일 : https://dart.dev/guides/language/effective-dart/style
Effective Dart: Style
Formatting and naming rules for consistent, readable code.
dart.dev