Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rubocop-good-and-bad #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions rubocop/examples/bad/Style_AccessModifierIndentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 'https://github.com/bbatsov/ruby-style-guide#indent-public-private-protected'

# Indent the public, protected, and private methods as much as the method definitions they apply to.
# Leave one blank line above the visibility modifier and one blank line below in order to emphasize that it applies to all methods below it

# bad
class SomeClass
def public_method
# ...
end

private
def private_method
# ...
end

def another_private_method
# ...
end
end
5 changes: 5 additions & 0 deletions rubocop/examples/bad/Style_Alias.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 'https://github.com/bbatsov/ruby-style-guide#alias-method'

# TODO: Always use alias_method when aliasing methods of modules, classes,
# or singleton classes at runtime as the lexical scope of alias leads
# to unpredictability in these cases.
2 changes: 2 additions & 0 deletions rubocop/examples/bad/Style_AlignHash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# TODO: Here we check if the keys, separators,
# and values of a multi-line hash literal are aligned.
20 changes: 20 additions & 0 deletions rubocop/examples/bad/Style_AlignParameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# https://github.com/bbatsov/ruby-style-guide#no-double-indent

# Align the parameters of a method call if they span more than one line.
# When aligning parameters is not appropriate due to line-length constraints,
# single indent for the lines after the first is also acceptable.

# starting point (line is too long)
def send_mail(source)
Mailer.deliver(to: '[email protected]', from: '[email protected]',
subject: 'Important message', body: source.text)
end

# bad (double indent)
def send_mail(source)
Mailer.deliver(
to: '[email protected]',
from: '[email protected]',
subject: 'Important message',
body: source.text)
end
13 changes: 13 additions & 0 deletions rubocop/examples/bad/Style_AndOr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 'https://github.com/bbatsov/ruby-style-guide#no-and-or-or'

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

# bad
# boolean expression
if some_condition and some_other_condition
do_something
end

# control flow
document.saved? or document.save!
8 changes: 8 additions & 0 deletions rubocop/examples/bad/Style_ArrayJoin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 'https://github.com/bbatsov/ruby-style-guide#array-join'

# Favor the use of Array#join over the fairly cryptic
# Array#* with a string argument.

# bad
%w(one two three) * ', '
# => 'one, two, three'
9 changes: 9 additions & 0 deletions rubocop/examples/bad/Style_AsciiIdentifiers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 'https://github.com/bbatsov/ruby-style-guide#english-identifiers'

# Name identifiers in English.

# bad - identifier using non-ascii characters
заплата = 1_000

# bad - identifier is a Bulgarian word, written with Latin letters (instead of Cyrillic)
zaplata = 1_000
7 changes: 7 additions & 0 deletions rubocop/examples/bad/Style_Attr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 'https://github.com/bbatsov/ruby-style-guide#attr'

# Avoid the use of attr. Use attr_reader and attr_accessor instead.

# bad - creates a single attribute accessor
attr :something, true
attr :one, :two, :three # behaves as attr_reader
3 changes: 3 additions & 0 deletions rubocop/examples/bad/Style_BeginBlock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 'https://github.com/bbatsov/ruby-style-guide#no-BEGIN-blocks'

#TODO: Avoid the use of BEGIN blocks.
10 changes: 10 additions & 0 deletions rubocop/examples/bad/Style_BlockComments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 'https://github.com/bbatsov/ruby-style-guide#no-block-comments'

# Don't use block comments.
# They cannot be preceded by whitespace and are not as easy to spot as regular comments

# bad
=begin
comment line
another comment line
=end
18 changes: 18 additions & 0 deletions rubocop/examples/bad/Style_BlockDelimiters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 'https://github.com/bbatsov/ruby-style-guide#single-line-blocks'

# Prefer {...} over do...end for single-line blocks.
# Avoid using {...} for multi-line blocks (multiline chaining is always ugly).
# Always use do...end for "control flow" and "method definitions" (e.g.
# in Rakefiles and certain DSLs). Avoid do...end when chaining.

names = %w(Bozhidar Steve Sarah)

# bad
names.each do |name|
puts name
end

# bad
names.select do |name|
name.start_with?('S')
end.map { |name| name.upcase }
5 changes: 5 additions & 0 deletions rubocop/examples/bad/Style_BlockNesting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO: This cop checks for excessive nesting of conditional
# and looping constructs. Despite the cop's name,
# blocks are not considered as an extra level of nesting.

# TODO: The maximum level of nesting allowed is configurable.
2 changes: 2 additions & 0 deletions rubocop/examples/bad/Style_BracesAroundHashParameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# TODO: This cop checks for braces around the last parameter
# in a method call if the last parameter is a hash.
10 changes: 10 additions & 0 deletions rubocop/examples/bad/Style_CaseEquality.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 'https://github.com/bbatsov/ruby-style-guide#no-case-equality'

# Avoid explicit use of the case equality operator ===.
# As its name implies it is meant to be used implicitly by case expressions
# and outside of them it yields some pretty confusing code.

# bad
Array === something
(1..100) === 7
/something/ === some_string
17 changes: 17 additions & 0 deletions rubocop/examples/bad/Style_CaseIndentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 'https://github.com/bbatsov/ruby-style-guide#indent-when-to-case'

# Indent when as deep as case.
# This is the style established in both "The Ruby Programming Language"
# and "Programming Ruby".

# bad
case
when song.name == 'Misty'
puts 'Not again!'
when song.duration > 120
puts 'Too long!'
when Time.now.hour > 21
puts "It's too late"
else
song.play
end
8 changes: 8 additions & 0 deletions rubocop/examples/bad/Style_CharacterLiteral.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 'https://github.com/bbatsov/ruby-style-guide#no-character-literals'

# Don't use the character literal syntax ?x.
# Since Ruby 1.9 it's basically redundant - ?x would interpreted as 'x'
# (a string with a single character in it).

# bad
char = ?c
20 changes: 20 additions & 0 deletions rubocop/examples/bad/Style_ClassAndModuleCamelCase.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 'https://github.com/bbatsov/ruby-style-guide#camelcase-classes'

# Use CamelCase for classes and modules.

# bad
class Someclass
...
end

class Some_Class
...
end

class SomeXml
...
end

class XmlSomething
...
end
9 changes: 9 additions & 0 deletions rubocop/examples/bad/Style_ClassAndModuleChildren.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# TODO:This cop checks the style of children definitions at classes and modules.
# Basically there are two different styles:
# nested - have each child on its own line

# bad
class Foo
class Bar
end
end
1 change: 1 addition & 0 deletions rubocop/examples/bad/Style_ClassCheck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: This cop enforces consistent use of `Object#is_a?` or `Object#kind_of?`.
23 changes: 23 additions & 0 deletions rubocop/examples/bad/Style_ClassMethods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#def-self-class-methods'

# Use def self.method to define class methods.
# This makes the code easier to refactor since the class name is not repeated.

class TestClass
# bad
def TestClass.some_method
# body omitted
end

# Also possible and convenient when you
# have to define many class methods.
class << self
def first_method
# body omitted
end

def second_method_etc
# body omitted
end
end
end
8 changes: 8 additions & 0 deletions rubocop/examples/bad/Style_ClassVars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 'https://github.com/bbatsov/ruby-style-guide#no-class-vars'

# TODO: Avoid the usage of class (@@) variables due to their "nasty"
# behavior in inheritance.

# As you can see all the classes in a class hierarchy actually
# share one class variable.
# Class instance variables should usually be preferred over class variables.
5 changes: 5 additions & 0 deletions rubocop/examples/bad/Style_CollectionMethods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO: This cop checks for uses of unidiomatic method
# names from the Enumerable module.

# TODO: The current definition of the check is flawed and should be enhanced
# by check for by blocks & procs as arguments of the methods.
9 changes: 9 additions & 0 deletions rubocop/examples/bad/Style_ColonMethodCall.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 'https://github.com/bbatsov/ruby-style-guide#double-colons'

# Use :: only to reference constants(this includes classes and modules) and
# constructors (like Array() or Nokogiri::HTML()).
# Do not use :: for regular method invocation.

# bad
SomeClass::some_method
some_object::some_method
4 changes: 4 additions & 0 deletions rubocop/examples/bad/Style_CommentAnnotation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 'https://github.com/bbatsov/ruby-style-guide#annotate-keywords'

# TODO:The annotation keyword is followed by a colon and a space,
# then a note describing the problem.
1 change: 1 addition & 0 deletions rubocop/examples/bad/Style_CommentIndentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: This cops checks the indentation of comments.
6 changes: 6 additions & 0 deletions rubocop/examples/bad/Style_ConstantName.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 'https://github.com/bbatsov/ruby-style-guide#screaming-snake-case'

# Use SCREAMING_SNAKE_CASE for other constants.

# bad
SomeConst = 5
15 changes: 15 additions & 0 deletions rubocop/examples/bad/Style_DefWithParentheses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 'https://github.com/bbatsov/ruby-style-guide#method-parens'

# Use def with parentheses when there are parameters.
# Omit the parentheses when the method doesn't accept any parameters.

# bad
def some_method()
# body omitted
end

# bad
def some_method_with_parameters param1, param2
# body omitted
end

9 changes: 9 additions & 0 deletions rubocop/examples/bad/Style_DeprecatedHashMethods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 'https://github.com/bbatsov/ruby-style-guide#hash-key'

# Use Hash#key? instead of Hash#has_key? and Hash#value?
# instead of Hash#has_value?. As noted here by Matz,
# the longer forms are considered deprecated.

# bad
hash.has_key?(:test)
hash.has_value?(value)
4 changes: 4 additions & 0 deletions rubocop/examples/bad/Style_Documentation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TODO: This cop checks for missing top-level documentation
# of classes and modules. Classes with no body are exempt from the check and
# so are namespace modules - modules that have nothing
# in their bodies except classes or other other modules.
20 changes: 20 additions & 0 deletions rubocop/examples/bad/Style_DotPosition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 'https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains'

# Adopt a consistent multi-line method chaining style.
# There are two popular styles in the Ruby community,
# both of which are considered good - leading . (Option A) and
# trailing . (Option B).

# (Option A) When continuing a chained method invocation
# on another line keep the . on the second line.

# bad - need to consult first line to understand second line
one.two.three.
four

# (Option B) When continuing a chained method invocation on another line,
# include the . on the first line to indicate that the expression continues.

# bad - need to read ahead to the second line to know that the chain continues
one.two.three
.four
9 changes: 9 additions & 0 deletions rubocop/examples/bad/Style_SelfAssignment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 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
7 changes: 7 additions & 0 deletions rubocop/examples/bad/Style_Semicolon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Don't use ; to separate statements and expressions.
# As a corollary - use one expression per line.

# bad
puts 'foobar'; # superfluous semicolon

puts 'foo'; puts 'bar' # two expressions on the same line
4 changes: 4 additions & 0 deletions rubocop/examples/bad/Style_SignalException.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TODO: This cop checks for uses of `fail` and `raise`.

# TODO: 'Use `fail` instead of `raise` to signal exceptions.'
# TODO: 'Use `raise` instead of `fail` to rethrow exceptions.'
5 changes: 5 additions & 0 deletions rubocop/examples/bad/Style_SingleLineBlockParams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO: This cop checks whether the block parameters of a single-line method
# accepting a block match the names specified via configuration.

# TODO: For instance one can configure `reduce`(`inject`) to use |a, e|
# as parameters.
16 changes: 16 additions & 0 deletions rubocop/examples/bad/Style_SingleLineMethods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Avoid single-line methods. Although they are somewhat popular in the wild,
# there are a few peculiarities about their definition syntax
# that make their use undesirable. At any rate - there should be no
# more than one expression in a single-line method.

# bad
def too_much; something; something_else; end

# okish - notice that the first ; is required
def no_braces_method; body end

# okish - notice that the second ; is optional
def no_braces_method; body; end

# okish - valid syntax, but no ; makes it kind of hard to read
def some_method() body end
22 changes: 22 additions & 0 deletions rubocop/examples/bad/Style_SpaceAfterColon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use spaces around operators, after commas, colons and semicolons,
# around { and before }. Whitespace might be (mostly) irrelevant to the
# Ruby interpreter, but its proper use is the key to
# writing easily readable code.

sum = 1 + 2
a, b = 1, 2
[1, 2, 3].each { |e| puts e }
class FooError < StandardError; end
The only exception, regarding operators, is the exponent operator:

# bad
e = M * c ** 2

# { and } deserve a bit of clarification, since they are used for block
# and hash literals, as well as string interpolation.
# For hash literals two styles are considered acceptable.

# The first variant is slightly more readable (and arguably more
# popular in the Ruby community in general). The second variant has
# the advantage of adding visual difference between block and hash literals.
# Whichever one you pick - apply it consistently.
1 change: 1 addition & 0 deletions rubocop/examples/bad/Style_SpaceAfterComma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: Checks for comma (,) not followed by some kind of space.
5 changes: 5 additions & 0 deletions rubocop/examples/bad/Style_SpaceAfterMethodName.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Do not put a space between a method name and the opening parenthesis.

# bad
f (3 + 2) + 1

Loading