• 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. JDBC链接

    //1. MySQL(http://www.mysql.com)mm.mysql-2.0.2-bin.jar  Connection con = null;  Class.forName( " ...

  2. UILocalNotification本地通知

    // 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知. 1.在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息.因此,我们将询问用户许可的代码片段添加到了ap ...

  3. Python和VS

    下载VS Code 安装插件Python 安装Python,注意这里需要把Python的目录配置到环境变量中 文档结构非常重要,py文件一定位于根目录,.vscode平级:我曾经因为py文件在.vsc ...

  4. 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例

    wait, notify 和 notifyAll,这些在多线程中被经常用到的保留关键字,在实际开发的时候很多时候却并没有被大家重视.本文对这些关键字的使用进行了描述. 在 Java 中可以用 wait ...

  5. NSStringUIImage~NSData的相互转换以及中文转码

    中文转码 str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 图片编码 NSData *data; ...

  6. Mongodb介绍

    MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前noSql数据库产品中最热门的一种.它在许多场景下用于替代传统的关系型数据库或键值对存储方式,MongoDB是用C++开发,MongoD ...

  7. JDBC之PreparedStatement模糊查询

    今天要做一个关于模糊查询的需求,以前用JDBC做精确查询都是用 "SELECT * FROM test WHERE id = ?",所以用模糊查询时理所当然的也用了"SE ...

  8. vs运行代码版本不一致删除缓存

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files

  9. 数组方法slice()把类数组转成数组和复制一个数组

    function a(){ console.log(arguments.length); var c = [].slice.call(arguments);//类数组转成数组 c.push(5); c ...

  10. loadrunner 脚本和replaylog中的中文乱码问题(转载)

    解决这个问题必须认识到一个事实就是,loadrunner和测试服务器交换数据使用的是utf8格式,但是展现在replaylog中是使用gb2312格式,而且在脚本中如何使用web_reg_find的时 ...