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 ...
随机推荐
- Delphi 文字跑马灯
//跑马灯 procedure Tfr_Main.tme_TitleTimer(Sender: TObject); var strTrim: Widestring; begin strTrim := ...
- Python 守护进程
import os import sys from time import sleep try: pid = os.fork() if pid > 0: sys.exit(0) # Exit p ...
- Django数据库配置
将Django使用数据库由默认的sqlite3更改为mysql: 1.安装mysql驱动程序 MySQLdb(mysql-python) mysqlclient Connector/Python Py ...
- PL/SQL — BULK COLLECT用法
BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNING ...
- BZOJ 2124等差子序列 线段树&&hash
[题目描述 Description] 给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len& ...
- 解释型语言和编译型语言的不同以及Python如何运行
计划写关于Python中如何实现属性管理.函数(或类方法)管理.类管理的几篇成系列的文章. 而这篇文章写在这个系列之前,希望对后面几篇文章的理解有所帮助. 老实说,我也是在网上搜索了一些资料才写的这篇 ...
- 上传项目到Github
1.使用根工具(均是图形化的界面) TortoiseGit-1.8.12.0-32bit GitExtensions-2.48.05-SetupComplete 2.大致步骤 首先,你需要一个Gith ...
- android ListView内数据的动态添加与删除
main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- 使用CURL出现certificate verify failed错误的解决方法
今天使用CURL访问微信平台接口时遇到一个错误,返回错误代码如下: ? 1 2 SSL certificate problem, verify that the CA cert is OK. Deta ...
- KMP的next[]数组
KMP是众多字符串问题的基础 理解next数组尤为重要 next又称前缀数组 是 它所处位置的前一个位置的元素 与 该链 链首开始 几个元素相匹配(即相同) 举个实例来说明: next对应的是该位置的 ...