Refactoring checklist for beautiful Ruby code
Tags: ruby, codequality, webdev, beginners
Published: 2020-12-14 21:46:38

![checklist](https://dev-to-uploads.s3.amazonaws.com/i/7eukyla6bzmtx9csxhnw.png)
You should never underestimate the importance of refactoring. If you're new to Ruby, you might be writing longer or more complex code than it should be without knowing how to use Ruby's convenient syntax. Since I learned basic Ruby at a coding bootcamp, I have been trying to learn from the code of experienced developers and realized that my code was much more verbose and thus confusing. In this article, I will introduce some techniques I came across to make Ruby code more readable, less expensive, and easy to change. 

##Conditional expressions##

###1. Use `unless`###
This might depend on your preference.
```ruby
# Bad
user.destroy if !user.active?

# Good
user.destroy unless user.active?
```
***
###2. Ternary operator `?:`###
```ruby
# Bad
if score > 8
  result = "Pass"
else
  result = "Fail"
end

# Good
result = score > 8 ? "Pass" : "Fail" 
```
***
###3. Avoid `== true`, `== false`, and `== nil`###
```ruby
# Bad
if foo == true
  # something
end

# Good
if foo
  # something
end
```
```ruby
# Bad
if bar == false
  # something
end

# Good
if !bar
  # something
end
# or
unless bar
  # something
end
```
```ruby
# Bad
if baz == nil
  # something
end

# Good
if !baz
  # something
end
# or
unless baz
  # something
end
```
Or, if you need to check if the value is specifically `nil` and not any other falsey value, this might be a better option:
```ruby
# Good
if baz.nil?
  # something
end
```
***
###4. One-line `if` statement###
```ruby
# Bad
if num > 5
  puts "#{num} is greater than 5"
end

# Good
puts "#{num} is greater than 5" if num > 5
```
***
###5. [Safe navigation operator `&.`](https://ruby-doc.org/core-2.6/doc/syntax/calling_methods_rdoc.html#label-Safe+navigation+operator) (after Ruby 2.3.0)###
The safe navigation operator `&.` allows skipping method call when the receiver is `nil`.

In the example below, if `book.author` or `book.author.name` returns `nil`, you need to check that to avoid `NoMethodError` exception.
```ruby
# Bad
if book.author
  if book.author.name
    puts "#{book.title} was written by #{book.author.name}"
  end
end
```
If you use the safe navigation operator, this can be achieved with fewer lines of code.
```ruby
# Good
if book.author&.name
  puts "#{book.title} was written by #{book.author.name}"
end
```
***
###6. Use `||=` for conditional initialization###
```ruby
# Bad
book = Book.new if book.nil?

# Good
book ||= Book.new
```
***
##Array##

###7. Use `%w()` or `%i()` when create an array ([Percent strings](https://docs.ruby-lang.org/en/2.0.0/syntax/literals_rdoc.html#label-Percent+Strings))###
I would say using percent strings is a better way because it will prevent you from forgetting quotation mark or colon and making errors.
```ruby
# Bad
genres = [ "fantasy", "adventure", "mystery", "romance" ]

# Good
# String
genres = %w(fantasy adventure mystery romance)
# =>[ "fantasy", "adventure", "mystery", "romance" ]

# Symbol
genres = %i(fantasy adventure mystery romance)
# =>[ :fantasy, :adventure, :mystery, :romance ]
```
***
###8. Use `&`(ampersand) operator when iterate over an array###
```ruby
# Bad
titles = books.map { |book| book.title }

# Good
titles = books.map(&:title)
```

For more details, [here](https://dev.to/junko911/ruby-iteration-shortcut-with-ampersand-operator-923) is my blog post about iteration and `&` operator
***
###9. Use `.last` method to get the last element of an array###
```ruby
genres = [ :fantasy, :adventure, :mystery, :romance ]

# Bad
genres[genres.length - 1] # => :romance

# Good
genres.last # => :romance
```
***
###10. Destructuring assignment###
Destructuring is a convenient way of extracting multiple values from data stored in objects like arrays.
```ruby
quotient, remainder = 20.divmod(3) # => [6, 2]
quotient # => 6 
remainder # => 2 
```
***
##Others##
###11. [Heredoc(Here documents)](https://ruby-doc.org/core-2.5.0/doc/syntax/literals_rdoc.html#label-Here+Documents)
If you are writing a large block of text, heredoc will help you make it cleaner and more readable.
```ruby
# Bad
text = "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam,\nquis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

# Good
text = <<-TEXT
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
TEXT
```
***
Hope you find this article helpful and please leave comments if you have any suggestions to add!