Monday 5 May 2014

Ruby on Rails - Some practices to avoid

Rails Web development
The web framework of Ruby on Rails is open source and optimized so that sustainable productivity is inspired in programmers. It favours convention instead of configuration so that beautiful code can be written. However, those who are new to the framework find that there are some preferred ways to accomplish a task given; knowing about them is absolutely imperative since otherwise a lot of problems crop up. If you know how to handle a particular task and how to approach it, then the task becomes really smooth; then you will be able to write clean, more secure as well as maintainable codes. In order to write the best codes, here are some practices that you must always avoid.

Do not Rescue everything- The individual controller actions should not be embedded with rescues. Some edge cases may require stuff to be rescued specifically during actions but the entire controller should not be assaulted by rescue. In fact, a rescue must always be attached to individual exception as otherwise all exceptions are caught and then useful plugins do not get called. It leads to no generation of back traces which results in a scaffolding of handling error and then finding of collapses, leaving us with an inconsistently failing mess.

No overriding of existing methods- This must be avoided like the plague. An existing method mustnever be overridden unless the existing functionality is mimicked completely along with tests to prove the authenticity. If there is a compelling reason for you to do this then the resulting method must be put in place that is very clear; it should be documented extensively as well. It should not be put in somewhere bizarre such as a plugin; if the code fails then you would have to research through a big winding backtrace to find the problem.

The controller must not be long- The controller should in no conditions, be more than 1000 lines long. For 100 actions there must be at least 10 individual controllers; 1 huge one should not have them all. If the controller has 5 actions then whatever goes into every one of the actions should be reconsidered.

Validations should not be in the controller- Nothing should ever be validated in a controller; there can be no excuse for that. For validations there are helper methods in the moles which are quick as well as easy to use.

The params hash should never be modified- All data submitted from request is contained by params hash. In case of modifications, it cannot be accessed by later codes. Instead the params hash must be copied and the copy should be modified.

Avoid using time_ago_in_words- Rails developers often use time_ago_in_words so that time such as “4 minutes ago” may be displayed. However when the server side attempts to calculate the time it becomes too expensive. So client CPU must be used for time ago calculation.

Kill chubby controllers- A controller should not have any business logic. Remember that there are just two functions of controllers- taking data from session or params to send to model and then deciding what the user should see. The necessary logic is performed by the model. Including anything else in the action is simply overdoing.

Avoid copying and pasting huge swatches of CSS and code in a view- When copy pasting the code, make sure to paste it into a helper that is easily accessible so that if somebody has to change the code’s instances, it can be done in one place. Even in case of CSS, you surely do not want to confuse people with its size as during modification it will be a tiresome task, so avoid copying out huge amounts of CSS.

There are still plenty of places where you might go wrong while coding. The only thing to do is to carefully consider everything and then assign specific roles very strictly. The conventions as well rules must be taken very seriously. Make sure that the final output is tested thoroughly so that there is little room for errors. You can get in touch with a ruby on rails development company who can help you web develop apps that are stable, scalable and secure.

we provide Rails web development service. If you would like to hire Ruby on Rails developers for your development needs, please contact Mindfire Solutions.