• 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 and find.

      Typically direct translations from database tables, or other data sources.

Clean Code – Chapter 6 Objects and Data Structures的更多相关文章

  1. Objects and Data Structures

    Date Abstraction Hiding implementation is not just a matter of putting a layer of fucntions between ...

  2. Clean Code – Chapter 3: Functions

    Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...

  3. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

  4. Clean Code – Chapter 4: Comments

    “Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...

  5. 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 ...

  6. Clean Code – Chapter 5 Formatting

    The Purpose of Formatting Code formatting is about communication, and communication is the professio ...

  7. 代码整洁之道Clean Code笔记

    @ 目录 第 1 章 Clean Code 整洁代码(3星) ?为什么要整洁的代码 ?什么叫做整洁代码 第 2 章 Meaningful Names 有意义的命名(3星) 第 3 章 Function ...

  8. 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 ...

  9. (转) Data structures

      Data structures A data structure is a group of data elements grouped together under one name. Thes ...

随机推荐

  1. Mysql ID重新排列

    我们经常会遇到,在删除数据库某条记录时,原来的ID排序会有间隔,比如删除了ID为8的数据,这个表的ID排序就会从7直接到9, 那我们如何解决这个ID重新排列的问题呢? 只需一下三步: 1.删除这个表的 ...

  2. VB-获取本机计算机名,登录名、ip地址

    真的是很简单,执行结果:Computer:不平凡总在于坚持  User:Administrator  IP:192.168.0.111     '获取计算机名.用户名.本机ip     Dim Loc ...

  3. Sublime Text 3之Package Control 安装

    1.通过快捷键 ctrl+` 或者 View > Show Console 打开控制台,然后粘贴以下安装代码: import urllib.request,os; pf = 'Package C ...

  4. cocos2d-js Mac下的JSB绑定步骤

    cocos2d-js由于采用js语言,使得做一些native的功能比较受限,例如文件和目录操作.socket操作等.逼不得已,这时我们就不得不做jsbinding了.. 官方提供的jsbinding方 ...

  5. Codeforces Round #361 div2

    ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...

  6. Ubuntu14.04下Unity桌面托盘图标显示问题

    本来想丰富一下功能,遂开始安装大开眼界:Ubuntu下10个厉害的Indicator小程序这里的Indicator小程序. 很不幸,在安装到indicator-multiload的时候,准备注销看一下 ...

  7. Unity3D研究院之在MAC上脚本XlsxWriter写入Excel .xlsx格式

    原地址:http://www.xuanyusong.com/archives/3011 以前找了很久可以跨平台支持读写Excel的工具,我也试了很多种DLL.可在Windows上各个完美支持,可是在M ...

  8. PIXLCLOUND

    http://pixlcloud.com/main/career/ https://www.recordedfuture.com/siem-threat-intelligence-part-1/

  9. POJ3714+最近点对

    特判标记即可 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h& ...

  10. UVA 515 King

    差分约束系统的第一个题目,看了落花大神的博客后,对差分约束有了一定了解,关键在于建图,然后就是判断是否存在负权回路. 关于差分约束系统的解释详见维基百科:http://zh.wikipedia.org ...