Community

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

← Go back
TIL: 6. Objects and Data Structures
#clean_code
2년 전
528

TIL (Today I Learned)

2022.03.01

오늘 읽은 범위

  1. Objects and Data Structures

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

  • There is a reason that we keep our variables private.

Data Abstraction (p.93)

Data/Object Anti-Symmetry (p.95)

  • Objects hid their data behind abstractions and expose functions that operate on that data. Data structure expose their data and have no meaningful functions.

  • Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. OO code, on the other hand, makes it easy to add new classes without changing existing functions.

  • Procedural code makes it hard to add new data structures because all the functions must change. OO code makes it hard to add new functions because all the classes must change.

  • In any complex system there are going to be times when we want to add new data types rather than new functions. For these cases objects and OO are most appropriate. On the other hand, there will also be times when we'll want to add new functions as opposed to data types. In that case procedural code and data structures will be more appropriate.

The Law of Demeter (p.97)

  • There is a well-known heuristic called the Law of Demeter that says a module should not know about the innards of the objects it manipulates.

Train Wrecks (p.98)

  • Train wreck: chains of calls are considered to be sloppy style and should be avoided. It is usually best to split them up.
    Before:
    final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
    After:
    Options opts = ctxt.getOptions();
    File scratchDir = opts.getScratchDir();
    final String outputDir = scratchDir.getAbsolutePath();

Data Transfer Objects (p.100)

  • The quintessential from of a data structure is a class with public variables and no functions. This is sometimes called a data transfer object, or DTO.

Conclusion (p.101)

  • Objects expose behavior and hide data. This makes it easy to add new kinds of objects without changing existing behaviors. It also makes it hard to add new behaviors to existing objects.

  • Data structures expose data and have no significant behavior. This makes it easy to add new behaviors to existing data structures but makes it hard to add new data structures to existing functions.

  • In any give systems we will sometimes want the flexibility to add new data types, so we prefer objects for that part of the system. Other times we will want the flexibility to add new behaviors, and so in that part of the system we prefer data types and procedures. Good software developers understand these issues without prejudice and choose the approach that is best for job at hand.

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

  • It would be more intuitive for authors to have examples using other languages. I've never used Java before (I'm pretty sure lots of people are in same position as me), so it's quite hard for me to feel differences directly.

  • What I learned today was to be a flexible programmer!

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