๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ํ™œ๋™/๐Ÿ“— ๋ฉด์ ‘์„ ์œ„ํ•œ CS ์ „๊ณต์ง€์‹ ๋…ธํŠธ

[๋งˆ๋ฌด๋ฆฌ] ํด๋ฆฐ์ฝ”๋“œ

by ๐Ÿ’พ๊ณ ๊ตฌ๋งˆ๋ง›ํƒ•๋จน๊ณ ์‹ถ๋‹ค 2024. 10. 16.
728x90

๐Ÿ‘ฉ‍๐Ÿ’ป ํด๋ฆฐ์ฝ”๋“œ

# ์ฝ๊ธฐ ์‰ฌ์šด ์ฝ”๋“œ๊ฐ€ ํด๋ฆฐ์ฝ”๋“œ์ด๋‹ค.

๋”๋ณด๊ธฐ

"I like my code to be elegant and efficient. The logic should be straightforward and make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and performance close to optimal so as not to tempt people to make the code messy with unprincipled optimizations. Clean code does one thing well."

Bjarne Stroustrup, inventor of ‘C++

1. ์ฝ”๋“œ๊ฐ€ ์šฐ์•„ํ•˜๋‹ค -> ๊ตฐ๋”๋”๊ธฐ ์—†์ด ๊น”๋”ํ•˜๋‹ค.

2. ํšจ๊ณผ์ ์ด๋‹ค -> ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ตœ๋Œ€ํ•œ ์ž‘์€ ๋ผ์ธ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

 

ํด๋ฆฐ์ฝ”๋“œ -> ์ง์ ‘์ ์ด๊ณ  ์—๋Ÿฌ ์ฒ˜๋ฆฌ๊ฐ€ ์ž˜ ๋˜์–ด ์žˆ์œผ๋ฉฐ ํ•˜๋‚˜์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

๋”๋ณด๊ธฐ

Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designers’ intent but rather is full of crisp abstractions and straightforward lines of control.”

Grady Booch, author of ‘Object-Oriented Analysis and Design with Applications’

1. ๋‹จ์ˆœํ•˜๊ณ  ์ง์ ‘์ ์ด๋‹ค.

2. ์›์ž‘์ž์˜ ์˜๋„๋ฅผ ์ˆจ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.

 

< Bjarne Stroustrup, Grady Booch์˜ ์˜๊ฒฌ ์ •๋ฆฌ >

ํด๋ฆฐ์ฝ”๋“œ์˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋Š” ๊ฐ€๋…์„ฑ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ๋ชจ๋“  ํŒ€์›์ด ์ดํ•ด(understandability)ํ•˜๊ธฐ ์‰ฝ๋„๋ก ์ž‘์„ฑ๋œ ์ฝ”๋“œ์ด๋‹ค.

 


Clean Code์˜ ์ฃผ์š”์›์น™(General)

๐Ÿค” ํŠน์ง•

  • ๊ฐ€๋…์„ฑ: ๋ˆ„๊ตฌ๋‚˜ ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹จ์ˆœ์„ฑ: ๋ณต์žกํ•œ ๋กœ์ง์€ ์ตœ๋Œ€ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ช…ํ™•ํ•œ ๋„ค์ด๋ฐ: ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค ๋“ฑ์˜ ์ด๋ฆ„์€ ๊ทธ ์—ญํ• ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ค‘๋ณต ์—†๋Š” ์ฝ”๋“œ: ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ค‘๋ณต์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹ , ๊ณตํ†ตํ™”ํ•˜์—ฌ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ž…๋‹ˆ๋‹ค.
  • ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅ์„ฑ: ์ฝ”๋“œ๋ฅผ ๋‹จ์œ„๋ณ„๋กœ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์กฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์˜ˆ์‹œ

class Library {
  constructor() {
    this.books = [];
  }

  addBook(book) {
    if (!book || !book.title || !book.author) {
      throw new Error('์ฑ…์˜ ์ œ๋ชฉ๊ณผ ์ €์ž๋Š” ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค.');
    }
    this.books.push(book);
  }

  getBooksByAuthor(author) {
    if (!author) {
      throw new Error('์ €์ž ์ด๋ฆ„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.');
    }
    return this.books.filter(book => book.author === author);
  }

  getAllBookTitles() {
    return this.books.map(book => book.title);
  }
}

const myLibrary = new Library();

myLibrary.addBook({ title: 'Clean Code', author: 'Robert C. Martin' });
myLibrary.addBook({ title: 'JavaScript: The Good Parts', author: 'Douglas Crockford' });


console.log(myLibrary.getBooksByAuthor('Robert C. Martin'));
console.log(myLibrary.getAllBookTitles()); 

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
// [{ title: 'Clean Code', author: 'Robert C. Martin' }]
// ['Clean Code', 'JavaScript: The Good Parts']

 

  • ๋ช…ํ™•ํ•œ ๋„ค์ด๋ฐ : ๋ฉ”์†Œ๋“œ ์ด๋ฆ„ addBook, getBooksByAuthor, getAllBookTitles๋Š” ๊ฐ๊ฐ ์ฑ…์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์ €์ž๋ณ„ ์ฑ… ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ค๋ฉฐ, ๋ชจ๋“  ์ฑ…์˜ ์ œ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ๋ช…ํ™•ํžˆ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • ์—๋Ÿฌ ์ฒ˜๋ฆฌ : addBook๊ณผ getBooksByAuthor ๋ฉ”์†Œ๋“œ๋Š” ํ•„์š”ํ•œ ์ž…๋ ฅ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ ๋ช…ํ™•ํ•œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๊ฐ€๋…์„ฑ : ํด๋ž˜์Šค์™€ ๋ฉ”์†Œ๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๋ฉฐ, ์—ญํ• ์ด ๋ถ„๋ช…ํžˆ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋‹ค. ๋ˆ„๊ฐ€ ์ฝ์–ด๋„ ์ด ์ฝ”๋“œ๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

์ฐธ๊ณ  : https://www.samsungsds.com/kr/insights/cleancode-0823.html

728x90