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
saveandfind.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 ...
随机推荐
- mysql存储过程讲解
1.数据库存储过程:简单滴说,存储过程就是存储在数据库中的一个程序. 2..数据库存储过程作用: 第一:存储过程因为SQL语句已经预编绎过了,因此运行的速度比较快. 第二:存储过程可以接受参数.输出参 ...
- Oracle 分析函数之聚集函数(MAX、MIN、AVG和SUM)
MAX 查找组中表达式的最大值 MAX(COL ) OVER ( [ <partition_by_clause> ] < order_by_clause > )MIN 查找组中 ...
- IIS修改队列长度
Internet Information Services (IIS) 限制了在任何给定时间可在队列中等待的应用程序池请求的最大数量.如果达到此限制,则所有新请求都将被拒绝,而且用户将收到错误消息“5 ...
- DB天气app冲刺第一天
今天算是正式的第一天开始着手做这个app了,前两天作的是嵌入式的大作业,看着书上的例子做了一个小游戏.基本也算完成了作业.主要是为了练手,熟悉android的开发流程.基本明白了.以后好上手了. 今天 ...
- PHP与最丑的后台管理系统
第二天阿Q到公司还是比较早,同事只有阿梅在,阿Q坐在椅子上旋转来旋转去,有点像个小孩子.公司有书柜,书柜上放了好几本很新的php的书,.net的书反倒比较少而且显得老旧.阿Q起身走过去拿了本php翻了 ...
- 关于移动端和PC端的交互的区别
对于现在的移动端设备的普及,移动端上的用户体验成了一个重要的关注点. 看了一些网上的关于移动端的交互和用户体验的知识,这里总结了一些.若有不足的地方,希望大家能够积极补充. PC端和移动端的产品的设计 ...
- JavaScript中===与==的区别
参考网址:http://zhidao.baidu.com/link?url=RbWnRUHKPBTakwm2-iRfEDicrh4ZSAAeVfq2WvBkvfeLPkU-ZSqZ4chV8LWkCk ...
- [转载]C#读写配置文件(XML文件)
.xml文件格式如下 [xhtml] view plaincopy <?xml version="1.0" encoding="utf-8"?> & ...
- apk签名《转》
出处!:http://jeff-pluto-1874.iteye.com/blog/847366 我觉得写的不错就转载了. 一.Android Apk签名Apk签名首先要有一个keystore的签名用 ...
- HDU 1400 (POJ 2411 ZOJ 1100)Mondriaan's Dream(DP + 状态压缩)
Mondriaan's Dream Problem Description Squares and rectangles fascinated the famous Dutch painter Pie ...