Coding Standards of Ruby Watir

in #ruby7 years ago (edited)

LOOPS:

  1. Never use unless with else. Rewrite these with the positive case first.
    BAD
    unless success?
    message = 'FAIL'
    else
    message = 'PASS'
    end

GOOD
if success?
message = 'PASS'
else
puts 'failure'message = 'FAIL'
end

  1. Use unless instead of if for negative conditions.
    BAD
    unless x == 10
    ...
    end

GOOD
if x != 10
...
end

BAD
unless x < 10
...
end

GOOD
if x >= 10
...
end

  1. Don't use parentheses around the condition of an if/unless/while.
    BAD
    if (x > 10)
    ...
    end

GOOD
if x > 10
...
end

  1. Prefer next in loops instead of conditional blocks.
    BAD
    [0, 1, 2, 3].each do |item|
    if item > 1
    puts item
    end
    end

GOOD
[0, 1, 2, 3].each do |item|
next unless item > 1
puts item
end

STATEMENTS:

  1. The and, or, and not keywords are banned. It's just not worth it. Always use &&, ||, and ! instead.

  2. Do not include space inside block parameter pipes. Include one space between parameters in a block. Include one space outside block parameter pipes.
    BAD
    {}.each { | x, y |puts x }

GOOD
{}.each { |x, y| puts x }

  1. Don't use extra whitespace in range literals.
    BAD
    (0 ... coll).each do |item|

GOOD
(0...coll).each do |item|

  1. Omit parentheses for a method call if the method accepts no arguments.
    BAD
    nil?()

GOOD
nil?

  1. Use shorthand self assignment operators whenever applicable
    BAD
    x = x + y
    x = x * y
    x = x**y
    x = x / y
    x = x || y
    x = x && y

GOOD
x += y
x *= y
x **= y
x /= y
x ||= y
x &&= y

STRINGS:

  1. Omit whitespace when doing string interpolation.
    BAD
    var = "This #{ foobar } is interpolated."

GOOD
var = "This #{foobar} is interpolated."

  1. Don’t use Object#to_s on interpolated objects. It’s invoked on them automatically.
    BAD
    message = "This is the #{result.to_s}."

GOOD
message = "This is the #{result}."

GENERAL:

  1. Never leave commented-out code in our codebase.

  2. Use snake_case for naming files and directories

  3. Use block comments if comments are more than 2 lines:
    BAD

1st comment line

2nd comment line

3rd comment line

GOOD
=begin
1st comment line
2nd comment line
3rd comment line
=end

FUNCTIONS:

  1. Function should not accept more than 3 parameters.
    More than 3 values should be sent in hash.
    BAD
    def obliterate(things, gently = true, except = [], at = Time.now)
    ...
    end

GOOD
def obliterate(things, options = {})
options = {
:gently => true, # obliterate with soft-delete
:except => [], # skip obliterating these things
:at => Time.now, # don't obliterate them until later
}

...

end

  1. A function can return only 2 values. If more than 2 values needs to be returned, use hash

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.029
BTC 75904.17
ETH 2906.60
USDT 1.00
SBD 2.64