Readable booleans
Here is a trick I've learned from James Edward Gray II.
Have a look at this code. How can we improve it?
<%= rating_stars(..., true) %>
Looking at this line we have no idea what this "true" is actually responsible for and we'll have to go to the definition of rating_stars to find out.
There we will find:
def rating_stars(..., clickable = false)
# ...
end
Ok, the boolean variable is responsible for the rating being clickable/non-clickable.
Now, since everything except false and nil is true in Ruby, we can improve the original line by using a meaningful symbol instead of true:
<%= rating_stars(..., :clickable) %>
Now, the code says: "return me a rating that is clickable".
Much more clear than the original version.
Written by Alexander Zaytsev
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#