Skip to content

Instantly share code, notes, and snippets.

@adriangohjw
Created August 7, 2021 08:14
Show Gist options
  • Save adriangohjw/3003bf3360e2903130e62d54d4f6bbb2 to your computer and use it in GitHub Desktop.
Save adriangohjw/3003bf3360e2903130e62d54d4f6bbb2 to your computer and use it in GitHub Desktop.
improving code readability: abstractions
#=> better code (hiding implementation details with abstractions)
STATUS_EMPTY_CART = Status.new(false, "empty cart")
STATUS_PRODUCTS_UNAVAILABLE = Status.new(false, "some products are unavailable")
STATUS_INSUFFICIENT_MONEY = Status.new(false, "not enough money")
STATUS_SUCCESS = Status.new(true, nil)
def check_out(current_user)
cart = current_user.cart
return STATUS_EMPTY_CART if is_cart_empty(cart)
return STATUS_PRODUCTS_UNAVAILABLE if are_products_unavailable(cart)
return STATUS_INSUFFICIENT_MONEY if \
has_insufficient_money(balance: current_user.balance,
total_price: cart.total_price)
current_user.deduct_balance(total_price)
return STATUS_SUCCESS
end
private
def is_cart_empty(cart)
return cart.count == 0
end
def are_products_unavailable(cart)
return cart.are_products_available
end
def has_insufficient_money(balance:, total_price:)
return balance < total_price
end
#=> meh code
def check_out(current_user)
cart = current_user.cart
if cart.count == 0
return Status.new(false, "empty cart")
end
if cart.are_products_unavailable
return Status.new(false, "some products are unavailable")
end
if current_user.balance < cart.total_price
return Status.new(false, "not enough money")
end
current_user.deduct_balance(total_price)
return Status.new(true, nil)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment