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 ...
随机推荐
- 11个有用的Linux命令
Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.今天为你解释下面几个命令:sudo.python.mtr.Ctrl+x+e.nl.s ...
- RHEL 6.1字符界面无法登录SSH却能登录
1.具体版本: 2.具体现象: 每次输入用户名密码登录之后又跳到这个界面.但是用ssh却可以登录. 3.查看日志 [root@localhost ~]# tail -f /var/log/secure ...
- expdp ORA-39213
[oracle@BI2 dir_dp]$ impdp ruijie_kettle/ruijie_kettle schemas=ruijie_kettle directory=dir_dp dumpfi ...
- Sublime Text 2 插件
一直以来写代码都是用的EditPlus,也尝试了一段时间学习Vim这神器,后来因为使用不习惯还是改回了原来的EditPlus.前几天朋友想我推荐了Sublime Text 2,喜欢尝鲜我的肯定是不会放 ...
- 使用Python编程语言连接MySQL数据库代码
使用Python编程语言连接MySQL数据库代码,跟大家分享一下: 前几天我用python操作了mysql的数据库,发现非常的有趣,而且python操作mysql的方法非常的简单和快速,所以我把代码分 ...
- VM启动报错:Failed to lock the file
http://www.cnblogs.com/kristain/articles/2491966.html Reason: Failed to lock the fileGoogle 了一下, 在網路 ...
- ibdata1是?
MySQL使用InnoDB引擎的时候,ibdata1这个文件会随着时间的增长,会变得越来越大,占据大量的磁盘空间. 那么,ibdata1里保存了哪些东西,为什么会变得越来越大呢,让我们开看看ibdat ...
- vm安装ubuntu桥接模式无法联网
桥接模式,就是和主机不同的ip,其他都是一样的. 编辑网络连接 查看自己机子的ip ipconfig 一般情况下是 ip 192.168.1.XXX 子网掩码 255.255.255.0 网关 192 ...
- asp.net推送
http://tech.it168.com/a2012/0210/1310/000001310252_all.shtml http://www.infoq.com/cn/news/2012/09/rc ...
- Python中通过Image的open之后,去show结果打不开bmp图片,无法正常显示图片
在windows的cmd命令行下,使用Python的PIL库打开并显示一个jpg图片: ? 1 2 3 openedImg = Image.open(saveToFile); print " ...