Community

개발자 99% 커뮤니티에서 수다 떨어요!

← Go back
Clean code#3 함수
#clean_code
2년 전
443


TIL (Today I Learned)

오늘 읽은 범위

3장. 깨끗한 코드

책에서 기억하고 싶은 내용을 써보세요.

  • 코드는 위에서 아래로 이야기처럼 읽혀야 좋다 - 내려 가기 규칙

    (Reading code from top to bottom - The step down rule)

  • 부수 효과를 일으키지 마라
    (Have no side effects, side effects are lies)

오늘 읽은 소감은? 떠오르는 생각을 가볍게 적어보세요

코드를 작성하는것을 이야기를 쓰는 것과 같은 맥락으로 소개한다

함수의 추상화를 조절해 작은것은 작은것끼리 큰것은 큰것 끼리 모으고 분류하길 권한다

그 구체적인 방법으로는 함수를 여러개의 작은 함수로 추출하고 여러 기능을 하는 함수를

각 기능별로 분리하기를, 함수의 인자도 줄이거나 없애기를 강권한다

특히 기능을 분리하는 것을 강조하는데 인자의 질문을 던지는 조회와

인자를 가공하는 변형을 같이 쓰지말기를 요청한다

나도 코드를 쓰다보면서 자연스럽게 했던 일들이다

내가 필요하다고 느낄때 필요한것을 확인하고 그 아래줄에서 내가 필요한것을 수행하는 코드를 작성했고, 큰 가지를 쓰다가도 생각나면 세부사항을 확인하곤 했다

마치 돌, 모래, 자갈, 흙, 흰색, 검은색이 뒤섞여있는 코드였다

저자는 코드들을 크기별로 나누고 색깔별로 나누기를 요청한다

그저 코드가 돌아가는것을 요청하는 것이 아니라 한편의 정리된 글을 쓰는 느낌으로

For Nico:

Author consider coding a writing story

He recommend being careful about abstraction levels, gather same size and classify by their size

For action steps, extract small functions in huge function , separate function by their different feature at composed function and reduce parameter even if remove it

He emphasize the splitting composed-feature function to functions which has only one feature and one purpose

For instance exhibit to make function which validate something about parameter as transform it

This is what I did often, My code do something I want, immediately after I checked some attribute in same function

I usually checked some detail, when I draw some rough concept

My code looks like gross mixture some dirt, stones, pebbles, sand and white, brown, black color stuff all together

The author says classify and arrange them all the way

Making just piece of running shit to clean and sleek art

궁금한 내용이 있거나, 잘 이해되지 않는 내용이 있다면 적어보세요.

  • 스위치문을 한번만 사용해라 - 다형성 객체를 이용해 분리해라


    They can be tolerated if they appear only once, are used to create polymorphic obejcts

    스위치문이 코드를 지저분하게 만들고 때로는 의미없는 맥락을 넣어 흐름을 끊기는 하지만 필요할 때가 있다


    다형성 객체를 사용하라고 저자는 말하고 있지만 interface class가 없는 swift에서는 protocol과 generic을 사용해야 할것같다


    스위치문을 줄이려고 이렇게까지 해야 할까 싶지만 그대로 놔두면 온갖 장소에 스위치문이 끼어들어 코드를 읽기 어렵게 한다
    깔끔하게 처리할수는 없을까?

    For Nico:
    Author says use switch statement only once and by using it make some polymorphism class and put it under the hood


    but making some protocol and generics ( in Swift language their is no interface class ) is big thing as is
    It look like too heavy tool for just reducing switch statement


    but if I don't touch them It goes growing and take all the place in my code and make hard to reading


    Is there any way making them neat and clean?