|
|
|
SearchCategories
Books by the AuthorOther Ruby Projects |
Readable Booleans
Posted 2 months ago
in Ruby Tutorials and Ruby Voodoo.
There's a great little trick you can do to improve the readability of your code. A common problem is dealing with methods that have a boolean flag arguments. Here's an example I ran into just today in a Rail application:
The problem with this is that you typically see calls like this scattered around the application:
Would you know what Ironically the opposite problem, a magical dangling Anyway, the point is that we can typically improve the ease of understanding the common case. Remember that in Ruby
My hope is that might save a future maintainer a trip to the method definition to understand the call. I've used that trick in quite a few situations now. To give another example, |
|
|
|
If you need to pass another type when its purpose is not clear (e.g. false, or a number), you can use the slightly less attractive method of assigning to a throwaway variable:
print_something("Foo", rows=5)Just make sure that the variable name isn't something that might be used :)
This is awesome! Seems so obvious now. Why didn't I think of that?