5 Jun
Obie Fernandez: Ruby coding best practices
Through examples e fragments of code Obie bestow the practices to avoid when developing in Rails.
Obie: You need to know the basis of ruby before you can declare yourself a Rails developer. David Black has published a book for those who would like to gain experience ruby knowing only Rails.
Those who do not discern Ruby can construct a very complicated construction when you could application ‘shortcut’ offered by Ruby; we don’t reinvent things that Ruby has already created and optimized. (for example in that place exists a function include? to govern if an element is in an array without creating a cycle).
It is momentous to use the routes and the dynamic associations
On the screen appeared a method that before carries out a query on all the photos to choose a random id and retrieve with one more query that photo, clearly not a good programming example.
At general condition of affairs the laziness easily takes the programmer to frame numerous misprints.
It is important to use various methods that follow simple actions, not to create methods that make many diverse things!
The speaker in front now ironically defines some errors “Premature DeOptimization” that are centered on generating digest that decreases the something done of the application, also to generate actions directly in the ApplicationController is not a good idea.
Abuse of the database, happens when we use without too a great deal of reflection the functionality of ActiveRecord (like for precedent we use with light association dynamically inside the cycle).
Also to use the cache in a way that is wrong and dangerous. You don’t cache anymore putting the data inside the global constants/variables in how much it finishes all the RAM.
Don’t create controllers that are too long.
On the screen appeared a long action (about a hundred lines) that at the end of the process has ‘return true’, applauding and laughs came from the room. It is important that you encapsulate all the logic of the business in the models following the thin controller principle, adipose models.
We must also be careful to maintain the code created always readable, not to be tempted to “compress” the collection of laws too much.
The RESTful science must be used correctly (upon the body the veil compiles a controller attached to a shopping situation with various procedures, that are for nothing RESTful)
We need to always try to exercise divergent plugins that make the sort thing to understand which is the most functional and production the most expedient. see the various meanings of good.
When the HTML helper degenerates: on the screen appeared a helper to produce a frame and inside a photo, the auxiliary is hundreds of rows long and the speaker shows for that which cause difficult it is to maintain a function of this token. In this case what does it verify, says the speaker, it is utility to discern that it is needed to generate a just discovered level of extraction to manage the problem (for example to create a ad-hoc model).
We create a methods with constant names, that are most possibly self explanatory (on the screen appears a method that is called finderer that returns a set of options for a method find, difficult to extrapolate independently of reading the comments).
We cancel the commented code, in any case we use SVN or GIT that keeps track of these things….
On the screen appears the UsersController, an complete application all in a controller, the curse of best practice, applauding and laughs came from the audience.
Conclusions
- learn the fundamentals (of Ruby, of Rails) Rails is not a magic language that makes programmers those who don’t know, it is faster to establish on the conventions end it is necessary to know how to use them!
- learn to learn from lower classes that know more than us, if it is maybe possible to program together with race (pair programming). (the best would, says Obie would be to take SmallTalk programmers with years of experience)
- Read books about the subject.
Annalisa
