beyond the rails way

35
Beyond The Rails Way Andrzej Krzywda http://rails-refactoring.com

Upload: andrzej-krzywda

Post on 07-Jul-2015

2.107 views

Category:

Software


0 download

DESCRIPTION

The Rails Way is the reason of Rails success. It's also the reason of Rails disappointment. Why is it so? Is there any alternative?

TRANSCRIPT

Page 1: Beyond The Rails Way

Beyond The Rails Way

Andrzej Krzywda http://rails-refactoring.com

Page 2: Beyond The Rails Way

Why is Rails business-friendly?

• Quick to produce a prototype

• Prototypes are production-ready enough

• Easy to add new features

• Possible to turn a prototype into proper production app

Page 3: Beyond The Rails Way

Because of The Rails Way

Page 4: Beyond The Rails Way

Why is Rails not business-friendly?

• Because of The Rails Way

• Business loves the speed of adding new features

• … but loves predictability even more

• There’s no predictability with The Rails Way

Page 5: Beyond The Rails Way

The Rails Way• Scaffold-like code in controllers/views

• ActiveRecord goes all the way up to the view

• features implemented with external gems

• external gems assume ActiveRecord in the views

• all models connected to each other with associations

• non-trivial things implemented with callbacks, filters, state-machine, STI, validations

• some JS/Coffee on top of the server-rendered html

• one monolith app

• Convention over Configuration

• Magic (relying on meta)

• Don’t Repeat Yourself

• “We’re 95% done with this app, can you help us finish it?”

Page 6: Beyond The Rails Way

When is The Rails Way good?

• for business/coding people to prototype

• for less-experienced developers

• to quickly get a result

• for geniuses

• they will handle any code

• mostly-CRUD

• logic-less apps

Page 7: Beyond The Rails Way

When is The Rails Way bad?

• advanced developers

• complex business logic

• long-living business processes (like order)

• multiple teams

• predictable speed of work

Page 8: Beyond The Rails Way

If not The Rails Way then what?

Page 9: Beyond The Rails Way

Beyond The Rails Way

Page 10: Beyond The Rails Way

The Next Way

Page 11: Beyond The Rails Way

The Next Way

• service objects

• repositories

• form objects

• adapters

• domain objects

• events

Page 12: Beyond The Rails Way

The Next Way is heavily influenced by DDD and classical

OOP patterns

Page 13: Beyond The Rails Way

It’s not all or nothing

Page 14: Beyond The Rails Way

You can mix The Rails Way with The Next Way

Page 15: Beyond The Rails Way

Gradual changes

Page 16: Beyond The Rails Way

start with service objects

Page 17: Beyond The Rails Way

service objects are the gateway drug

Page 18: Beyond The Rails Way

Gradually reduce the Rails magic

Page 19: Beyond The Rails Way

Magic is bad

Page 20: Beyond The Rails Way

We’re software developers, not software magicians

Page 21: Beyond The Rails Way

Turn implicit into explicit.

Page 22: Beyond The Rails Way

Turn conventions into

explicit code

Page 23: Beyond The Rails Way

Don’t Repeat Yourself

We went too far with this rule.

Page 24: Beyond The Rails Way

Coupling is worse than

code duplication

Page 25: Beyond The Rails Way

DRY examples

• It’s OK to have different User classes for authentication, storage and for presentation

• It’s OK to duplicate some code in controllers/services instead of relying on the controller filters

Page 26: Beyond The Rails Way

The Next Way is just one possible set of techniques

Page 27: Beyond The Rails Way

Other alternatives

• DCI / Clean Ruby

• CQRS

• Event Sourcing

• Ports & Adapters

Page 28: Beyond The Rails Way

Choose what’s best for your project

Page 29: Beyond The Rails Way

Experiments are OK (as long as you keep the app working)

Page 30: Beyond The Rails Way

Make safe changes

Page 31: Beyond The Rails Way

small steps

Page 32: Beyond The Rails Way

Your tests should be green all the time

Page 33: Beyond The Rails Way

Learn how to refactor without any fear.

Page 34: Beyond The Rails Way

Rails Refactoring recipes• Inline controller filters

• Explicitly render views with locals

• Extract render/redirect methods

• Extract a SingleActionController class

• Extract a routing constraint

• Extract an adapter object

• Extract a repository object

• Extract a service object

Page 35: Beyond The Rails Way

Thanks!

“Fearless Refactoring: Rails Controllers” http://rails-refactoring.com

still on a discounted price! (1.0 available from December 1st)