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 has the code like:
function Charge () {if( This. moive.type==="Regular"){ //... some logic if( This. daysrented >2){ //... some logic } }Else if( This. moive.type==="New Release"){ //... some logic}Else if( This. moive.type==="Childrens"){ //... some logic if( This. daysrented >3){ //... some logic } } returnamount;}
We can refactor to:
Require"Activesupport") This. Charge = () = { return This. [ This. Type.titleize (). Split (" "). Join ("'). Camelize () +"Charge"] (daysrented);} This. Regularcharge = () ={ if(Daysrented >2){ }} This. Newrelseasecharge = () = {} This. Childrencharge = () = { if(Daysrented >3){ }}
So based the type we'll call different function.
[Javascript] Refactoring:polymorphic Functions