Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should you avoid the then keyword in Ruby?

It's mentioned in several Ruby style guides that you should "Never use then." Personally, I think the "then" keyword allows you to make code denser, which tends to be harder to read. Is there any other justification for this recommendation?

like image 324
readonly Avatar asked Apr 14 '11 06:04

readonly


People also ask

Should I use ruby2_keywords for keyword arguments?

If you really worry about the portability, use ruby2_keywords. (Acknowledge that Ruby 2.6 or before themselves have tons of corner cases in keyword arguments. :-) ruby2_keywordsmight be removed in the future after Ruby 2.6 reaches end-of-life. At that point, we recommend to explicitly delegate keyword arguments (see Ruby 3 code above).

When will ruby2_keywords be removed?

(Acknowledge that Ruby 2.6 or before themselves have tons of corner cases in keyword arguments. :-) ruby2_keywordsmight be removed in the future after Ruby 2.6 reaches end-of-life. At that point, we recommend to explicitly delegate keyword arguments (see Ruby 3 code above). Other minor changes

How many keywords are there in Ruby?

There are total 41 keywords present in Ruby as shown below: The script encoding of the current file. The line number of this keyword in the current file. The path to the current file. Runs before any other code in the current file. Runs after any other code in the current file.

How to pass keyword arguments instead of hashobject in Ruby 3?

It explicitly specifies passing keyword arguments instead of a Hashobject. Likewise, you may add braces {}to explicitly pass a Hashobject, instead of keyword arguments. Read the section “Typical cases” below for more details. In Ruby 3, a method delegating all arguments must explicitly delegate keyword arguments in addition to positional arguments.


1 Answers

I almost never use the then keyword. However, there is one case where I believe it greatly improves readability. Consider the following multi conditional if statements.

Example A

if customer.jobs.present? && customer.jobs.last.date.present? && (Date.today - customer.jobs.last.date) <= 90
  puts 'Customer had a job recently'
end

Line length too long. Hard to read.

Example B

if customer.jobs.present? &&
   customer.jobs.last.date.present? &&
   (Date.today - customer.jobs.last.date) <= 90
  puts 'Customer had a job recently'
end

Where do the conditions end and where does the inner code begin. According to must Ruby Style Guides, you to have one extra space of indentation for multi line conditionals, but I still don't find it all the easy to read.

Example C

if customer.jobs.present? &&
   customer.jobs.last.date.present? &&
   (Date.today - customer.jobs.last.date) <= 90
then
  puts 'Customer had a job recently'
end

To me, Example C is by far the most clear. And it is the use of the then that does the trick.

like image 142
Andrew Griffith Avatar answered Oct 05 '22 01:10

Andrew Griffith