DEV Community

Cover image for Stop this, use this instead : Top 5 best practices in Ruby to reduce "if" in your code

Stop this, use this instead : Top 5 best practices in Ruby to reduce "if" in your code

Pimp My Ruby on December 06, 2023

Table of Contents  1. Inject dependency instead of sending a boolean  2. Use a NilObject as fallback value  3. Use validator object  ...
Collapse
 
a_chris profile image
Christian

Great examples! Some of them gave me Java vibes but in a positive way.
The one I liked most is the example n.2, that could have saved us a lot of bug at work 😂

Collapse
 
pimp_my_ruby profile image
Pimp My Ruby

Ahaha I completely understand the Java vibe.

I’m a bigger project, having a NilObject can help by introducing some default behavior. In my exemple I just have 1 method. But when you have several attributes and stuff it shine a lot 🧞‍♂️