[Dart] Conding Convention

๊น€์ •ํ˜„ยท2024๋…„ 1์›” 15์ผ
0

Dart

๋ชฉ๋ก ๋ณด๊ธฐ
2/3
post-thumbnail

Dart ๊ด€๋ จ ๋‚ด์šฉ์„ ์ฐพ์•„๋ณด๋˜ ์ค‘ ์žฌ๋ฐŒ๊ณ  ์œ ์ตํ•œ ๊ธ€์ด ์žˆ์–ด ๋ฒˆ์—ญ๋ณธ์„ ์˜ฌ๋ ค๋ณด๋ ค ํ•œ๋‹ค.
์˜ˆ์‹œ๋“ค์€ ์กฐ๊ธˆ ์ถ”๊ฐ€ํ•˜์˜€์œผ๋‹ˆ ๋„์›€์ด ๋˜๋ฉด ์ข‹๊ฒ ๋‹ค.
(p.s ์ฒจ๋ถ€๋œ ์‚ฌ์ง„์˜ ์›๋ฌธ ์ œ๋ชฉ๋ถ€ํ„ฐ ๋Š๊ปด์ง€๋“ฏ ์ž‘์„ฑ์ž ๋ถ„์ด ๊ฝค๋‚˜ ๊ฑฐ์น ๋‹ค. ๊ทธ ๋งˆ์ €๋„ ๋Š๋‚Œ์„ ๋Œ€์ถฉ ์‚ด๋ ค๋ณด๊ณ  ์‹ถ์—ˆ๋‹ค..)




๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ๋“ค์„ ์‚ดํŽด๋ณด๋Š” ๋™์•ˆ, ํ•ญ์ƒ ๊ฑฐ์Šฌ๋ฆฌ๋Š” ๊ฒŒ ์žˆ์—ˆ์–ด.
๊ทธ๊ฑด ๋Œ€๋ถ€๋ถ„์ด Dart ๋ฌธ๋ฒ• ๊ทœ์น™์„ ์ œ๋Œ€๋กœ ์ง€ํ‚ค์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฑฐ์•ผ.

๋„Œ ์•„๋งˆ ๋‹ค๋ฅธ ์–ธ์–ด๋“ค์„ ์จ์™”๊ฒ ์ง€๋งŒ ์ง€๊ธˆ์€ Dart๋ฅผ ์“ฐ๊ณ  ์žˆ๊ณ , ๋˜ Dart์—์„œ๋Š” ๋ช‡ ๊ฐ€์ง€๋ฅผ ์ข€ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋“ .

์‚ฌ์‹ค Dart ๋ฌธ์„œ๊ฐ€ ๋ชจ๋“  ๊ฑธ ๊น”๋”ํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์ง€๋งŒ ์šฐ๋ฆฌ ๋Œ€๋ถ€๋ถ„์€ ๋ฌธ์„œ๋ฅผ ๋‹ค ์ฝ๋Š” ๊ฑธ ๋„ˆ๋ฌด ๊ท€์ฐฎ์•„ํ•˜์ž–์•„.
๊ทธ๋ž˜์„œ ๋‚œ ์ด๋Ÿฐ ๊ฒŒ์œผ๋ฅธ ๋†ˆ๋“ค์„ ์œ„ํ•ด ์š”์•ฝ๋ณธ์„ ๋งŒ๋“ค๊ธฐ๋กœ ํ–ˆ์–ด.

๋„์›€์ด ๋˜๊ธธ ๋ฐ”๋ž„๊ฒŒ!

  • folders/files (lower_snake_case)

    my_widgets
    ใ„ด my_fancy_widget.dart
    ใ„ด my_hot_widget.dart
  • classes (UpperCamelCase)

    class MyFancyClass { ... }
  • extensions (UpperCamelCase)

    extension MyFancyList<T> on List<T> { ... }
  • mixins (UpperCamelCase)

    mixin MyCoolMixin { ... }
  • functions (lowerCamelCase)

    void MyGorgeousFunc() { ... }
  • variables (lowerCamelCase)

    var myPrettyVar
  • constants (lowerCamelCase)

    const CAPITALIZE_EVERY_DAMN_LETTER // NO
    const goodConstant // yes
  • enums (lowerCamelCase)

    enum Name { FIRST_NAME, LAST_NAME } // WRONG!!
    enum Name { firtName, lastName }    // RIGHT!!
  • ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ฝœ๋ฐฑ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” _, __ ๋“ฑ์œผ๋กœ ๊ธฐ์ž…ํ•ด๋ผ.

    // IF YOU WON'T USE DON'T MENTION IT
    futureOfVoid.then((unusedParameter) => print('Operation complete.'));
    futureOfVoid.then((_) => print('Operation complete.'));
  • ๋ฌธ์ž์—ด๊ณผ ๊ฐ’๋“ค์„ ๊ตฌ์„ฑํ•  ๋•, interpolation์„ ์„ ํ˜ธํ•ด๋ผ.

    // GOOD BOY
    'Hello, $name! You are ${year - birth} years old.';
    // BAD BOY
    'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';
  • ๋ถˆํ•„์š”ํ•œ getter/setter๋ฅผ ํ”ผํ•˜๋ผ.

    // GOOD
    class Box {
     var contents;
    }
    // BAD
    class Box {
     var _contents;
     get contents => _contents;
     set contents(value) {
       _contents = value;
     }
    }
  • ์  ์žฅํ•  ๋ชจ๋“  ํƒ€์ž…์„ ์–ด๋””๋“  ์ ์–ด๋ผ.

    add(a,b) => a + b; // DAMN WRONG
    int add(int a, int b) => a + b;  // HELL YEAH
    final List<String> users = <String>[];  // THAT'S OVERKILL
    final List<String> users = []; // GREAT
    final users = <String>[]; // WONDERFUL
  • ์„ ์‚ฌ์‹œ๋Œ€์  'new' ํ‚ค์›Œ๋“œ ์ข€ ์ œ๋ฐœ ๊ทธ๋งŒ ์‚ฌ์šฉํ•ด๋ผ.

    // I'm old dude
    new Container();
    // I'm a brand new energetic open-minded sexy young dude
    Container();

์ข€ ๊ณต๊ฒฉ์ ์ด๋ผ๋ฉด ๋ฏธ์•ˆํ•˜์ง€๋งŒ, ๋‹น์žฅ ์ฝ”๋“œ ์•ˆ ๊ณ ์น˜๋ฉด ๋‚ด๊ฐ€ ๋„ˆ ์ฐพ์•„๊ฐ„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กœ์šด ๋‚œ์žฅํŒ๋“ค์ด ๊ณ„์† ๋ณด์ด๋ฉด ๋‚ด์šฉ์„ ๋” ์ถ”๊ฐ€ํ•  ์ƒ๊ฐ์ด๋‹ˆ๊นŒ ์กฐ์‹ฌํ•ด.



๐Ÿ’ก Reference

medium post: Fix your Dart DAMN Syntax

profile
ํ•˜๊ณ ํ’๊ฑฐ(ๆฒณ้ผ“้ขจๅŽป) ์‚ญ๋‹คํ•˜์ž(ๅ‰Šๅคšใฐคๆ…ˆ)

0๊ฐœ์˜ ๋Œ“๊ธ€