The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Refactoring: Polymorphic Functions

Refactoring: Polymorphic Functions

4:37
*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.
Watch this lesson now
Avatar
egghead.io

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

Avatar
Benjamin

I feel like its a bit clearer to create a mapping of type/functions in an object:

{ "new release": "newReleaseCharge" }

Particularly useful when searching text in the app

Avatar
Krono-Safe

The problem here is with obfuscation, because methods' name will be transformed.

Avatar
chad

Really nice pointer on something that exists in nearly every code base. I think the "Strategy Pattern" is also at work here too. Could be an alternative metaphor for explaining how this pattern works. Also, Composition > Inheritance.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?