Home [Dart]Dart์—์„œ Getter์™€ Setter
Post
Cancel

[Dart]Dart์—์„œ Getter์™€ Setter

๐Ÿ“Œ Dart์—์„œ Getter์™€ Setter

์—ฌ๋Š๋•Œ์™€ ๊ฐ™์ด Peach-Tri์—์„œ ์Šคํ„ฐ๋””๋ฅผ ์ง„ํ–‰์ค‘์ด์—ˆ๋‹ค. ์ง„ํ–‰ ๋„์ค‘์— riverpod ์˜ˆ์‹œ์ฝ”๋“œ๋“ค์— getter setter ์„ธํŒ…์ด ์ „ํ˜€ ์—†๋‹ค๋Š” ์ ์„ ํŒ€์›์ด ์ด์ƒํ•˜๊ฒŒ ๋Š๊ผˆ๊ณ , ๊ทธ๊ฑธ ๊ธฐ์ ์œผ๋กœ Dart๋ผ๋Š” ์–ธ์–ด์˜ ์ง€ํ–ฅ์ ์ด ์–ด๋”˜์ง€์— ๋Œ€ํ•ด์„œ ์˜๋ฌธ์„ ํ’ˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋„ˆ๋ฌด๋‚˜๋„ ๋‹น์—ฐํ•˜๊ฒŒ Java์Šคํƒ€์ผ๋กœ Getter Setter๋ฅผ ์ •ํ•ด๋†“๋˜ ์šฐ๋ฆฌ๋Š” ์ฝ”๋“œ ์ž‘์„ฑ ์‹œ๊ฐ„๋ณด๋‹ค ๋””์ž์ธ ํŒจํ„ด ํŒŒ์•…์— ๊ธด ์‹œ๊ฐ„์„ ์Ÿ์€ ๊ฒฐ๊ณผ, Dart์˜ ๋„ํ๋จผํŠธ์™€ ๋ช‡๋ช‡ ๊ธ€๋“ค์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ , ์‹œ์ž‘์€ Dart์ง€๋งŒ ์•„๋งˆ ์ด ๋‚ด์šฉ์€ ์ „๋ฐ˜์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ํ๋ฆ„์— ๋Œ€ํ•ด์„œ ์ผ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

๐Ÿ“Ž Getter? Setter?

โ˜• In Java

Java์—์„œ Getter์™€ Setter๋Š” ํ•„์ˆ˜์ ์ด๋‹ค. Lombok์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด์„œ๋ผ๋„, ์ด ๋ถˆํŽธํ•œ accessor๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•œ๋‹ค. ์ด๋Š” OOP์˜ ์›์น™ ์ค‘ ํ•˜๋‚˜์ธ ์บก์Šํ™”๋ฅผ ์ด๋ค„๋‚ด๊ธฐ ์œ„ํ•จ์ด๋ฉฐ, CLASS ๋‚ด๋ถ€์ ์œผ๋กœ private๋กœ ์„ ์–ธํ•œ field๋“ค์— ๋Œ€ํ•ด์„œ ์ ‘๊ทผ์„ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜์˜ ์ž…์ถœ๊ตฌ๋กœ ์ƒ๊ฐํ•˜๋ฉด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•™๋ถ€์ƒ์ •๋„์—์„œ, ๊ทธ๋ฆฌ๊ณ  ๊นŠ์ง€ ์•Š์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€์—์„œ๋Š” DTO๋‚˜ Entity์˜ ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์ด ์ค„์ค„์ด getter์™€ setter๋ฅผ, ๊ทธ์ € ๋‹จ์ˆœ์ ‘๊ทผ์„ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด๋ผ๋Š” ๋ชฉ์ ํ•˜์— ์ค„์ค„์ด ๋‹ฌ๋ ค์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์ด๋Ÿฐ ์ ‘๊ทผ์ž๋“ค์ด ์ง„์งœ๋กœ ํ•„์š”ํ•œ์ง€์— ๋Œ€ํ•ด์„œ ์žฌ๊ณ ํ•ด์•ผํ•œ๋‹ค.

๊ณผ์—ฐ ๊ทธ์ € ๋ชจ๋“  field๋“ค์— getter์™€ setter๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ ๋งŒ์œผ๋กœ ์บก์Šํ™”๋ผ๋Š” ๋ชฉํ‘œ๋ฅผ ์ด๋ฃฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ผ๊นŒ?

๐ŸŽฏ In Dart

๋‹คํŠธ๋Š” ์ด๋Ÿฐ ๋‹จ์ˆœ์ ‘๊ทผ์ž๋“ค์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์„ ์ง€์–‘ํ•˜๋ผ๊ณ  ๋‚˜์™€์žˆ๋‹ค.

AVOID wrapping fields in getters and setters just to be โ€œsafeโ€.

๋‹จ์ˆœํ•œ ์•ˆ์ „์„ ์œ„ํ•ด์„œ, getter์™€ setter๋ฅผ ํ†ตํ•ด์„œ ํ•„๋“œ๋“ค์„ ๋งคํ•‘ํ•˜๋Š”๊ฒƒ์€ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค๋Š” ๋ง์ด๋‹ค

๐Ÿคฆ ๊ทผ๋ฐ ์ด์ œ์™€์„œ ์™œ ์šฐ๋ฆฌ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๊ฑธ๊นŒ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Example {
  private String name;
  private int age;
  
  public void setName (String name) {
    this.name = name;
  }
  public String getName (String name) {
    return this.name;
  }
  public void set age (int age) {
    this.age = age;
  }
  public int getAge (int age) {
    return age;
  }
}

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž๋ฐ” ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ, ๊ณผ์—ฐ ์บก์Šํ™”๋˜์–ด ์žˆ๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณด์ž, ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€ field๋“ค์€ private ์ƒํƒœ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ •์ž‘ ๋ชจ๋“  field๋“ค์ด public getter์™€ setter๋กœ ์ธํ•ด์„œ ๋…ธ์ถœ๋˜์–ด ์žˆ๋Š” ์ƒํƒœ์ด๋‹ค. ์™ธ๋ถ€์—์„œ ์ด class๋ฅผ ํ™œ์šฉํ•˜๋ ค๊ณ  ํ•œ๋‹ค๋ฉด, ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ค ๊ตฌ์กฐ๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”์ง€, ์–ด๋–ค ํ˜•ํƒœ๋ฅผ ๋„๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„์•ผ ํ•˜๊ณ , ์ด๋Ÿฐ ๊ตฌ์กฐ๋Š” getter setter์—†์ด ๊ทธ๋ƒฅ .์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋ณ„๋ฐ˜ ๋‹ค๋ฅผ ๋ฐ” ์—†๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์€ ์–ธ๋œป ๋Š๋ผ๊ธฐ์—๋Š” ์บก์Šํ™”๋ฅผ ๋‹ฌ์„ฑํ•œ ๊ฒƒ ๊ฐ™์•„ ๋ณด์ธ๋‹ค. ํ•˜์ง€๋งŒ ๊ถ๊ทน์ ์œผ๋กœ ์ถ”์ƒํ™”๋ฅผ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” OOP์—์„œ๋Š” ์˜คํžˆ๋ ค ์ถ”์ƒํ™”๋ฅผ ํ•ด์น˜๊ณ  ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ๋“œ๋Ÿฌ๋‚ธ ๊ฒƒ๊ณผ ๋‹ค๋ฅผ๋ฐ” ์—†๋Š” ์ฝ”๋“œ๊ฐ€ ๋˜์–ด ๋ฒ„๋ฆฐ๋‹ค. ๋‹จ์ˆœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ๋ชฉ์ ์ด ์•„๋‹Œ, ๋‚˜๋ฆ„์˜ ์—ญํ• ๊ณผ ์ฑ…์ž„์„ ๊ฐ€์ง„ ํด๋ž˜์Šค๊ฐ€ ์ด๋Ÿฌํ•œ ์ƒํƒœ๋ผ๋ฉด ์บก์Šํ™”๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค๊ณ  ๋ณด๊ธฐ ์–ด๋ ต๋‹ค.

1
2
3
4
5
...
public boolean isSame(Example example) {
  return (example.age == someAge) ? true : false;
}
...

์ ‘๊ทผ์ œ์–ด์ž๋“ค์€ API ์ฆ‰ ์ธํ„ฐํŽ˜์ด์Šค๋“ค์ด๊ณ  ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‚ด๋ถ€๋กœ์ง์„ ์ˆจ๊ธฐ๊ณ  ๊ฐ์ฒด์—๊ฒŒ ์—ญํ• ์„ ๋ถ€์—ฌํ•˜๋ฉฐ, ์™ธ๋ถ€์—์„œ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋“ค์ด ๋‚ด๋ถ€ ๊ฐ’์„ ๊ฐ€์ ธ๊ฐ€ ๋น„๊ตํ•˜๊ฑฐ๋‚˜ ๋ณ€ํ™˜ํ•˜๋Š”๊ฒƒ์ด ์•„๋‹Œ, ํ•ด๋‹น๊ฐ์ฒด์—๊ฒŒ ๊ฐ’์„ ์ฃผ๋ฉด์„œ ์งˆ๋ฌธ์„ ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค. ์•„๋ž˜์˜ ๋ฉ”์†Œ๋“œ๋Š” Example๋‚ด๋ถ€์— ์กด์žฌํ•˜๋ฉฐ, privateํ•œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ ๋„, ๊ฐ’์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
class Example {
  ...
  
  public boolean isSame (int age) {
    return (age == this.age) ? true : false;
  }
  public boolean isSame (Stirng name) {
    return (name == this.name) ? true : false;
  }
  
  ...
}

์œ„์˜ ์˜ˆ์‹œ๋“ค์€ ๊ทน๋„๋กœ ๋‹จ์ˆœํ•˜์—ฌ ์ฐจ๋ผ๋ฆฌ getter setter์—†์ด field๋ฅผ ๋ฐ”๋กœ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ๋” ์‹ค์šฉ์ ์ด๊ฒ ์ง€๋งŒ, ๋” ๋ณต์žกํ•˜๊ณ , ํ˜น์€ ๋” ๋‹ค์–‘ํ•œ ์ˆœ๊ฐ„๋“ค์—์„œ ๋‹จ์ˆœํ•œ ๊ฐ’ ์ „๋‹ฌ์„ ์ข€ ๋” ํ™•์‹คํ•˜๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด์„œ getter์™€ setter๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์˜คํžˆ๋ ค ์บก์Šํ™”๋ฅผ ํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ฐฉ๊ฐ์— ์‚ฌ๋กœ์žกํž ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.
์บก์Šํ™”๋Š” ์ •๋ณด์€๋‹‰, ๊ทธ๋ฆฌ๊ณ  ๋” ๋‚˜์•„๊ฐ€์„œ OOP์˜ ๊ถ๊ทน์  ๋ชฉํ‘œ์ธ ์ถ”์ƒํ™”์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด, ๊ทธ๋ฆฌ๊ณ  ๊ฐ ํด๋ž˜์Šค์— ์—ญํ• ๊ณผ ์ฑ…์ž„์„ ๋ถ€์—ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ๊ฐ์ฒด๋“ค์ด ์„œ๋กœ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ์œ„ํ•ด์„œ ๊ตฌ์กฐ๋ฅผ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ ๊ฐ์ฒด๋“ค์ด ์„œ๋กœ์—๊ฒŒ ์งˆ๋ฌธ์„ ๋˜์ง€๊ณ  ๋‹ต๋ณ€์„ ๋“ค์œผ๋ฉฐ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์„ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•˜๋‹ค.

๐Ÿ’ ๊ทธ๋ ‡๋‹ค๋ฉด ์ ์ ˆํ•œ Accessor์˜ ์‚ฌ์šฉ์€ ์–ด๋–ค ๋•Œ ์ผ๊นŒ?

Accessor๋“ค์„ ํ™œ์šฉํ•˜๊ธฐ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ์œ„์น˜๋Š” ๋ฐ”๋กœ ์ถ”๊ฐ€์ ์ธ ์—ฐ์‚ฐ์ด๋‚˜ ๋ฐ์ดํ„ฐ์— ๋ณ€๋™์ด ํ•„์š”ํ•œ ๋•Œ์ด๋‹ค. getter๋‚˜ setter๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์ด ๋ฐ”๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ทธ ์ค‘๊ฐ„์— ๊ฐ’์„ ๋นผ์˜จ๋‹ค๋Š”์ง€, ๋ฐ์ดํ„ฐ์— ๋”ฐ๋ผ์„œ ๋”ฐ๋กœ ๊ฐ€๊ณต์ด ํ•„์š”ํ•œ ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ์—๋Š” ์ด๋Ÿฐ getter setter๋ฅผ ํ†ตํ•ด์„œ ์ค‘๊ฐ„๋‹ค๋ฆฌ ์—ญํ• ์„ ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ๊ฒฝ์šฐ๋Š” ์˜์‹ฌ์น˜ ์•Š๊ณ  getter setter๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋„์›€์ด ๋  ๊ฒƒ์ด๋‹ค. ํ˜น์€ ์ž…๋ ฅ๊ฐ’์— ๋”ฐ๋ผ์„œ Data๋‚ด๋ถ€์— ์žˆ๋Š” Type์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. Data๊ฐ€ ํ’ˆ๊ณ  ์žˆ๋Š” ๊ฐ’์„ String์œผ๋กœ ์ž…๋ ฅ๋œ ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ๋‹ค๋˜์ง€ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” getter setter๋ฅผ ํ™œ์šฉํ•  ์—ฌ์ง€๊ฐ€ ์ถฉ๋ถ„ํžˆ ์กด์žฌํ•œ๋‹ค.

์ง์ ‘์ ์œผ๋กœ ์ž๋ฃŒํ˜• ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•˜์ง€ ๋ง๊ณ , ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ๋ฌด์—‡์„ ํ•˜๊ณ  ์‹ถ์€์ง€๋ฅผ ๋ถ„๋ฆฌํ•ด๋‚ด์ž

์‚ฌ์‹ค ์œ„์™€ ๊ฐ™์€ ๋ถ€๋ถ„์—์„œ๋„ getter๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์• ๋งคํ•œ ๋ถ€๋ถ„์ด ๋ถ„๋ช…ํžˆ ์กด์žฌํ•œ๋‹ค. ์ •๋ง ๋‹จ์ˆœํžˆ (ex: ์ „ํ™”๋ฒˆํ˜ธ ํŒŒ์‹ฑ์ด๋ผ๋˜์ง€) ํ•˜๋Š” ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋ผ๋ฉด, ์–ด๋–ค ์ผ์„ ํ• ์ง€์— ๋” ์ค‘์ ์„ ๋‘๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์ง ํ•˜๋‹ค.

DONT

1
2
3
4
5
6
class ์„œ๋น„์Šค ๊ฐ์ฒด {
  fun addThisToExampleListClass(example: Example) {
    exampleListClassInstance.getNames.add(example.name)
    exampleListClassInstance.getAges.add(example.age)
  }
}

DO

1
2
3
4
5
6
7
8
9
10
11
12
class ์„œ๋น„์Šค ๊ฐ์ฒด {
  fun addThisToExampleListClass(example: Example) {
    exampleListClassInstance.addExample(example)
  }
}

class ExampleListClass {
  fun addExample(example: Example) {
    this.exampleList.put(example)
  }
  
}

์œ„์˜ ์˜ˆ์‹œ๊ฐ€ ์™„์„ฑ๋„ ๋†’์ง€๋Š” ์•Š์ง€๋งŒ, ์˜ˆ์‹œ์ฒ˜๋Ÿผ, ์„œ๋น„์Šค ๊ฐ์ฒด์—์„œ ์ง์ ‘ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์™€ ๋‚ด๋ถ€์— getter setter๋ฅผ ํ†ตํ•ด์„œ ๋ฒ„๋ฌด๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ์ฒด์— ์žˆ๋Š” publicํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์–ด๋–ค ๋™์ž‘์„ ํ•˜๋„๋ก ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๊ณ , ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด๋Š” ์š”์ฒญ์— ๋งž๋Š” ํ–‰๋™์„ ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ๋” ๋ฐ”๋žŒ์งํ•œ ์บก์Šํ™”๊ฐ€ ๋œ๋‹ค.


๐Ÿ“– ์ฐธ๊ณ ์ž๋ฃŒ

  • https://erik-engheim.medium.com/to-hell-with-setters-and-getters-7814e7b2f949

  • https://dart-lang.github.io/linter/lints/unnecessary_getters_setters.html

  • https://stackoverflow.com/questions/61720221/why-should-i-avoid-wrapping-fields-in-getters-and-setters

  • https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors

This post is licensed under CC BY 4.0 by the author.

[Server]๋ถ„์‚ฐ ๋ชจ๋†€๋ฆฌ์‹ VS MSA

[Kotlin] ์ฝ”ํ‹€๋ฆฐ์€ ๋นŒ๋” ํŒจํ„ด์„ ํ•„์š”๋กœ ํ• ๊นŒ?