What is Replace Conditional with Polymorphism Refactoring? How is it implemented in Ruby?

Anony-mouse

I recently came across the Replace Conditional with Polymorphism Refactoring while asking for elimination of if..else conditional in ruby.the link

Can anybody explain to me how can i implement the same in ruby?(A simple sweet code would do)

Jörg W Mittag

The Replace Conditional with Polymorphism Refactoring is rather simple and it is pretty much exactly what it sounds like. You have a method with a conditional like this:

def speed
  case @type
  when :european       then base_speed
  when :african        then base_speed - load_factor * @number_of_coconuts
  when :norwegian_blue then if nailed? then 0 else base_speed(@voltage) end
end

and you replace it with polymorphism like this:

class European
  def speed
    base_speed
  end
end

class African
  def speed
    base_speed - load_factor * @number_coconuts
  end
end

class NorwegianBlue
  def speed
    if nailed? then 0 else base_speed(@voltage)
  end
end

You can apply the Refactoring again to NorwegianBlue#speed by creating a subclass of NorwegianBlue:

class NorwegianBlue
  def speed
    base_speed(@voltage)
  end
end

class NailedNorwegianBlue < NorwegianBlue
  def speed
    0
  end
end

Voilà, all your conditionals are gone.

You might ask yourself: does this always work? Can I always replace an if with message dispatch? And the answer is: yes, you can! In fact, if you didn't have if, you can implement it yourself using nothing but message dispatch. (This is, in fact, how Smalltalk does it, there are no conditionals in Smalltalk.)

class TrueClass
  def iff(thn:, els: ->{})
    thn.()
  end

  def &
    yield
  end

  def |
    self
  end

  def !
    false
  end
end

class FalseClass
  def iff(thn:, els: ->{})
    els.()
  end

  def &
    self
  end

  def |
    yield
  end

  def !
    true
  end
end

(3 > 4).iff(thn: ->{ 'three is bigger than four' }, els: ->{ 'four is bigger than three' } )
# => 'four is bigger than three'

true.& { puts 'Hello' }
# Hello

true.| { puts 'Hello' }
# => true

Also relevant: the Anti-IF Campaign

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace Conditional with Polymorphism — how it works?

From Dev

Replace conditional with polymorphism

From Dev

How to do decompose conditional refactoring?

From Java

What is polymorphism, what is it for, and how is it used?

From Dev

How is Ruby's sorting implemented?

From Dev

Refactoring Ruby

From Dev

refactoring code with conditional return

From Dev

How is Array#each implemented in Ruby?

From Dev

How is conditional_wait() implemented at the kernel and hardware/assembly level?

From Dev

refactoring Ruby scraping code

From Dev

ruby - refactoring if else statement

From Dev

Refactoring in Ruby on Rails 4.2.3

From Dev

refactoring ruby methods in rails

From Dev

Refactoring large conditional-statements

From Dev

How to replace \r in a string in ruby

From Dev

How to replace a character by regex in ruby

From Dev

How are Arrays Sets and SortedSets implemented under the hood in Ruby

From Dev

How to use a conditional within a Ruby expect script

From Dev

How to use a conditional within a Ruby expect script

From Dev

How to choice what method should be called when use polymorphism

From Dev

Conditional Postback though ICallbackEventHandler implemented

From Dev

Refactoring if elsif else or in Ruby block

From Dev

Ruby: Refactoring a method to predicted method

From Java

What is polymorphism in Javascript?

From Dev

What is Levity polymorphism

From Dev

What is polymorphism, explained simply?

From Dev

What is the need Polymorphism in OOP?

From Dev

What is the need Polymorphism in OOP?

From Dev

replace simple factory using polymorphism