Skip to content

Instantly share code, notes, and snippets.

@ParthivPatel-BTC
Created October 14, 2016 10:23
Show Gist options
  • Save ParthivPatel-BTC/2f996ca7387b558b8d2b345f13fbc36c to your computer and use it in GitHub Desktop.
Save ParthivPatel-BTC/2f996ca7387b558b8d2b345f13fbc36c to your computer and use it in GitHub Desktop.
1.DRY - Dont' Repeat Yourself
2.Two spaces, no tabs
3.Boolean Tests: don't use "and" and "or", always use "&&" and "||"
Comments
--------
1.Remove old commented code
2."How to" comments - If you add/change any method/variables etc.., always add detailed description in just above line. So, anybody can understand that code.
Camels for Classes, Snakes Everywhere Else
------------------------------------------
1.Snake case - lowercasse_words_separated_by_underscore
2.Camel case:
ClassName - good
Class_name - bad
3.Constants:
ALL_UPPERCASE = true
Parentheses (Optional)
----------------------
1.def find_customer(id, name)
# body
end
or
def find_customer id, name
# body
end
2.Do & Don't
# Don't do this
def split_message()
@msg.split() if (@msg.size > 140)
end
# Leave parentheses
def split_message
@msg.split if @msg.size > 140
end
Conditions
----------
if not @user
message.title = new_title
end
# More Idiomatic way
unless @user
message.title = new_title
end
# Avoid negated conditions
while ! @printer.ready?
...
end
# More ideomatic way
until @printer.ready
...
end
# Change
if request.xhr?
render :index, layout: false
else
render :index
end
# to
render :index, layout: !request.xhr?
Each loop
---------
colors = ['green', 'blue', 'pink']
# Don't do this
for color in colors
puts color
end
# use each
colors.each do |color|
puts color
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment