if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions to simplify your ifs and dynamically call the appropriate method.

For example you have the code like:

  1. function charge(){
  2.  
  3. if(this.moive.type==="regular"){
  4. // ...some logic
  5. if(this.daysRented > ){
  6. // ...some logic
  7. }
  8. }else if(this.moive.type==="new release"){
  9. // ...some logic
  10. }else if(this.moive.type==="childrens"){
  11. // ...some logic
  12. if(this.daysRented > ){
  13. // ...some logic
  14. }
  15. }
  16.  
  17. return amount;
  18. }

We can refactor to:

  1. require("activesupport")
  2.  
  3. this.charge = () => {
  4. return this.[this.type.titleize().split(" ").join('').camelize() + "Charge"](daysRented);
  5. }
  6.  
  7. this.regularCharge = ()=>{
  8. if(daysRented > ){
  9.  
  10. }
  11. }
  12.  
  13. this.newRelseaseCharge = () => {
  14.  
  15. }
  16.  
  17. this.childrenCharge = () => {
  18. if(daysRented > ){
  19.  
  20. }
  21. }

So based on the type we will call different function.

[Javascript] Refactoring: Polymorphic Functions的更多相关文章

  1. JavaScript ES6 Arrow Functions(箭头函数)

    1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...

  2. [Javascript] Compose multiple functions for new behavior in JavaScript

    In this lesson you will create a utility function that allows you to quickly compose behavior of mul ...

  3. Eloquent JavaScript #05# higher-order functions

    索引 Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop ...

  4. [Javascript Crocks] Compose Functions for Reusability with the Maybe Type

    We can dot-chain our way to great success with an instance of Maybe, but there are probably cases wh ...

  5. [Javascript] Run asynchronous functions in sequence using reduce

    This can be handy if you have a rate limit on API requests or if you need to pass the result of each ...

  6. FusionCharts JavaScript API - Functions 常用方法整理笔记

    FusionCharts JavaScript API - Functions Home > FusionCharts XT and JavaScript > API Reference  ...

  7. 前端面试-----JavaScript题

    用面试题,复习一下,js基础. 1.综合题 function Foo() { getName = function () { alert (1); }; return this; } Foo.getN ...

  8. Duilib嵌入CEF以及JavaScript与C++交互

    转载:http://blog.csdn.net/foruok/article/details/50573612 转载:http://blog.csdn.net/foruok/article/detai ...

  9. 44个 Javascript 变态题解析 (上\下)

    第1题 ["1", "2", "3"].map(parseInt) 知识点: Array/map Number/parseInt JavaS ...

随机推荐

  1. Asp.net 事务处理

    事务处理是在数据处理时经常遇到的问题,经常用到的方法有以下三种总结整理如下:方法1:直接写入到sql 中在存储过程中使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRA ...

  2. 更新Xcode7 后 .dylib变成了.tbd的问题解决

    拿添加libsqlite3.dylib为例 1.打开你添加的libsqlite3.tbd 文本文件,然后有一行 install-name:    /usr/lib/libsqlite3.dylib   ...

  3. android 06

    1.android原理 菜单-->MainActivity-->onCreate-->setContentView(R.layout.item)-->layout(item.x ...

  4. Linux下python3与python3的多版本共存

    python3已经出来有些许时候了,python3相比python2进行了大量的改进,包括语法,新的功能,还有优化.虽然很多库已经同时支持 python2和python3了,但是有些库仍然没有很好的支 ...

  5. InstallShield Basic MSI工程常见问题解答[转]

    1.  问题描述:采用何种安装模式?实现方法:如果对用户界面等自定义要求不高的话,建议用Basic Msi Project,否则用InstallScript MSI Project. 2.  问题描述 ...

  6. 如何在C#添加鼠标右键菜单

    C#添加鼠标右键方法步骤: 1 选中要添加右键功能的Form或者控件,打开控件的设计页面. 2 从工具箱中找到ContextMenuStrip控件,将这个控件拖曳到Form或者控件的设计页面上.这时系 ...

  7. Razor引擎的转换数据类型

    AsInt() 把字符串转换为整数. if (myString.IsInt()) IsInt() {myInt=myString.AsInt();} AsFloat() 把字符串转换为浮点数. if ...

  8. PHP面向对象(OOP)编程完全教程:10.__set(),__get(),__isset(),__unset()四个方法的应用

    一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...

  9. window.onload() 等待所有的数据加载都完成之后才会触发

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Memcached源码分析——hash

    以下为memcached中关于使用的hash算法的一点记录 memcached中默认使用的是Bob Jenkins的jenkins_hash算法 以下4段代码均在memcached-1.4.22/ha ...