Make your code beautiful
Why ?
- Style
- Syntax
Global Outline
Style
- Use 2 spacing indentation
- Use snake case
- use all caps on constant
- space around operator
- no messy space around (,[,{,},],),!,?,...
- comment wisely
- empty line between different logic
- indent wisely
- align element of array or hash
Syntax
- Prefer "unless" than if !
- Use one line "if" or "unless" when only has 1 line code
- avoid 1 single line method
- put . at the first line when chaining method goes too long
- use "each" instead of "for"
- use ternary if only has 1 line code
- don't mix hash rocket with literal hash
- prefer find instead of where(conditions).first
- use where.not (rails 4) instead of query comparation
- use understandable word
- remove dead code , don't comment it
Rails Outline
- config
- model
- controller
- view
- AR
- database migration
- I18n
- assets
- mailer and jobs
Config
- put constant and gem config in config
- add / remove assets that you want to precompile
- create staging.rb for staging
- use correct routes method (GET, POST, PUT, DELETE)
- use resource and nested resources wisely
- use collection and member for routes
Model
- Make it fat and clean
- separate to another method if it's too long
- DRY or make it as module
- use scope
Controller
- Make it thin and understandable
- Keep application controller clean, use it for global only
- Keep simple logic, if it's too complicated move to model
- use before_action
- follow REST
- use private and public method
View
- Do not change variable in view
- minimal or no logic
- move logic to helper
- separate from assets as much as possible
- use partial for shared views
- don't use too much partial it will slow down your rendering
AR
- Avoid altering default method
- Group your method e.g. validation, scopes, dll
- Use custom validator
- Avoid sql injection vulnerability (string interpolation)
- use find_each for big records
Database Migration
- Always use migration
- Add index if needed
- When it's deployed, don't up/down old migration
I18n
- Make it hierarcy
- Separate for each language
Assets
- Use your own asset grouping (don't require all)
- Use gemified version
- Precompile only css / js you need
Mailer and Jobs
- Name your mailer class with suffix Mailer
- Place your long process in background
- Sending email should be in background
- Style should be inline
That's all from me
Make your code beautiful
By Mukhammad Yunan Helmy
Make your code beautiful
- 594