Slide 1

Slide 1 text

Refactoring Fat Components With Ruby on Rails examples

Slide 2

Slide 2 text

Who am I? ● Tutti Quintella (@tuttiq online) ● From São Paulo, Brazil ● Major in Computer Engineering ● Software Engineer since 2011 ● Software Engineer @ Mercari ● Director @ Women Who Code Tokyo

Slide 3

Slide 3 text

DEAR WOMEN, WE NEED YOU https://meetup.com/Women-Who-Code-Tokyo/

Slide 4

Slide 4 text

Disclaimer ● This presentation touches controversial topics like: ○ Code readability ○ Design patterns ○ Coding principles / "good practices" ● Most of the content is opinion based and subjective to exceptions / counter-arguments

Slide 5

Slide 5 text

What are Fat Components? ● Too much logic in the same place ● Too many responsibilities in the same component

Slide 6

Slide 6 text

Problems of fat components ● Harder to find logic when looking at the file structure ● Harder to read and understand what a component does ● Components are too specific and less reusable ● Harder to break the app in separate modules ● Harder to unit test

Slide 7

Slide 7 text

Design patterns ● Splitting the code into smaller parts ● More defined responsibilities ● Well known patterns: easier to find people that already know them, or easier to find learning resources

Slide 8

Slide 8 text

Design patterns - MVC Image copyright by Sandro Mancuso at Codurance

Slide 9

Slide 9 text

Sometimes only M, V and C is not enough... ● Fat Controllers ● Fat Models ● Too much logic in one place

Slide 10

Slide 10 text

First advice "Skinny controllers, fat models" "Controllers should only concern themselves with receiving data from the client and passing data to the views. Logic should be on the models" Result: Skinny controller, OBESE models. User.rb - 700+ lines...

Slide 11

Slide 11 text

Single Responsibility Principle SOLID ↪ Single Responsibility Principle: a class should have only a single responsibility

Slide 12

Slide 12 text

Ways to extract logic ● Mixins ● Value objects ● Service objects ● Form objects ● Policy objects ● Query objects

Slide 13

Slide 13 text

Mixins ● Pulling sets of methods out of a large model into a module/interface: makes the model smaller, but does it really organize the logic?

Slide 14

Slide 14 text

Mixins (properly implemented)

Slide 15

Slide 15 text

Value Objects ● Most languages already have some native value objects like Date, Time, URI, File. ● Any attributes that are more than basic strings or numbers are good candidates to be domain specific value objects ● Ex: Phone, Address, Currency, Rating, Score, Color...

Slide 16

Slide 16 text

Value Objects

Slide 17

Slide 17 text

Service Objects Moving actions to objects dedicated only to perform them. Good candidates: ● Actions that reach across multiple models ○ E.g.: User + Order + CreditCard to perform a purchase ● Actions that interact with external services / APIs ○ E.g.: FacebookAPI Service

Slide 18

Slide 18 text

System design: Service Objects Moving actions to objects dedicated only to perform them. Good candidates: ● Actions that are not a core concern of the underlying model ○ E.g.: data cleanup, parsing, normalization... ● Actions that can be performed in multiple ways (Strategy pattern) FileParser XML Parser JSON Parser

Slide 19

Slide 19 text

System design: Service Objects

Slide 20

Slide 20 text

Form Objects ● Form Objects are useful (and recommended) when a form doesn't map directly and simply to a single model. E.g: ○ Data needs parsing or formatting ○ Data has complex validations ○ More than one model are updated through the same form (nested models)

Slide 21

Slide 21 text

Form Objects

Slide 22

Slide 22 text

Others ● Policy objects (define rules / complex validations for other models) ● Query objects (objects to perform complex queries, that depend on multiple relations) ● etc...

Slide 23

Slide 23 text

Bonus: Breaking down the architecture microservices.io

Slide 24

Slide 24 text

THANK YOU Contact: - Email: [email protected] - Facebook: https://fb.me/tuttiquintella - Twitter: @tuttiq - Github: @tuttiq - LinkedIn: https://linkedin.com/in/tuttiquintella