Skip to content

Instantly share code, notes, and snippets.

@rwc9u
Created May 18, 2011 15:30
Show Gist options
  • Save rwc9u/978805 to your computer and use it in GitHub Desktop.
Save rwc9u/978805 to your computer and use it in GitHub Desktop.

Rails 3 Design Choices - Jose Valim

SOLID Design Principles

Agile Software Development - Bob Martin Book is based on developing in Java/C… some interpretation for ruby.

Single Responsibility Open Closed - open for extension … closed for modification Liskov Substition Interface Segregation Dependency Inversion

in rails 0.x ActionView::Base (tracking details, rendering templates, rendering context, etc. ) was monolithic. As rails has maturesd template handling was pulled out. In rails 2.2 view paths was pulled out (support for rails engines). Rails 3 view paths subdivided in to resolvers and view paths. The resolver allows you to put templates anywhere (e.g. database, or web service).

Could use these resolvers to make fixtures for your views.

Created Lookup context to handle the tracking of details… pulled out of ActionView::Base in rails 3. Mainly a code cleanliness item.

AV::Renderer now handles rendering templates

Top Down Comprehension worse Maintainabiliy better focuses comprehension much better extensibility possible

Dependency Inversion principle… depend on abstractions not on contretions. More appropriate for static languanges). In Java you end up using interfaces… in ruby we have duck typing

The following are the ways that rails achieves dependency inversion – define protocols – remove hardcoded dependencies – define hooks

Liskov Substition Principle - derived classes must be substitutable for their base classes. Harder in static languages. e.g. DataMapper vs ActiveRecord

ActiveModel specifies what models are going to be used by the controllers and view model_name persisted? valid? errors to_key to_param

How do we ensure sustitutability? ActiveModel::Lint::Tests

Interface Segregation - clients should depend on as narrow a protocol as possible. Ensure a narrow protocol

ActiveSupport 3 gem ruby facets is the precursor ActiveSupport::Configurable

Keeping Rails on the Track - Lindsaar

Ruby Patterns - Addison Wesley read it

Working Code? - not a good metric… light being green is a nice concept but not enough

Sexy Code - opposite end of the scale

Maintanable Code … has the right pattern applied, but not overly trying to be a case study

a product is only a product if it is exchanged – budget matters beautiful code is ugly if no one buys it – trade offs are life (code use) – you are not selling code (you are selling a solution running app are what counts)

Deployment is a first class Citizen

Document your commit should be trivial and documented.

chef/puppet/whatever - repeatable configuration

much easier to tell chef to rebuild postgres instead of trying to do it yourself

Use bundler

be explicit in your versions only lock to git repos that you own (fork it, and make a branch for that app)

Repeatable Deployment

Use common deployment methods

Use seeds

Make it easy for other developers (use seeds.rb) The other developer may be you.

Link in Security

Don’t store passwords inthe app Use some other datastore. Use the “app” gem for gathering data store.

Document your app

update the readme document what access you need for what machines, e.g. where you need to add ssh keys

For existing apps

Fast Page == Happy Client

  • page load times count combine javascript and css css sprites asset pipelining in rails 3.1

Caching Optimization Know and love rails view caching fragment cache action cache page cache

Big Session == Slow Site Session objects are a slippery slope to pain Store values not objects

Pick Your Fights don’ make a persisted session for every user

Opinions Matter

Remove Useless Code

Being Smart Can be Stupid

Explicit Methods

don’t use before after or around filters for object instantiation if it’s not authentication, authorization, or time zone settings then don’t do a filter filters are for state modification

Keep Tests Simple

Use explicit helpers.

Declare an expressive method such as given_a_logged_in_user and then call that in each block

Simple is better than Complex

what you leave iut is almost more important that what you put in

Good code is easy to read

Don’t reinvent stuff

Use the right tool for the right job

refactor… if it’s hard you’re doing it wrong

Databases have feelings too

You don’t need database agnostic code

Use the DB tools you have

Don’t share the DB

Refactor to API and CAS

Control flow is code smell

How to Stay on Track

This is probably the best talk I have seen yesterday

Maintainable code

Get an Inspection

http://rubyx.com/services/inspect @raasdnil

http://speakerrate.com/talk/7575 flog and flail

Duplicate Code Talk by D. Chleminsky

Code Smell: Duplicated Code

DRY Principle - not Don’t Repeat Yourself, but a piece of knowledge must have a single unambiguous authoratative representation

Every time you reduce duplication you increase coupling by increasing dependencies

Dependencies have to managed

The Dependency Inversion Priciple - depend on abstractions… but

weird in ruby because almost everything is an abstraction

Code is easier to understand when everything operates at a consistent

level of abstraction

DRY is about duplicated concepts and isolating change… not all

duplication is evil

So how you know if the duplicatoin is problematic.

Risk - might have to change things in multiple places and forget to

change it in one of the necessary places.

Law of Demeter… only talk to your closest neighbor and not have the

train wrecks of multiple object calls

Usage of before filter for assiging a resource seems to have fallen

out of favor…

Sass talk - Chris Eppstein

mix, lighten darken deaturate… lots of functions for

sprite support … define location of images use mixin, and it does

the rest

CSS3 mixins

Use CDN or asset hosts to make pages load faster

image-url helper from compass

presentational concerns into stylesheets… shouldn’t have to use class attribute to specify presentation use

additional compass mixins

how do you debug a sass file. it’s a problem.

@warn @debug , setting with, firesass plugin for firebug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment