ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

ios-og


๐Ÿ“ฑ Functions(ํ•จ์ˆ˜) vs Computed Property(์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ)


๐ŸŽฏ ๋ฌธ์ œ ์ •์˜ํ•˜๊ธฐ


  • ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ์ •์˜ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ‹ฐ์ผ“์„ ๋ฝ‘์•„์ฃผ๋Š” ๋ฐœ๊ถŒ๊ธฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•  ๋•Œ ๋ฐœ๊ถŒ๊ธฐ๋Š” ํ˜„์žฌ ์ƒํƒœ์˜ ํ‹ฐ์ผ“ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ–๊ณ , ๋ˆ„๊ตฐ๊ฐ€ ํ‹ฐ์ผ“์„ ๋ฝ‘๋Š”๋‹ค๋ฉด ํ‹ฐ์ผ“ ๋ฒˆํ˜ธ๋ฅผ ํ•˜๋‚˜ ๋†’์—ฌ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด๋ณด๋ฉด

    struct ticketGenerator {
      var ticketNumber: Int
    }

    ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ฒƒ์€ ๋ฐœ๊ถŒ์‹œ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๋†’์—ฌ๊ฐ€๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ๋ฉ”์†Œ๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด

    struct ticketGenerator {
      var ticketNumber: Int
    
      func raiseTicketNumber() -> Void {
        ticketNumber += 1
      }
    }

    ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ด๊ฒƒ์€ ticketNumber์˜ ์ƒํƒœ๋งŒ ๋ณ€ํ™”์‹œ์ผœ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์˜ ์ˆซ์ž์—์„œ 1์„ ๋”ํ•œ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ •์˜ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ๋กœ ์ •์˜ํ•˜๋ฉด

    struct ticketGenerator {
      var ticketNumber: Int {
        ticketNumber += 1
        return ticketNumber
      }
    }

    ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋Šฅ์ƒ์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ ์–ธ์ œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์–ธ์ œ ์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ• ๊นŒ์š”?


๐Ÿ“ Function, Computed Property ์ฐจ์ด ํŒŒ์•…ํ•˜๊ธฐ


  • ๋ธ”๋กœ๊ทธ ๊ธ€์„ ํ†ตํ•ด ๊ทธ ์ฐจ์ด๋ฅผ ํŒŒ์•…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

    If any call is considered to be expensive, can throw an error, or returns different results when invoked multiple times โ€” a function is preferred. If a call is cheap, does not throw errors, returns the same result or caches the result of its first invocation โ€” a computed property will probably suit your needs.

    ์œ„์˜ ๊ธ€์„ ํ•ด์„ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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

    ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์˜ ๊ฒฝ์šฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ถ”์ฒœ๋˜๊ณ  ๊ทธ ์™ธ์—๋Š” ์—ฐ์‚ฐ ํ”„๋กœํผํ‹ฐ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ถ”์ฒœ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ตฌํ˜„๋˜๋Š” ํ•จ์ˆ˜์˜ ๋กœ์ง์ด ๋ณต์žกํ•  ๋•Œ
  2. ํ˜ธ์ถœ์ด error๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๋•Œ
  3. ๋งค ์ž‘์—…๋งˆ๋‹ค ๋‹ค๋ฅธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ(ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด ๋ณ€ํ•  ๋•Œ)

๐Ÿ”ซ ์˜ˆ์‹œ์— ๊ฐœ๋… ์ ์šฉ


  • ์œ„์˜ ๊ธฐ์ค€์œผ๋กœ ํ‹ฐ์ผ“ ๋ฐœ๊ถŒ๊ธฐ๋ฅผ ๋‹ค์‹œ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    1๋ฒˆ ๊ธฐ์ค€ -> (X) ๋‹จ์ˆœํžˆ property๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋ณต์žกํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.

    2๋ฒˆ ๊ธฐ์ค€ -> (X) ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

    3๋ฒˆ ๊ธฐ์ค€ -> (O) ๋งค ์ž‘์—…๋งˆ๋‹ค 1์”ฉ ์ฆ๊ฐ€ํ•œ ์ƒˆ๋กœ์šด ๊ฐ’์„ ํ‹ฐ์ผ“ ๋ฒˆํ˜ธ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

    3๋ฒˆ ๊ธฐ์ค€์— ์ถฉ์กฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ๊ถŒ๊ธฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ตฌํ˜„์€ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋” ์ ํ•ฉํ•ด๋ณด์ž…๋‹ˆ๋‹ค.


ref: https://medium.com/swift-india/functions-vs-computed-property-what-to-use-64bbe2df3916#:~:text=If%20a%20call%20is%20cheap,will%20probably%20suit%20your%20needs.&text=A%20property%20expresses%20an%20inherent,parameters%3B%20properties%20don't.

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๋ฐ˜์‘ํ˜•
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG more
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ