Skip to content

Latest commit

 

History

History
155 lines (100 loc) · 4.1 KB

coding_conventions.md

File metadata and controls

155 lines (100 loc) · 4.1 KB

Coding Conventions

THIS IS NOT A FULL GUIDE. JUST A GENERAL GUIDE FOR PURE BEGINNERS

A full set of coding conventions can be found at bbatsov's writeup on Github here

Curly Braces vs do...end

Most style guides and best practices state that:

  • curly braces {} should be used for one liners
  • and do...end should be used for multiline statements

There is also a subtle difference between the two:

Braces have a high precedence; do has a low precedence. If the method invocation has parameters that are not enclosed in parentheses, the brace form of a block will bind to the last parameter, not to the overall invocation. The do form will bind to the invocation

The examples below make it more clear:

This:

task :rake => pre_rake_task do
  something
end

Really means:

task(:rake => pre_rake_task){ something }

And this with curly braces:

task :rake => pre_rake_task {
  something
}

really means:

task :rake => (pre_rake_task { something })

See Stackoverflow here for more details

Naming convention

Unlike JS where many developers use camelCase for their variable naming conventions, in Ruby:

  • For plain variable names: use the snake_case convention.
  • For file and directory names, use the snake_case convention as well.
  • For constants, use the SCREAMING_SNAKE_CASE convention.
  • For Classes and Modules, use the CamelCase convention.

Naming functions

Functions that return booleans

For functions that return a boolean value, the name should be suffixed with a ?. For example:

str.include? "substring"

Functions that are setters

In other languages like Java or C#, if we have an attribute name, and we have to set the value of that attribute, then the setter function would be named setName.

Ruby allows = in method names so the convention is to suffix the = with the attribute. So in this case, it would be:

def name=(value)
  @name = value
end

Terse-tastic!

For future reference, getter and setter functions are called reader and writer functions respectively in Ruby.

USE attr_accessor/attr_writer INSTEAD OF MANUALLY HAVING A SETTER

Most of the time, if this is a common instance variable, you do not need to set up your ...

Strings

String Concatenation vs String Formatting and Interpolation

#bad
email_with_name = user.name + ' <' + user.email + '> '
#good (with string interpolation)
email_with_name = "#{user.name} <#{user.email}>"
#good (with string formatting)
email_with_name = format('%s <%s>', user.name, user.email)

Parentheses

In Ruby, parentheses are technically optional but there are times when it should be used to make the code more readable.

According to the unofficial Ruby style guide, you do NOT use parentheses when:

  • It involves control flow statements (if-then, unless etc.)
  • It's a class, function or uses any language specific "keywords" words like puts or attr_reader.

You use parentheses for all other method invocations.

From the aforementioned style guide:

class Person
  attr_reader :name, :age

  # omitted
end

temperance = Person.new('Temperance', 30)
temperance.name

puts temperance.age

x = Math.sin(y)
array.delete(e)

bowling.score.should == 0

Hashes

Omit the {} when being called as the last parameter of a function:

foo({ :key => 'value' })     # bad. Its unnecessary.
foo(:key => 'value')         # good.
bar(32, :key => 'value')     # good
baz({ :key => 'value' }, 32) # good. The hash is not the last parameter so {} is needed

Chaining functions

Functions can easily be chained as long as the reference to the class using self is returned from the functions

See here: