Skip to content

Instantly share code, notes, and snippets.

@rymawby
Created September 15, 2015 14:52
Show Gist options
  • Save rymawby/da2233b58ebeae2885f4 to your computer and use it in GitHub Desktop.
Save rymawby/da2233b58ebeae2885f4 to your computer and use it in GitHub Desktop.
Padawan to Jedi: Improve with: Tell don't ask / magic number removal / abstraction
class Ship
{
  function droidWillFitInCockpit() : Boolean
  {
    var droid:Droid = getCurrentDroid()
    var droidHeight:Integer = 0;
    if(this.droid.name == "C-3PO")
    {
      droidHeight = 167
    }
    elseif(this.droid.name == "r2d2")
    {
      droidHeight = 109
    }
    return (droidHeight < 130)
  }
}

class Droid
{
  function Droid(name:String, height:Integer)
  {
    this.name = name;
    this.height = height;
  }
}
@cwright017
Copy link

class Ship
{
  function droidWillFitInCockpit() : Boolean
  {
    var droid:Droid = getCurrentDroid()
    return droid.getHeight() < this.cockpitSize
}

class Droid
{
  function Droid(name:String, height:Integer)
  {
    this.name = name;
    this.height = height;
  }

  function getHeight(){
    return this.height
  }

}

Much better - assuming that the ship object has been instantiated with a cockpit size / there is a setter for this .. then the droid doesn't need to care about that - nor does the ship care about the droids name or have to set the droids height.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment