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 ...
随机推荐
- python mysqldb连接数据库
今天无事想弄下python做个gui开发,最近发布的是python 3k,用到了数据库,通过搜索发现有一个mysqldb这样的控件,可以使用,就去官方看了下结果,没有2.6以上的版本 没办法就下了一个 ...
- Tomcat安装与配置图文教程
安装Tomcat之前先配置JDK,JDK的JAVA_HOME变量都必须设置好,以便Tomcat找到JDK.关闭防火墙等. 一:安装版Tomcat 1. 先下载tomcat,到http://tomcat ...
- The partner transaction manager has disabled its support for remote/network transactions.
http://technet.microsoft.com/en-us/library/cc753510(WS.10).aspx
- 独立两套DJANGO+CELERY配置(生产+测试)时要注意的一些细节
1,生产的NGINX环境,要指定自己的目录,而不是PROJ默认的. upstream ism_host { server ; } server { listen ; server_name local ...
- 使用php实现权限管理模块
在说权限管理模块前,应该先知道权限管理模块要有哪些功能: 1.用户只能访问,指定的控制器,指定的方法 2.用户可以存在于多个用户组里 3.用户组可以选择,指定的控制器,指定的方法 4.后台可以添加 ...
- Java从入门到精通(一)
Java编程可以分为三个方向 ① Java se (j2se) 桌面开发 ② Java ee (j2ee) WEB开发 ③ Java me (j2me) 手机开发 java se 是基础中的基础 Ja ...
- java.utils.HashMap数据结构分析(转)
上图为Hashmap的数据结构图,具体实线是采用数组结合链表实现,链表是为了解决在hash过程中因hash值一样导致的碰撞问题. 所以在使用自定义对象做key的时候,一定要去实现hashcode方 ...
- linux设备驱动那点事儿之平台设备理论篇
一:Platform总线 1.1概述 一个现实的linux设备驱动通常需要挂接在一种总线上,对于本身依附于PCI,USB,IIC,SPI等的设备而言,这自然不是问题,但是在嵌入式系统里面,SOC系统中 ...
- 【HDOJ】1508 Alphacode
简单DP.考虑10.20(出现0只能唯一组合).01(不成立). /* 1508 */ #include <iostream> #include <string> #inclu ...
- common.css 值得学习的css样式布局
正常的项目当中,应当有一个common.css,就是把一些常用的样式,写入其中. 然后再结合一些特性的css,构造漂亮的页面. 下面欣赏一些海盗商城的common.css. /***样式初始化***/ ...