Coding as a Craft Bootcamp Prep - v2.0
  • Introduction
  • Week 1 - Programming Basics - Ruby
    • Understanding the problem statement
    • User stories
    • Pair programming
    • The ATM challenge
      • Step 1 - Setting the stage
      • Step 2 - The core functionality
      • Step 3 - Interacting with objects
      • Step 4 - Refactoring
      • Step 5 - Testing the sad path
      • Step 6 - Cash is King
      • Step 7 - The Account
      • Step 8 - The Person
      • Step 9 - Making it all work together
    • Library Challenge
      • Important Topics
    • Extras
    • RSpec
      • RSpec introduction
      • Install and configure
      • RSpec Basics
      • How to write specs
      • So many Expectations...
      • Matchers
  • Week 2 -Programming Basics - JavaScript
  • Week 3 - TypeScript and Angular
  • Week 4 - Ruby on Rails Basics
  • Week 5 - Working With Legacy Code
  • Week 6 - Midcourse Project
  • Week 7 - Going Mobile
  • Week 8 & 9 - Advanced SaaS Applications
  • Week 10 - Expose and Consume API's
  • Configuring RSpec
Powered by GitBook
On this page
  1. Week 1 - Programming Basics - Ruby
  2. The ATM challenge

Step 4 - Refactoring

Okay, we will stop here for a moment ad do some changes to our code to make it more readable and to follow the principle that each method should only have one responsibility. In our case, the way we have written the withdraw method, the method perform several tasks. Since we will be developing that method further, we want to introduce a better, more readable structure.

In practical terms, we want to extract some of this methods responsibilities to separate, so called private methods.

  1. We want to extract the check of account.balance to a separate method.

  2. We want to extract the transaction to a separate method.

Evaluate this code carefully.

!FILENAME lib/atm.rb

class Atm
  attr_accessor :funds

  def initialize
    @funds = 1000
  end

  def withdraw(amount, account)
    # We will be using Ruby's `case`- `when` - `then` flow control statement
    # and check if there is enough funds in the account
    case
    when insufficient_funds_in_account?(amount, account)
      # we exit the method if the amount we want to withdraw is bigger than
      # the balance on the account
      return
    else
      # If it's not, we perform the transaction
      perform_transaction(amount, account)
    end
  end

  private

  def insufficient_funds_in_account?(amount, account)
    amount > account.balance
  end

  def perform_transaction(amount, account)
    # We DEDUCT the amount from the Atm's funds
    @funds -= amount
    # We also DEDUCT the amount from the accounts balance
    account.balance = account.balance - amount
    # and we return a responce for a successfull withdraw.
    { status: true, message: 'success', date: Date.today, amount: amount }
  end
end

Note that we have NOT made any changes to our test and if you run them now, they should all pass green.

Refactoring is all about that. You make your code better WITHOUT introducing any new functionality.

PreviousStep 3 - Interacting with objectsNextStep 5 - Testing the sad path

Last updated 7 years ago