-
Notifications
You must be signed in to change notification settings - Fork 0
Coding guideline points
- Look for areas in a code block if it can be written compactly once you have finished coding the logic.
- Practice and ownership to make the code better are primary things.
-
DRY - Don't Repeat Yourself. As soon as, the code starts repeating second time, abstract it out in a method and use the method
-
Use local variable to hold the value of Jquery selector and use it if needed multiple times.
//bad jQuery(‘#id’).css('..', ..) .... .... jQuery(‘#id’).html('..') //good var elem = jQuery('#id') elem.css('..', ..) .... .... elem.html('..')
-
Omit use of unnecessary if/unless. In the following cases, if statement is unnecessary
#empty array will not execute anything if arr.present? arr.each do |obj| …… end end
-
Limit the usage of instance variables (specially for rails, usage mainly for views/partials which is in controller)
-
Use consistent spacing (between operators, comma etc.)
-
Use consistent indentation preferably 2 spaces.
-
In Javascript,
-
if string
is sufficient. No need forif string != ''
-
if array.length
is sufficient. No need forif array.length == 0
-
-
Do not use unnecessary intermediate variables which are not used in a function.
-
In javascript
//unnecessary usage of variable value function test(){ var value = jQuery('#input-id').val(); return value; }
-
In ruby
#unnecessary usage of variable val def org_status(user) status = user.organization.status end
-
-
In ruby, prefer using
each, map, select, each_with_index
instead offor
(preference tomap/select/reduce
etc. if the work can be done by them) -
Discourage the usage of
eval
. Instead usesend
wherever necessary. -
If you are looking for nil and there is any possibility of false turning up, then look for nil explicitly. (by
.nil?
Or== nil
) -
use
unless
anduntil
in place ofif not
andwhile not
respectively. -
use modifier form if single statement (ternary operator or single statement if or unless etc.)
-
While using methods like which return
true/false
likepresent?
,empty?
in conditional statements, omit usage of== true
.if arr.present?
is sufficient. -
Use constants for static data in a class in ruby
-
Take care of scope of variables and avoid using global variables
-
Some useful references/links for more information