zoukankan      html  css  js  c++  java
  • Typora

    Typora

    怎么编写可测试代码?

    Flaw #1 构造方法做了真实的工作

    告警标志

    • 在构造方法中的new关键字和字段声明
    • 在构造方法中的static方法调用和字段声明
    • 在构造器中,任何除了字段赋值的工作
    • 在构造方法结束后,对象仍然没没有完全初始化
    • 在构造器中的分支逻辑(if,loop)
    • 在构造器内完成了复杂的对象图构造而不是使用工厂方法.
    • 添加或者使用初始化block

    Flaw #2 深入探究辅助对象

    告警标志

    • 对象传递进来,却从未直接使用(只是间接使用了其他对象)
    • 对象的方法调用使用了链式的调用(a.b.c之类的)
    • 可疑的命名:例如 context,environment,principal,container,or manager

    Flaw #3 脆弱的全局状态和单例

    告警标志

    • 添加和使用单例
    • 添加和使用静态属性和静态方法
    • 添加和使用静态的初始化block
    • 添加和使用registries(什么东西?)
    • 添加是使用service locators(什么东西?)

    Flaw #4 类做了太多事情

    告警标志

    • 使用了and来总结这个类做了什么
    • 类对与新人快速熟手有很大的挑战
    • 类有一些只有在某些方法使用的字段
    • 类有只操作了一个参数的静态方法

    原文链接:http://misko.hevery.com/code-reviewers-guide/

    原文:Guide: Writing Testable Code

    To keep our code at Google in the best possible shape we provided our software engineers with these constant reminders. Now, we are happy to share them with the world.

    Many thanks to these folks for inspiration and hours of hard work getting this guide done:

    Also thanks to Blaine R Southam who has turned it into a pdf book.

    Flaw #1: Constructor does Real Work

    Warning Signs

    • new keyword in a constructor or at field declaration
    • Static method calls in a constructor or at field declaration
    • Anything more than field assignment in constructors
    • Object not fully initialized after the constructor finishes (watch out forinitialize methods)
    • Control flow (conditional or looping logic) in a constructor
    • Code does complex object graph construction inside a constructor rather than using a factory or builder
    • Adding or using an initialization block

    Flaw #2: Digging into Collaborators

    Warning Signs

    • Objects are passed in but never used directly (only used to get access to other objects)
    • Law of Demeter violation: method call chain walks an object graph with more than one dot (.)
    • Suspicious names: context, environment, principal, container, or manager

    Flaw #3: Brittle Global State & Singletons

    Warning Signs

    • Adding or using singletons
    • Adding or using static fields or static methods
    • Adding or using static initialization blocks
    • Adding or using registries
    • Adding or using service locators

    Flaw #4: Class Does Too Much

    Warning Signs

    • Summing up what the class does includes the word “and”
    • Class would be challenging for new team members to read and quickly “get it”
    • Class has fields that are only used in some methods
    • Class has static methods that only operate on parameters

  • 相关阅读:
    在Swift中定义属于自己的运算符
    计算型属性 vs 懒加载
    swift- mutating
    什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
    微信授权登录-微信公众号和PC端网站
    PHP实现购物车的思路和源码分析
    PHP实现图片的等比缩放和Logo水印功能示例
    PHP实现IP访问限制及提交次数的方法详解
    Laravel 队列发送邮件
    laravel 定时任务通过队列发送邮件
  • 原文地址:https://www.cnblogs.com/xilifeng/p/4692361.html
Copyright © 2011-2022 走看看