DEV Community

Discussion on: Be careful when memoizing booleans!

Collapse
 
val_baca profile image
Valentin Baca

c/o github.com/rubocop-hq/ruby-style-g...

"Don't use ||= to initialize boolean variables. (Consider what would happen if the current value happened to be false.)"

# bad - would set enabled to true even if it was false
enabled ||= true

# good
enabled = true if enabled.nil?
Enter fullscreen mode Exit fullscreen mode

I've found that when using a language, style guides are more valuable than just style; they can help me avoid common pitfalls like this.

Collapse
 
codeandclay profile image
Oliver • Edited

Thanks. I'd never considered styleguides to be such an invaluable resource.

@happy = post_complete? if @happy .nil? is a lot more succinct.

I'm going to update the post as I actually prefer this and I think it should be mentioned.

It may be that I'm tired and haven't understood it properly but as I understand it, it doesn't do exactly the same thing.

The first time, it evaluates to false. The second time, however, it evaluates to nil.

This is what I get in pry:

> a = false if a.nil?
#=> false
> a = false if a.nil?
#=> nil
Enter fullscreen mode Exit fullscreen mode

I'll need to take a look again at this in the morning. Any input would be welcome!

Collapse
 
val_baca profile image
Valentin Baca • Edited

You're confusing the value of a with what the statement a = false if a.nil? is returning.

The console prints the output value of the whole statement; not necessarily the value of a.

If the value of a did change (as in, when it was assigned to), then that new value will print in the console. But if a doesn't change, then nil will be printed.

irb(main):001:0> a = false if a.nil?
=> false # this false is what "a = false if a.nil?" returned. It's false because 'a' was set to 'false'.
irb(main):002:0> a
=> false
irb(main):003:0> a = false if a.nil?
=> nil # this nil is what "a = false if a.nil?" returned. It's nil because 'a' was NOT set to anything.
irb(main):004:0> a
=> false
Thread Thread
 
codeandclay profile image
Oliver

🤦 Of course. Thanks for the explanation. I'd convinced myself that it was returning the variable's value. Doh!

So, in my case I would use it like so:

  def happy?
    @happy = post_complete? if @happy.nil?
    @happy
  end