Refactoring - Explaining Variable

This topic is for the [Explaining Variable] exercise in the [Refactoring] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Explaining Variable]: https://exercises.upcase.com/exercises/explaining-variable
[Refactoring]: Refactoring Code | Code Refactoring Online Tutorial by thoughtbot

Seems like thereā€™s something wrong with the syntax of the initialize method? my tests kept failing until i changed it to:

  def initialize(amount, discount_percentage= 0, tip_percentage)
    @amount = amount
    @discount_percentage = discount_percentage
    @tip_percentage = tip_percentage
  end

Hello @willisplummer, the example as written uses Ruby keyword arguments (with defaults) which were introduced in Ruby 2.0. My guess is you are running on an older ruby. Can you confirm the output of ruby -v, and likewise, are you running any Ruby version manager like rvm or rbenv?

@christoomey running into the same issue. iā€™m using rbenv, and have multiple ruby versions installed. my global version is set to 2.1.5, but the repo wants to use 2.0.0-p418, which is installed.

iā€™ve tried changing the ruby version of the project to something a little higher and it doesnā€™t seem to want to do anything besides throw a syntax error when I try to run the specs.

explaining-variable/lib/tipper.rb:4: syntax error, unexpected ',' (SyntaxError)def initialize(amount:, discount_percentage: 0, tip_percentage:)

@chicjean,

In my explaining-variable repo there are commits from March 2015 that add a .ruby-version file specifying 2.2.1. Where are you seeing 2.0.0-p418?

@davidcpell i noticed that, too. turns out that at least for me, i was having some trouble with rbenv recognizing the correct version. rbenv rehash and bundle install seemed to fix it for me.

1 Like

Hi, Iā€™m having issues running the setup script from the bin directory. I was able to clone the repo and cd into explaining-variable. However, each time I try to run the setup script, I receive this error:

bin/setup: line 10: bundle: command not found

The instructions are quite clear:

git clone git@git.upcase.com:TechnoAlchemist/explaining-variable.git
cd explaining-variable
bin/setup

Am I missing something? Is there a configuration issue with my .bash_profile? Iā€™m a little confused as to what is the problem.

Thanks in advance!

1 Like

It sounds like you donā€™t have bundler installed for that particular Ruby version. Run gem install bundler and try run bin/setup again.

Hi,

I have a question regarding explaining variable refactoring, is it like a preparation step before using extract method approach ?
and in which cases do you just use this refactoring approach instead of extracting to a method ?

Thx :slight_smile:

Introduce Explaining Variable is a refactoring by itself, not just a preparatory step for Extract Method, although it could be used as an intermediate step to Extract Method.

Thereā€™s usually a trade-off between reducing the complexity of a method vs adding an extra level of indirection.

For example, letā€™s say you were calculating the number of seconds in a year:

def seconds_in_year(leap)
  60 * 60 * 24 * (leap ? 366 : 365)
end

You could refactor this with Introduce Explaining Variable to:

def seconds_in_year(leap)
  seconds_in_minute = 60
  seconds_in_hour = 60
  hours_in_day = 24
  days_in_year = leap ? 366 : 365
  seconds_in_minute * seconds_in_hour * hours_in_day * days_in_year
end

You could stop there, but that method is starting to look a little dense. Itā€™s not worth extracting out the simple assignments, but it might be worth applying Extract Method for days_in_year:

def seconds_in_year(leap)
  seconds_in_minute = 60
  seconds_in_hour = 60
  hours_in_day = 24
  seconds_in_minute * seconds_in_hour * hours_in_day * days_in_year(leap)
end

def days_in_year(leap)
  leap ? 366 : 365
end

Hope this helps.

1 Like

Thanks @andyw8 for this amazing example. It was very helpful. :thumbsup:

Completely lost on what I am trying to do, but canā€™t even push as the hint suggests - just says Everything up-to-date

Even if I just add whitespace, I canā€™t commit and pushā€¦ does anyone know what is going wrong?

In the featured solution it is used a local variable named tax to hold the tax amount. Donā€™t you think that it can cause confusion with the TAX constant?