Designing in any regards is not a straight line. As we try to develop something whether it be a computer program, a painting, or even a plate of food, we try to adhere to certain principles and patterns to guide us in creating the best product possible.
Object orientation follows 3 main principles:
1) Separation of Concerns
2) DRY (Don't Repeat Yourself)
3) Line Limits.
Single Responsibility and Separation of Concerns
In Ruby, we have this concept of single responsibility, which is exactly what it sounds like; classes will have one job. This also means their services are aligned to facilitate that one job. This concept ties into separation of concerns. Each responsibility of a computer program needs to be separated and handle a different objective.
class MyStore
def sign_in(user)
@user = user
end
def current_user
@user
end
def item(item)
@item = item
end
def item_price=(price)
@item_price = price
end
def shopping_cart
@shopping_cart = []
end
def add_item_to_cart(item)
@shopping_cart << item
end
...
end
Each concern here is defined in its own class and handles a single responsibility. sign_in
will handle signing in to the program, current_user
recognizes the user signing in, item
defines items in the program, and so on. If something were to go awry in our application and needs correcting, we don't have to disrupt other parts of the code that may potentially cause more problems than are already occurring.
Don't Repeat Yourself
def checkout(discount=0)
total = 0
shopping_cart.each do |item|
total += item.price
end
if discount == 10
total = total - total * 10 / 100.00
elsif discount == 20
total = total - total * 20 / 100.00
elsif discount == 50
total = total - total * 50 / 100.00
end
total
end
This block of code is messy and repetitive. While it gets the job done, it is prone to breaking and any maintenance needed could cause more issues. If we were to re-factor, it would look like this:
if coupon
total = total - total * coupon / 100.00
end
We have now achieved the same function but with less code. This allows for the program to run smoother and if any maintenance is necessary, it is flexible and less prone to causing greater issues.
Line Limits
The final principle of object-oriented design would be line limits. Methods should not exceed 5 lines of code and classes should not exceed 100. These limits are not absolutely strict and are open to exceptions. They simply serve as a guide to writing clean and effective code. Programming is all about efficiency and effectiveness. If we have a block of code that's 20 lines long but we can refactor to achieve the same function in 5 lines, we should. If we think in terms of the shortest, most effective code possible, we can generate more efficient and less costly programs that are also easier to read, maintain, and update.
Conclusion
These three main concepts of object oriented design are meant as guides, not strictures. While they are not absolutely necessary, they are critical in developing real-world applications that are cost effective, easy to use, and easy to maintain. They also keep you sane which is incredibly important in the world of software engineering! There are many moving parts in this field so if there is someway to make our experience faster and easier, developers will do better and feel better in the long run.
References
https://www.poodr.com/
https://www.rubyguides.com/ruby-tutorial/object-oriented-programming/
Top comments (0)