Created
August 7, 2021 08:20
-
-
Save adriangohjw/2bbea0f0df2fc7d77fc91552bc82d1bb to your computer and use it in GitHub Desktop.
improving code readability: 5 simple tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> better code (with logical line breaks) | |
def check_out(current_user) | |
if cart.count == 0 | |
return Status.new(false, "empty cart") | |
end | |
total_price = cart.calculate_total_price | |
if current_user.balance < total_price | |
return Status.new(false, "not enough money") | |
end | |
current_user.deduct_balance(total_price) | |
return Status.new(true, nil) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> meh code | |
def check_out(current_user, cart) | |
if cart.count == 0 | |
return Status.new(false, "empty cart") | |
end | |
total_price = cart.calculate_total_price | |
if current_user.balance < total_price | |
return Status.new(false, "not enough money") | |
end | |
current_user.deduct_balance(total_price) | |
return Status.new(true, nil) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> better code (happy path first) | |
if transaction.is_successful | |
handle_success(transaction) | |
else | |
handle_failure(transaction) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> meh code | |
if transaction.is_failure | |
handle_failure(transaction) | |
else | |
handle_success(transaction) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> better code (early returns with guard clauses) | |
def check_out(current_user) | |
cart = current_user.cart | |
if cart.count == 0 | |
return Status.new(false, "empty cart") | |
end | |
if !cart.are_products_available | |
return Status.new(false, "some products are unavailable") | |
end | |
if current_user.balance < total_price | |
return Status.new(false, "not enough money") | |
end | |
current_user.deduct_balance(total_price) | |
return Status.new(true, nil) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> meh code | |
def check_out(current_user) | |
cart = current_user.cart | |
if cart.count > 0 | |
if !cart.are_products_available | |
if current_user.balance >= total_price | |
current_user.deduct_balance(total_price) | |
return Status.new(true, nil) | |
else | |
return Status.new(false, "not enough money") | |
end | |
else | |
return Status.new(false, "some products are unavailable") | |
end | |
else | |
return Status.new(false, "empty cart") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> better code (avoid reassigning of variables) | |
#= Method 1 (introduce a new variable) | |
users = Users.completed_onboarding_form | |
users_backend = users.where(specialisations: "backend") | |
#= Method 2 (define it to what we need immediately) | |
users = Users.completed_onboarding_form | |
.where(specialisations: "backend") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> meh code | |
users = Users.completed_onboarding_form | |
users = users.where(specialisations: "backend") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> better code (with usage of meaningful names) | |
def calculate_total_price(price, quantity, discount, shipping) | |
(price * quantity) - discount + shipping | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=> meh code | |
def cal_tp(p, q, d, s) | |
(p * q) - d + s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://adriangohjw.github.io/posts/2020/07/31/writing-readable-code.html