[Dart] Effective Dart - Style

jaehee kim·2021년 5월 24일
1

Dart

목록 보기
6/7
post-thumbnail

Effective Dart - Style

좋은 코드의 아주 중요한 부분은 좋은 스타일 입니다.

Identifiers

Dart 에는 3가지 유형의 Identifier가 있습니다.

  • UpperCamelCase
  • lowerCamelCase
  • lowercase_with_underscores

DO name types using UpperCamelCase.

Linter rule: camel_case_types

Class, enum, typedef, type parameter 의 첫 글자는 대문자로 표시해야 합니다.

class SliderMenu { ... }

class HttpRequest { ... }

typedef Predicate<T> = bool Function(T value);

annotation에 사용되는 것도 포함입니다.

class Foo {
  const Foo([arg]);
}

(anArg)
class A { ... }

()
class B { ... }

annotation class의 constructor가 parameter를 가지지 않으면 lowerCamelCase constant를 생성할 수 있습니다.

const foo = Foo();


class C { ... }

DO name extensions using UpperCamelCase.

Linter rule: camel_case_extensions

extension 도 첫 글자는 대문자로 표시해야 합니다.

extension MyFancyList<T> on List<T> { ... }

extension SmartIterable<T> on Iterable<T> { ... }

DO name libraries, packages, directories, and source files using lowercase_with_underscores.

Linter rules: library_names, file_names

<good>

library peg_parser.source_scanner;

import 'file_system.dart';
import 'slider_menu.dart';
<bad>

library pegparser.SourceScanner;

import 'file-system.dart';
import 'SliderMenu.dart';

DO name import prefixes using lowercase_with_underscores.

Linter rule: library_prefixes

<good>

import 'dart:math' as math;
import 'package:angular_components/angular_components'
    as angular_components;
import 'package:js/js.dart' as js;
<bad>

import 'dart:math' as Math;
import 'package:angular_components/angular_components'
    as angularComponents;
import 'package:js/js.dart' as JS;

DO name other identifiers using lowerCamelCase

Linter rule: non_constant_identifier_names

Class members, top-level definitions, variables, parameters, named parameters lowerCamelCase 를 사용해야 합니다.

var count = 3;

HttpRequest httpRequest;

void align(bool clearItems) {
  // ...
}

PREFER using lowerCamelCase for constant names.

Linter rule: constant_identifier_names

constant variables, enum values lowerCamelCase, 를 사용해야 합니다.

<good>

const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = RegExp('^([a-z]+):');

class Dice {
  static final numberGenerator = Random();
}
<bad>

const PI = 3.14;
const DefaultTimeout = 1000;
final URL_SCHEME = RegExp('^([a-z]+):');

class Dice {
  static final NUMBER_GENERATOR = Random();
}

DO capitalize acronyms and abbreviations longer than two letters like words.

예외 : IO와 같은 약어는 완전히 대문자로 표시합니다.

<good>

class HttpConnection {}
class DBIOPort {}
class TVVcr {}
class MrRogers {}

var httpRequest = ...
var uiHandler = ...
Id id;
<bad>

class HTTPConnection {}
class DbIoPort {}
class TvVcr {}
class MRRogers {}

var hTTPRequest = ...
var uIHandler = ...
ID iD;

PREFER using _, __, etc. for unused callback parameters.

<good>

futureOfVoid.then((_) {
  print('Operation complete.');
});

DON’T use a leading underscore for identifiers that aren’t private.

DON’T use prefix letters.

<good>

defaultTimeout
<bad>

kDefaultTimeout

Ordering

Linter rule: directives_ordering

DO place “dart:” imports before other imports.

<good>

import 'dart:async';
import 'dart:html';

import 'package:bar/bar.dart';
import 'package:foo/foo.dart';

DO place “package:” imports before relative imports.

<good>

import 'package:bar/bar.dart';
import 'package:foo/foo.dart';

import 'util.dart';

DO specify exports in a separate section after all imports.

<good>

import 'src/error.dart';
import 'src/foo_bar.dart';

export 'src/error.dart';
<bad>

import 'src/error.dart';
export 'src/error.dart';
import 'src/foo_bar.dart';

DO sort sections alphabetically.

<good>

import 'package:bar/bar.dart';
import 'package:foo/foo.dart';

import 'foo.dart';
import 'foo/foo.dart';
<bad>

import 'package:foo/foo.dart';
import 'package:bar/bar.dart';

import 'foo/foo.dart';
import 'foo.dart';

Formatting

일관된 whitespace style 은 사람들이 코드를 compiler와 같은 방식으로 볼수 있도록 도와줍니다.

DO format your code using dart format.

Formatting을 위해서 dart format 을 사용할 수 있습니다.

CONSIDER changing your code to make it more formatter-friendly.

AVOID lines longer than 80 characters.

Linter rule: lines_longer_than_80_chars

DO use curly braces for all flow control statements.

Linter rule: curly_braces_in_flow_control_structures

<good>

if (isWeekDay) {
  print('Bike to work!');
} else {
  print('Go dancing or read a book!');
}

예외 : else문이 없이 if 문을 사용하는 경우나 if문을 한줄로 작성가능한 경우 중괄호를 생략할 수 있습니다.

<good>

if (arg == null) return defaultValue;

만약 body가 다음 줄로 넘어가는 경우 중괄호를 사용합니다.

<good>

if (overflowChars != other.overflowChars) {
  return overflowChars < other.overflowChars;
}
<bad>

if (overflowChars != other.overflowChars)
  return overflowChars < other.overflowChars;






Reference

[Effective Dart - Style]

0개의 댓글