ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

Conflict in a DSL style

Open dsounded opened this issue 8 years ago • 4 comments

# bad expect(bowling.score).to eq 0 # good expect(bowling.score).to eq(0)

But:

Omit parentheses around parameters for methods that are part of an internal DSL (e.g. Rake, Rails,RSpec)

IMO eq 0 looks better

dsounded avatar Apr 11 '16 18:04 dsounded

+1 I would leave parentheses only for cases where it doesn't work without them ( e.g. expect(hash).to eq({}) )

vbyno avatar Apr 11 '16 19:04 vbyno

The comments on the example should be updated because they contradict the heading.

"Only omit parentheses for...Methods that are part of an internal DSL (e.g., Rake, Rails, RSpec)".

# good.  it's part of rspec's internal DSL
expect(bowling.score).to eq 0

If you have to pass in an array or hash then use parenthesis. This way the example doesn't contradict the rule.

uchagani avatar Dec 13 '16 16:12 uchagani

Only omit parentheses for...Methods that are part of an internal DSL (e.g., Rake, Rails, RSpec)

I'd relax this rule to "Always use parentheses for ... methods that are NOT part of an internal DSL".

expect(bowling.score)
  .to be > 0
  .and be_odd # BOOM

A less contrived example:

    expect([1, 2, 3])               # Omiting those parentheses with go BOOM
      .to include(be_odd, be_even)  # Those as well
      .and have_attributes(size: 3) # And  those too

pirj avatar Feb 21 '21 20:02 pirj

Yeah, that's a fair point. I guess to me it was obvious you shouldn't omit mandatory parentheses. :D And, of course, not all DSLs are the same and rely heavily on omitting parens.

bbatsov avatar Feb 22 '21 07:02 bbatsov