Clean Code – Chapter 6 Objects and Data Structures
Data Abstraction
Hiding implementation
Data/Object Anti-Symmetry
Objects hide their data behind abstractions and expose function that operate on that data. Data structure expose their data and hava no meaningful functions.
Procedural code(code using data structure) 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 functions must change. OO code makes it hard to add new functions because all the classes must change.
The Law of Demeter
A module should not know about the innards of the objects it manipulates.
A method f of a class C should only call the methods of these:
- C
- An object created by f
- An object passed as an argument to f
- An object held in an instance variable of C
推荐一篇博文:笛米特法则详解,通过一个简单的代码示例解释了上述概念。
Train Wrecks
Bad code:
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
Or
Options opts = ctxt.getOptions();
File scratchDir = opts.getScratchDir();
final String outputDir = scratchDir.getAbsolutePath();Only used for data structures
final String outputDir = ctxt.options.scratchDir.absolutePath;
Hybrids
Avoid hybrid structures that are half object and half data structure.
Hiding Structure
Tell an object to do something, not ask it about its internals.
Data Transfer Objects (DTO)
The quintessential form of a data structure is a class with public variables and no functions.
Active Record
Special form of DTO. Hava navigational methods like
save
andfind
.Typically direct translations from database tables, or other data sources.
Clean Code – Chapter 6 Objects and Data Structures的更多相关文章
- Objects and Data Structures
Date Abstraction Hiding implementation is not just a matter of putting a layer of fucntions between ...
- Clean Code – Chapter 3: Functions
Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...
- Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...
- Clean Code – Chapter 4: Comments
“Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...
- Clean Code – Chapter 2: Meaningful Names
Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...
- Clean Code – Chapter 5 Formatting
The Purpose of Formatting Code formatting is about communication, and communication is the professio ...
- 代码整洁之道Clean Code笔记
@ 目录 第 1 章 Clean Code 整洁代码(3星) ?为什么要整洁的代码 ?什么叫做整洁代码 第 2 章 Meaningful Names 有意义的命名(3星) 第 3 章 Function ...
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
- (转) Data structures
Data structures A data structure is a group of data elements grouped together under one name. Thes ...
随机推荐
- SQL函数说明大全 (转)
一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...
- hdu 2594 Simpsons’ Hidden Talents KMP应用
Simpsons’ Hidden Talents Problem Description Write a program that, when given strings s1 and s2, fin ...
- Hadoop常见的45个问题解答
(大讲台:国内首个it在线教育混合式自适应学习) 1.Hadoop集群可以运行的3个模式 单机(本地)模式 伪分布式模式 全分布式模式 2. 单机(本地)模式中的注意点? 在单机模式(standal ...
- 【面试题】百度糯米java工程师面试
面试经历: 技术题问的比较基础的java知识,有个编程题设计团购秒杀方面的设计,之前没有这种经验做的不好,做完题一个技术经理过来面试,主要问了一下之前做的什么项目,对struts,spring的原理做 ...
- 一步步学习ASP.NET MVC3 (15)——过滤器
请注明转载地址:http://www.cnblogs.com/arhat 今天老魏和大家一起讨论一下ASP.NET MVC中非常重要的一个知识:"过滤器".那么这个"过滤 ...
- sencha touch
download http://www.sencha.com/products/touch/thank-you/ Developer Center http://developer.sencha.co ...
- Oracle本地,远程,分布式登录
identify认证,确定; identity同一性,个性; 本地连接 sqlplus scott/tiger@localhost:1521/orcl 这句话就等于sqlplus scott/tige ...
- Codeforces Round #326 div2
Problem_A(588A): 题意: Duff 很喜欢吃肉, 每天都要吃,然而她又懒得下楼. 可以买很多放在家里慢慢吃.然而肉价每天都在变化,现给定一个n, 表示有多少天,然后第i天吃ai kg的 ...
- 精确到秒的JQuery日期控件
项目中需要用到精确到秒的日期控件,到网上搜了一下,发现有一个JQuery控件可以实现该功能---TimerPicker.但是官网上没有提供该控件的完整Demo,而且没有提供汉化包,所以自己汉化了一下, ...
- Git的一些基本概念
Git的一些基本概念 设置自己的用户名和邮箱git config –global user.name "Your Name"git config –global user.emai ...