TURI BLOG

[Nomad Coders Challenge] Clean Code - Assignment #13 ๋ณธ๋ฌธ

Nomad Coders

[Nomad Coders Challenge] Clean Code - Assignment #13

TURI BLOG 2024. 12. 15. 07:56

 

๐Ÿ“ It is part of a book club challenge on a programming learning website called Nomad Coders. The challenge is to demonstrate what I have learned after reading Clean Code by Robert C. Martin.

 

๐Ÿ“ It will consist of the following sections over three weeks: the range of reading, the top quotes from the book, my review, and remaining questions. 

  ๐Ÿ”—Challenge Schedule


๐Ÿ’ป I plan to read Clean Code in JavaScript and Python, referring to the GitHub repositories shared by Nomad Coders with the challengers.

 

Assignment #13

  • ๐Ÿ“š Chapter 10. Classes (Jeff Langr)
  • โœ”๏ธ TIL 
    • Top quotes from the book
    • Case study in Python
    • Remaining questions

 

๐Ÿ“ Top quotes from the book

  • ํด๋ž˜์Šค๋Š” ์ž‘์•„์•ผ ํ•œ๋‹ค.
    • ํด๋ž˜์Šค๋ฅผ ์„ค๊ณ„ํ•  ๋•Œ๋„, ํ•จ์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ '์ž‘๊ฒŒ' - ํด๋ž˜์Šค๊ฐ€ ๋งก์€ ์ฑ…์ž„์ด ์ ๊ฒŒ
    • ํด๋ž˜์Šค ์„ค๋ช… - if, and, or, but ์‚ฌ์šฉ ์—†์ด, 25๋‹จ์–ด ๋‚ด์™ธ๋กœ ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค. 
    • Single Responsibility Principle(๋‹จ์ผ ์ฑ…์ž„ ์›์น™, SRP): ํด๋ž˜์Šค๋‚˜ ๋ชจ๋“ˆ์„ ๋ณ€๊ฒฝํ•  ์ด์œ ๊ฐ€ ๋‹จ ํ•˜๋‚˜๋ฟ์ด์–ด์•ผ ํ•œ๋‹ค.
      • Robert C. Martin writes: A class should have only one reason to change.
      • ์ž‘์€ ํด๋ž˜์Šค๊ฐ€ ๋งŽ์€ ์‹œ์Šคํ…œ์ด๋“  ํฐ ํด๋ž˜์Šค๊ฐ€ ๋ช‡ ๊ฐœ๋ฟ์ธ ์‹œ์Šคํ…œ์ด๋“  ๋Œ์•„๊ฐ€๋Š” ๋ถ€ํ’ˆ์€ ๊ทธ ์ˆ˜๊ฐ€ ๋น„์Šทํ•˜๋‹ค. ์–ด๋Š ์‹œ์Šคํ…œ์ด๋“  ์ตํž ๋‚ด์šฉ์€ ๊ทธ ์–‘์ด ๋น„์Šทํ•˜๋‹ค. "๋„๊ตฌ ์ƒ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ์€๊ฐ€? ์ž‘์€ ์„œ๋ž์„ ๋งŽ์ด ๋‘๊ณ  ๊ธฐ๋Šฅ๊ณผ ์ด๋ฆ„์ด ๋ช…ํ™•ํ•œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚˜๋ˆ  ๋„ฃ๊ณ  ์‹ถ์€๊ฐ€? ์•„๋‹ˆ๋ฉด ํฐ ์„œ๋ž ๋ช‡ ๊ฐœ๋ฅผ ๋‘๊ณ  ๋ชจ๋‘๋ฅผ ๋˜์ ธ ๋„ฃ๊ณ  ์‹ถ์€๊ฐ€?"
    • Cohension(์‘์ง‘๋„): ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ์ˆ˜๊ฐ€ ์ž‘์•„์•ผ ํ•œ๋‹ค. ๋ฉ”์„œ๋“œ ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์„ ์ˆ˜๋ก ๋ฉ”์„œ๋“œ์™€ ํด๋ž˜์Šค์˜ ์‘์ง‘๋„๊ฐ€ ๋†’๋‹ค. ์‘์ง‘๋„๊ฐ€ ๋†’์€ ํด๋ž˜์Šค๋Š” ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ์‘์ง‘๋„๊ฐ€ ๋†’์€ ํด๋ž˜์Šค๋ฅผ ์„ ํ˜ธํ•œ๋‹ค. ํด๋ž˜์Šค์˜ ์†ํ•œ ๋ฉ”์„œ๋“œ์™€ ๋ณ€์ˆ˜๊ฐ€ ์„œ๋กœ ์˜์กดํ•˜๋ฉฐ ๋…ผ๋ฆฌ์ ์ธ ๋‹จ์œ„๋กœ ๋ฌถ์ด๊ธฐ ๋•Œ๋ฌธ. 'ํ•จ์ˆ˜๋ฅผ ์ž‘๊ฒŒ, ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก์„ ์งง๊ฒŒ' -> ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์•„์ง„๋‹ค. -> ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋กœ ์ชผ๊ฐœ์•ผ ํ•œ๋‹ค๋Š” ์‹ ํ˜ธ๋‹ค. 
    • OCP(Open-Closed Principle) - ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์—์„œ ํ•ต์‹ฌ ์›์น™ ์ค‘ ํ•˜๋‚˜. ํด๋ž˜์Šค๋Š” ํ™•์žฅ์— ๊ฐœ๋ฐฉ์ ์ด๊ณ  ์ˆ˜์ •์— ํ์‡„์ ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์›์น™
      • “Incorporate new features by extending the system, not by making modifications (to it)”, Uncle Bob.  
      • ๊นจ๋—ํ•œ ์‹œ์Šคํ…œ์€ ํด๋ž˜์Šค๋ฅผ ์ฒด๊ณ„์ ์œผ๋กœ ์ •๋ฆฌํ•ด ๋ณ€๊ฒฝ์— ์ˆ˜๋ฐ˜ํ•˜๋Š” ์œ„ํ—˜์„ ๋‚ฎ์ถ˜๋‹ค.
        ์œ„ ์ฝ”๋“œ๋Š”, ์ƒˆ๋กœ์šด SQL๋ฌธ์„ ์ง€์›ํ•˜ํ•˜๊ฑฐ๋‚˜ ๊ธฐ๋ณธ SQL๋ฌธ ํ•˜๋‚˜๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ sql ํด๋ž˜์Šค๋ฅผ ๊ณ ์ณ์•ผ ํ•˜๋ฏ€๋กœ, ๋ณ€๊ฒฝํ•  ์ด์œ ๊ฐ€ ๋‘ ๊ฐ€์ง€ -> SRP( Single Responsibility Principle, ๋‹จ์ผ์ฑ…์ž„์›์น™)์„ ์œ„๋ฐ˜ํ•œ๋‹ค. 
        - ๊ณต๊ฐœ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ -> sql ํด๋ž˜์Šค์—์„œ ํŒŒ์ƒํ•˜๋Š” ํด๋ž˜์Šค๋กœ ๋ณ€๊ฒฝ
        - ๋น„๊ณต๊ฐœ ๋ฉ”์„œ๋“œ -> ํ•ด๋‹น ํŒŒ์ƒ ํด๋ž˜์Šค๋กœ ์ด๋™- ๋ชจ๋“  ํŒŒ์ƒ ํด๋ž˜์Šค์— ๊ณตํ†ต๋˜๋Š” ๋น„๊ณต๊ฐœ ๋ฉ”์„œ๋“œ -> ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค๋กœ ์ด๋™ 
      • ํ•จ์ˆ˜ ํ•˜๋‚˜๋ฅผ ์ˆ˜์ •ํ•˜๋”๋ผ๋„, ๋‹ค๋ฅธ ํ•จ์ˆ˜๊ฐ€ ๋ง๊ฐ€์งˆ ์œ„ํ—˜์ด ์‚ฌ๋ผ์ง. ํด๋ž˜์Šค๊ฐ€ ์„œ๋กœ ๋ถ„๋ฆฌ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ. 
    • Dependency Inversion Principle(DIP): ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ธํ•œ ๊ตฌํ˜„์ด ์•„๋‹ˆ๋ผ ์ถ”์ƒํ™”์— ์˜์กดํ•ด์•ผ ํ•œ๋‹ค
      • ์ถ”์ƒํ™”๋กœ - ์‹ค์ œ๋กœ ์ฃผ๊ฐ€๋ฅผ ์–ป์–ด์˜ค๋Š” ์ถœ์ฒ˜, ์–ป์–ด์˜ค๋Š” ๋ฐฉ์‹ ๋“ฑ์˜ ๊ตฌ์ฒด์ ์ธ ์‚ฌ์‹ค์„ ์ˆจ๊ธด๋‹ค.  
      • ์ตœ์†Œ ๊ฒฐํ•ฉ๋„: ๊ฐ ์‹œ์Šคํ…œ ์š”์†Œ๊ฐ€ ๋‹ค๋ฅธ ์š”์†Œ๋กœ๋ถ€ํ„ฐ, ๋ณ€๊ฒฝ์œผ๋กœ๋ถ€ํ„ฐ ์ž˜ ๊ฒฉ๋ฆฌ๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ. ์ž˜ ๊ฒฉ๋ฆฌ๋ ์ˆ˜๋ก ์š”์†Œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋‹ค. 

 

๐Ÿ•ต  Case study in Python based on @zedr 's Clean Code Python GitHub repository

    • Single Responsibility Principle (SRP)

 

 

 

๐Ÿ” Remaining questions

  • OCP(Open-Closed Principle) - ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์—์„œ ํ•ต์‹ฌ ์›์น™ ์ค‘ ํ•˜๋‚˜. ํด๋ž˜์Šค๋Š” ํ™•์žฅ์— ๊ฐœ๋ฐฉ์ ์ด๊ณ  ์ˆ˜์ •์— ํ์‡„์ ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์›์น™

 

Comments