24
The Art of Readable Code 세세 세세 The Art of Readable Code..................................................1 Chapter 1. Code Should Be Easy to Understand..............................2 Part I. Surface-Level Improvements........................................4 Chapter 2. Packing Information into Names.................................4 Chapter 3. Names That Can’t Be Misconstrued...............................9 Chapter 4. Aesthetics (아아아아아 아아아)........................................12 Chapter 5. Knowing What to Comment.......................................19 Chapter 6. Making Comments Precise and Compact...........................26

The art of readable code _ Part I

  • Upload
    -

  • View
    504

  • Download
    0

Embed Size (px)

Citation preview

  • 1. The Art of Readable Code The Art of Readable Code ...................................................................................................................... 1Chapter 1. Code Should Be Easy to Understand ................................................................................... 2Part I. Surface-Level Improvements ....................................................................................................... 4Chapter 2. Packing Information into Names ........................................................................................... 4Chapter 3. Names That Cant Be Misconstrued ..................................................................................... 9Chapter 4. Aesthetics ( ).......................................................................................... 12Chapter 5. Knowing What to Comment ................................................................................................ 20Chapter 6. Making Comments Precise and Compact ........................................................................... 27

2. Chapter 1. Code Should Be Easy to Understand . compact , . ? . . Code should be written to minimize the time it would take for someone else to understand it. . ? , . , . , . , . , . . . .assert((!(bucket = FindBucket(key))) || !bucket->IsOccupied()); ? ? ?bucket = FindBucket(key);if (bucket != NULL) assert(!bucket->IsOccupied()); . 3 3 . , . 3. ."So even though having fewer lines of code is a good goal, minimizing the time-till-understanding is an evenbetter goal." readable . . , . ? . ? . . . , . . , . 4. Part I. Surface-Level Improvements , , . refactoring , . . . , .Chapter 2. Packing Information into Names comment . , , comment . ? 1. Choosing Specific Words., , . , . .BinaryTree.Size(); Size ? . . , tree depth , node , . , . BinaryTree.getHeigh() , BinaryTree.getNumNodes() . 2. Finding More Colorful Words. . . make .create, set up, build, generate, compose, add, new , . . 3. Avoid Generic Names Like tmp and retval . tmp, retval . 5. . .if (right < left) {tmp = right; right = left; left = tmp;} ?String tmp = user.name();tmp += " " + user.phone_number();tmp += " " + user.email();...template.set("user_info", tmp); tmp user_info tmp userInfo . , . tmp , , . , useInfo , userInfo . loop .for (inti = 0; i m;lookUpNamesNumbers(&m);print(m);} m . LookUpManager.m; . ? LookUpManager m . . . BEPManager . ? . BackEndProcessManager ? . . . . , ,readDocument readDoc . 8. , , . 7. Use Name Formatting to Convey Meaning . m_pageHeight m_ , . . (_), . . , . 9. Chapter 3. Names That Cant Be Misconstrued , . ? . , .. Filter .Results = Database.all_objects.filter(year = CART_TOO_BIG_LIMIT: Error(Too many items in cart) ? , ? 10 ? ? CART_TOO_BIG_LIMIT . , . , , . .MAX_ITEMS_IN_CART = 10if shopping_cart.num_items() > MAX_ITEMS_IN_CART: Error(Too many items in cart) MAX MIN . . , , . 10. first, last . , . , begin/end . end exclusive . .substring(intbeginIndex, intendIndex) end index .Naming Booleans , Boolean . booleanread_password = true; , ? , , . . . . . . need_password user_is_authenticated ? ? . , is, has, can should . , not . not not , . ? .Diable_ssl = false;use_ssl = true; 2 ssl , ? Not ? .Matching Expectations of Users . begin, end . . . . . get . get / , . getMean , 10 . , 11. (?) getMean , . . , size . size . O(1) . , O(n2) . ? , . , . , . . . , . . Is_overwrite Boolean . Is_overwrite = true; ? ? overwrite overwrite. ? can_overwrite = false; ? . is_overwrite , , . , . . 12. Chapter 4. Aesthetics ( ) . , . . . ? .? ? , . . .Rearrange Line Breaks to Be Consistent and Compact 80 , . . . 13. ? ? . . . . , ? . . 14. , . ? ? . .Use Methods to Clean Up Irregularity . . . ^^ ExpandFullName . . ExpandFullName(database_connection, ) . . . . ? CheckFullName , . 15. refactoring . . . , , . .Use Column Alignment When Helpful , . , column . . ? . .? ? ? . . . 16. ? ? . . , . ? ? . ? Phone request r ? , . . , diff . . . , diff . . . .Pick a Meaningful Order, and Use It Consistently . . request.POST.get . . . ? . . , , . . . 17. Organize Declarations into Blocks , . . ? , . . . ? , . , . , ? , . 18. Break Code into Paragraphs , , . . ! . , . ~ . .Summary . codingconvention , . . 19. , . . . 20. Chapter 5. Knowing What to Comment . , ? . . , . . . , . .Knowing what not to comment . . . . ? ? . . . . ? . ? . , . 21. . , , . , , . . . . getter/setter . , . . . . .? ? ? . ? . ? , . , . . . 22. , . . . , ! . . , .. Self-documenting . . . . . handle . ? ?? . self-documenting .Recording your thoughts . . , . . insight . . . insight . insight . . ? , . 100% 23. . . , , . ( ) . . . . . . , . . . . , . . , . . . . . . ? , ? 8 . . , . , NUM_THREADS , , . . , 24. . . . SECONDS_PER_DAY , . . . ^^Put Yourself in the Readers Shoes , . . . ? clear swap ? . , . , . . . . , , , 1 timeout . . , , . , . , 25. . . , . big picture . , . , big picture , . . , . . . , . . . , . . . , . . 26. . . , . . . , ^^Summary , , . . . insight , good code > bad code + good comments. .Good code . 27. Chapter 6. Making Comments Precise and Compact . , . . , . . .// int CategoryType.// pair float score. Weight.Typedefhash_map>ScoreMap; ? , .// : CategoryType, Typedefhash_map>ScoreMap; . , . . . , , . . , . : , . : , . , . : URL . : URL . . 28. : . : n . , . . , . . producst.sort(CompareProductByPrice);for(list::reverse_iterator it = products.rbegin(); it != products.rend(); ++it) {DisplayPrice(it -> Price);} , . , . . . Connect(10, false, 5); primitive . . , . . Connect( /* timeout_ms= */ 10, /* use_encryption= */ false, /* max_retry_times= */ 5); . ., . . ^^;; , . , , . , . 29. . . . , . . . . unhappy.substring(2) = happyunhappy.substring(7) = , . .