AUTOMATIC.<%= form_for @cash_transfer do |f| %> <%= f.text_field :from %> <%= f.text_field :to %> <%= f.text_field :amount %> <%= button "Transfer!" %><% end %> Enter Text here
Conventions Tools Hand-RolledConventions allow you to avoidthinking about the problem at all whileworking on features.Tools make the problem easier to solve,but still make you think about it.When we dont have a convention forsomething, we end up forcing thedeveloper to learn about the problemand choose a solution, which is most ofthe cognitive overhead of solving ityourself. Cognitive Overhead
The common conventions of Railsapplications provide a foundation foradditional abstractions.
COURSE CORRECTIONSNo interest in cosmetic, no time forrefactoring.Interested in figuring out why myclients dont want me to use Rails.
MANY PEOPLE HAVE A PROBLEM.Criteria for course corrections
MANY PARTS OF THESOLUTION ARETRIVIAL.REST: What HTTP structure should I use? What names should I give my methods? How should I generate URLs?
THERE IS BENEFIT IN ASHARED SOLUTION. Asset pipeline: * Works out of the box * Reduces cost of entering a project * Concept of "Rails asset processor"
LEARNING ABOUTTHE PROBLEM ISHARD. Encodings, Security, ETags
FAILURE TO SOLVEIN RAILS CORE. What are the problems for failing to provide solutions in Rails core. Flip side of what I just said.
CRITICAL MASS. Having multiple competing solutions robs any one solution of the critical mass it needs to rapidly improve. No initial implementation is perfect, and Rails provides eyeballs and hands.
Its tempting to say "let this feature be a plugin, and well let our users flesh it out before we include it"USAGE. This often results in several competing solutions, each languishing with few users, all of which have major issues. The lack of usage can also hide conceptual problems with an approach (heroku deployment?)
INTEGRATION. When users pick a solution on their own, they are willing to put up with a lot of manual work. The process of integrating a feature into Rails to automate it often brings fundamental problems to light.
ECHO CHAMBER.The people most likely to choose a plugin solution to aproblem also understand it the most. This means thatthere is little impetus to provide completely transparentsolutions that work without complete (or any) knowledgeof the underlying problems. By making something a Railsconcern, it immediately busts the echo chamber of thosewho already understand the problem.
TECHNICAL DEBT. As Aaron said yesterday, when solving these larger issues, we have a larger tolerance for technical debt. That said, we shouldnt throw caution to the wind and take on unlimited debt. At this point, were still paying back our last emergency loan, so lets be more prudent now.
GOOD ARCHITECTUREENABLES FUTUREFEATURES. Architecting well means that we can easily improve the feature in the future. Many of the good things in the Rails 3 architecture (notifications) have only come to fruition now.
FEATURES NOW + FEATURES LATER ÷ COST NOW + MAINTENANCE
FEATURES NOW + FEATURES LATER ÷ COST NOW + MAINTENANCE Building a feature right lets us extend it easily in the future with lower maintenance costs. This allows us to make necessary investments more easily in the future.
BETSWhen we correct course, we areplacing a bet about the future of ourindustry.
BEFORE RAILS.■ Primary key: chosen per table■ Table name: chosen per table■ Timestamps: chosen per timestamp■ Polymorphic tables: ad hoc setup■ Foreign keys: chosen per relationship■ Camel/Underscore: chosen per field
Because of all of these discrepancies,you end up needing to build a map ofyour database for your application.
"DATABASES ARETOO DIFFERENT" In the early days of Rails, we heard a lot of arguments that databases were simply too different for AR to be more than just a toy. We hear similar arguments today with APIs.
JSON APIS.■ Should responses include a root?■ How to embed associations?■ Include the identifier? In what form? ■ id or href?■ Include additional/associated resources?■ How to update in bulk?■ Primary key defined on server or client?■ Moving a belongs_to record? Just like there were questions in SQL, there are questions in JSON APIs.
STANDARDIZEDCLIENT CODE. Without standardization, we cannot easily build standardized code on the client.
Were also back to making the sametrivial decisions over and over again,if we even realize that we aremaking decisions.
And because were not taking on theproblem, were pushing the concernsonto every client.
Since Rails doesnt provide these conventions, people are asking "Is Rails the right tool for the job" Even though other tools dont provide conventions, Rails is all about CoC, so the lack of conventions makes *Rails* feel like the wrong tool for the job, even though much of Rails is still useful.IS RAILS WORTH IT? Rails starts feeling more like a very polished library and less like a framework.
The advantage of serializers is that it avoids mixing the common (representation) with the unique (attributes, associations)AVOID MIXINGCOMMON ANDUNCOMMON. This is in contrast with builders, which put the common and unique things in one place, so we have no place for conventions.
CONFIGURABLE. Especially for authorization, there is a need to be able to poke under the declarative hood. Its not all documented, but it works.
AUTHORIZATION.class PostSerializer < ApplicationSerializer attributes :title, :body has_many :comments has_many :troll_ratings # override just default association inclusion def include_associations! comments = post.comments unless scope.can(:see_hidden, post) comments = comments.where(visible: true) end # override default value, but not embedding rules, etc. include! :comments, value: comments # conditionally include an association include! :troll_ratings if scope.can(:troll_rate, post) endend
“ Back in 1995, we knew something that I dont think our competitors understood, and few understand even now: when youre writing software that only has to run on your own servers, you can use any language you want. When youre writing desktop software, theres a strong bias toward writing applications in the same language as the operating system. But with Web-based software, you can use whatever language you want.PAUL GRAHAM
“ This new freedom is a double-edged sword, however. Now that you can use any language, you have to think about which one to use. Companies that try to pretend nothing has changed risk finding that their competitors do not.PAUL GRAHAM
TRANSPORT. Standards and Same Language Conventions Everywhere• HTML • JMS• ActiveRecord • DRb Thinking "I need to talk with JS, therefore I need to write JS on the ser ver" is pretty lazy thinking. We can get seamlessness without insisting on the same language everywhere.
TRANSPORT. Standards and Same Language > Conventions Everywhere• HTML • JMS• ActiveRecord • DRb Thinking "I need to talk with JS, therefore I need to write JS on the ser ver" is pretty lazy thinking. We can get seamlessness without insisting on the same language everywhere.
RECAP.■ Good conventions save developers from having to agonize over common problems.■ Rails bet on REST gave it a leg up in many areas that are relevant to rich client apps.■ There is still room for improvement: we can make APIs as transparent as ActiveRecord.■ ActiveModel::Serializers is one approach we are looking at to get us there.■ We can also make browser frameworks better through the same conventions.