Community

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

← Go back
TIL: 10. Classes
#clean_code
2년 전
821

오늘 TIL 3줄 요약

  • Classes should be small.

  • Classes should have one reason to change.

  • Classes should be isolated enough from changes.

TIL (Today I Learned) 날짜

  • 2022.03.08

오늘 읽은 범위

  1. Classes

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

Class Organization (p.136)

  • Public static constants, if any, should come first. Then private static variables, followed by private instance variables.

Encapsulation (p.136)

Classes Should Be Small! (p.136)

  • The first rule of classes is that they should be small. The second rule of classes is that they should be smaller than that.

  • With classes we use a different measure. We count responsibilities.

  • The name of a class should describe what responsibilities it fulfills.

The Single Responsibility Principle (p.138)

  • The Single Responsibility Principle (SRP) states that a class or module should have one, and only one, reason to change.

  • Classes should have one responsibility - one reason to change.

  • SRP is one of the more important concept in OO design.

  • Getting software to work and making software clean are two different activities.

  • Most of us have limited room in our heads, so we focus on getting our code to work more than organization and cleanliness.

  • The problem is that too many of us think that we are done once the program works.

  • We move on to the next problem rather than going back and breaking the overstuffed classes into decoupled units with single responsibilities.

  • However, a system with many small classes has no more moving parts than a system with a few large classes.

  • Do you want your tools organized into toolboxes with many small drawers each containing well-defined and well-labeled components? Or do you want a few drawers that you just toss everything into?

  • A system with larger, multipurpose classes always hampers us by insisting we wade through lots of things we don't need to know right now.

  • We want our systems to be composed of many small classes, not a few large ones.

  • Each small class encapsulates a single responsibility, has a single reason to change, and collaborates with a few others to achieve the desired system behaviors.

Cohesion (p.140)

  • In general the more variables a method manipulates the more cohesive that method is to its class.

  • You should try to separate the variables and methods into two or more classes such that the new classes are more cohesive.

Maintaining Cohesion Results in Many Small Classes (p.141)

  • When classes lose cohesion, split them!

  • So breaking a large function into many smaller functions often gives us the opportunity to split several smaller classes out as well. This gives our program a much better organization and a more transparent structure.

  • There are several reasons for the growth of refactored programs.
    First, the refactored program uses longer, more descriptive variable names.
    Second, the refactored program uses function and class declarations as a way to add commentary to the code.
    Third, we used whitespace and formatting techniques to keep the program readable.

Organizing for Change (p.147)

  • For most systems, change is continual.

  • Every change subjects us to the risk that the remainder for the system no longer works as intended.

  • In a clean system we organize our classes so as to reduce the risk of change.

  • We want to structure our systems so that we muck with as little as possible when we update them with new or changed features.

  • In an ideal system, we incorporate new features by extending the system, not by making modifications to existing code.

Isolating from Change (p.149)

  • Needs will change, therefore code will change.

  • The lack of coupling means that the elements of our system are better isolated from each other and from change.

  • This isolation makes it easier to understand each element of the system.

  • By minimizing coupling in this way, our classes adhere to another class design principle known as the Dependency Inversion Principle (DIP).

  • The DIP says that our classes should depend upon abstractions, not on concrete details.

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

  • Classes should be small and isolated.

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

  • Responsibility?

  • Cohesion?

오늘 읽은 다른사람의 TIL

  • I have not read others' TIL.