Skip to content

Instantly share code, notes, and snippets.

@adacosta
Created July 26, 2010 17:47
Show Gist options
  • Save adacosta/490910 to your computer and use it in GitHub Desktop.
Save adacosta/490910 to your computer and use it in GitHub Desktop.
# 1. Use two spaces instead of tabs
# 2. Avoid using 'return' when not necessary. It's unnecessary.
# before
def current_user
return session[:user]
end
# after
def current_user
session[:user]
end
# 3. Review code before pushing commits, first git pull, then review with: git diff hash_id..hash_id, or git diff origin/master..master
# 4. Create a topic branch (branch with descriptive name) instead of committing broken or (exposed) incomplete code.
# If you have code you would like to checkpoint, you can commit into a feature branch.
# 5. Avoid committing files with whitespace only changes, call git checkout path/to/file to reset changes you've made.
# 6. Don't use shortened variable names (e.g. while iterating surveys, each iterator should be a survey rather than an svy)
# more examples: performance_measures_table vs pm_table, project_survey_score vs pss.
# Variable naming should be verbose, not brief.
# Longer variable names are much easier to recognize, and your code will be read much more than written / extended.
# Avoid specifying variable type in names unless absolutely required by some wierd edge case.
# 7. Use do |var|; end blocks rather than mustache blocks {|var| } for multi-line implementations. This is ruby convention.
# We want a consistent style which makes it easy to change gears between different libraries, etc.
# before
items.each do |item|
end
# after
items.each {|item| }
# 8. Refrain from over-casting vars, e.g. some_var.to_i == some_var.to_i
# 9. Attempt to use regexes rather equality matches for multiple possible values,
# e.g. value == 'string' vs. value[/string/] or value =~ /string/ (because it's easy to exand the regex version).
# This helps when several items later need to be compared.
value = 'radio'
# before
value == 'radio' | value == 'text'
# after
value[/radio|text/]
# or
value =~ /radio|text/
# 10. Instead of naming methods with get_/set_, use method_accessor for getters and method_accessor= for setters
# before
def get_survey_scores; end
def set_survey_scores(value); end
# after
def survey_scores; end
def survey_scores=(value); end
# 11. Be aware, code which follows repetitive patterns is a code smell. Refactor!
# 12. Try to comment code for what it does, not what it is (not always possible, but consider it).
# before
question_ids = [1,2,3,4]
# user questions
show question_ids
# after
question_ids = [1,2,3,4]
# show baseline_survey questions
show question_ids
# this can still be improved (format can be described, etc.)
# 13. Learn from other large, successful projects like: rails, rack, etc. Don't be afraid to dig into source code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment