• 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. PHP提取身份证号码中的生日并验证是否成年的函数

    php 提取身份证号码中的生日日期以及确定是否成年的一个函数.可以同时确定15位和18位的身份证,经本人亲测,非常好用,分享函数代码如下: <?php //用php从身份证中提取生日,包括15位 ...

  2. DELPHI关于文件的操作

    caption:= ExtractFileExt(‘带路径,带扩展名的文件名’);//返回的就是扩展名 caption:=  ChangeFileExt(ExtractFileName(‘带路径,带扩 ...

  3. [python]使用ElementTree解析XML【译】

    19.7 The ElementTree XML API 源码:Lib/xml/etree/ElementTree.py Element类型是一个灵活的容器对象,设计出来是用于存储有层次的数据结构到内 ...

  4. 【pyhton】【转】修改递归次数

    import sys sys.setrecursionlimit(1500) # set the maximum depth as 1500 def recursion(n): if(n <= ...

  5. 基于Emgu CV 的手势识别实现PPT的控制放映

    Emgu CV 简介         众所周知,Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版的OpenCV.开发者可以很方便的通过C#,VB等语言调用OpenCV函数 ...

  6. c语言贪吃蛇

    思路:函数gotoxy(x,y)使光标移植屏幕的x,y坐标(屏幕左上角为0,0),用来绘制蛇和界面,color()函数用来设置绘制的颜色.设有snakelong节,第i节蛇的x坐标为x[i],y坐标为 ...

  7. Oracle RAC备份异机单实例恢复演练

    本文只节选了操作方案的部分章节: 3.   操作步骤 3.1. 异机单实例Oracle数据库软件安装 在异机上进行单实例Oracle数据库软件安装.该步骤过程不再本文中重复描述,如果对安装过程存在疑问 ...

  8. Sybase ASE无响应的又一个情况

    昨天下午,客户那边的系统管理员给我电话,说有套系统的SYBASE数据库最近有点怪,总是时不时莫名其妙地就忽然卡死,有可能一下子就自动恢复了,也有可能后面一直卡住,只好重启.根据客户反映的状况,初步判断 ...

  9. 洛谷 P1064 金明的预算方案

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  10. Matlab使用心得

    1..*和*的区别 .*只能用于两个同型矩阵相乘,且是相对应的元素做乘法运算,其运算规则和我们线性代数里的乘法规则是不一样的:而*用于两个矩阵相乘,如mxn,nxk两个矩阵相乘,它的运算规则和线性代数 ...