Posted about 7 years ago. Visible to the public.
Software design basics [2d]
Reading
Read the following chapters from The Pragmatic Programmer (in our library):
- Software Entropy
- The Evils of Duplication
- Orthogonality
- Decoupling and the Law of Demeter
- Evil Wizards
Read the following chapters from Clean Code (in our library):
- Chapter 1: Clean Code
- Chapter 2: Meaningful Names
- Chapter 3: Functions
- Chapter 4: Comments
- Chapter 5: Formatting
- Chapter 8: Boundaries
- Chapter 10: Classes
- Chapter 12: Emergence
- Chapter 17: Smells and Heuristics
Also read:
- Keep It DRY, Shy, and Tell the Other Guy Archive
- GRASP (object-oriented design) Archive
- Single Responsibility Principle Archive
Read the following chapters from our book Growing Rails Application in Practice:
- Dealing with fat models
- Extracting service objects
Discuss with your mentor what you took away from each topic.
Exercise
We're building an e-commerce app where users can create and view invoices.
This is our current model:
Copyclass Invoice < ApplicationRecord has_many :items validates_presence_of :recipient_address, :number end class Invoice::Item < ApplicationRecord belongs_to :invoice belongs_to :product validates_numericality_of :units end class Product < ApplicationRecord validates_presence_of :description validates_numericality_of :unit_price end
This is a view that shows an invoice:
Copy%h1 Invoice = @invoice.number %h3 Recipient = @invoice.recipient_address %h3 Items %table %tr %th Description %th Quantity %th Item total - @invoice.items.each do |item| %td= item.product.description %td= item.units %td= item.units * item.product.unit_price %tr %th Invoice total %td(colspan=3) = @invoice.items.sum { |item| item.units * item.product.unit_price } * 1.19
How would you judge the quality of this code?
Can you apply what you learned and improve the model and the view?
Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).