Rails Best Coding Practices

With the help of these coding practices, One can learn the best coding practices.

Rails Best Coding Practices

Writing well-formatted and readable code holds equal importance as writing bug-free code. No matter how good a programmer you are, if you don't write organized and formatted code, you will always leave a bad impression on other people looking at your code. Formatting gives the first impression of your code and your coding skills. In this article, I am going to talk about some of the coding practices that will help you in writing organized code in the Rails app.

Use Rails Generators while creating a new model, controller or mailer

Instead of manually creating the controller/model/mailer files, I always prefer to generate them using rails generator commands. The biggest benefit of using generator commands is all the related files are automatically generated in one single command, e.g the test suite files, thereby saving time. This helps us to standardize our code and enables our developers to be more efficient, since we don't have to manually create every single file. For example, running rails generate controller users would generate the following files 

Use meaningful methods and variable names

Always choose the method and variable names such that the name itself reveals the intention of the method or variable. For example, we are creating a method that returns. Choose a small and descriptive name for your methods and variables.

Define Predicate Methods with a ?

Predicate methods are the methods returning true or false. Adding a ? at the end of the predicate method name makes it clear that the method returns true or false. For instance, a method describing whether an order is paid or not would be named as.

order.paid?

Use spaces instead of tabs

Tabs might take a different number of columns depending on your environment, whereas space is always equal to one column. Hence space allows your code to have the same indentation in every environment, whereas tabs may lead to noisy visual whitespaces. You can set your editor settings to convert the tabs to spaces automatically. For example in sublime, go to preferences -> settings - Syntax Specific and add the following lines to ruby. sublime-settings.

Do not Repeat Yourself(DRY)

Avoid duplication of code as much as you can. Extracting duplicate code to a single place enhances the maintainability and readability of the code. It saves a lot of time as changes are required to be done in a single place. One of the best ways to DRY up your code in rails application is with the use of Concerns.

Concerns are nothing more than modules that helps to extract and DRY up your code. Place your repeating logic inside concerns and include the concerns wherever you want to use the logic.

Conclusion

It enhances your coding experience if you organize your code well and follow the best practices defined for your framework. Moreover, it is always going to save time when you are working in a team. So write clean and well-formatted code following proper naming conventions. Happy Coding!