If-ElseIf-Else-Ception18 Jun 2016
I HATE when my code looks like a monstrosity of conditions.
There’s way too much logic involved and no effort to simplify it. It’s harder for someone else to understand this, let alone when you yourself look back at this later on. There’s got to be a quantified way to solve this code spaghetti!
There’s a great talk by Sandi Metz at Rails Conf 2014, All the Little Things, that addresses this.
Her approach is very simple — take your huge monolithic logic, and break it down to smaller, manageable, and reusable components. The rest will sort out over time.
Here are a few of my tricks when reducing complexity,
Return From The Top
If you have an ultra simple function that compares a condition to return a value if it matches, and another if it doesn’t. Fairly straight forward scenario.
Sure it’s not too bad, but it can look even better if you do this,
You can bid good bye to your hanging else statements!
Pipes Are Awesome
The pipe symbol, || is a favorite among Ruby devs. You can quickly assign default values when a variable is nil.
But it’s not too friendly with strings.
When you use this with the presence method, magic happens!
Try, Try again
If I got a dollar for every time i ran into the nil:NilClass error, I’d be a millionaire! This made me paranoid to stick a if obj.present? everywhere.
Yuck! There’s a better way to do this, you just got to keep trying.
Simple! Now when your user doesn’t exists, it will fail quietly and return a nil.
This works with methods too. If you have a method called fullname, try this out,
I have this horrible habit of organizing background workers as if they were logical classes. What I mean is, if I have a few tasks that need to be processed for the user in the background, I put them all in one worker (if it’s not too big). It’s easier to manage, and I don’t have a mess of workers when others collaborate.
Since I have different tasks to be processed for each call of the worker, I pass an “identifier”. Something that tells the worker class what function to perform.
Fortunately, there’s a cleaner way of doing this. This is where Ruby’s powerful meta-programming comes handy. Use send().
What is send?
send( ) is an instance method of the Object class. The first argument to send( ) is the message that > you’re sending to the object - that is, the name of a method. You can use a string or a symbol, but > symbols are preferred. Any remaining arguments are simply passed on to the method.
So from the above mess, this is how my reduced worker looks like.
So much cleaner, and so much easier to extend.
This particular tip needs to be taken with a pinch of salt. It’s not for everyone, and it might seriously damage your logic if not used carefully.
The great things about Ruby being OOPs complaint is it’s ease of extending base classes like String, TrueClass, and Integer. When used carefully you can do wonders.
I hate doing an if condition for booleans, it just feels wrong. Something like this,
I’d have to create a specific function in the User class just to handle something as simple as printing out different messages for a boolean value. Here’s a neat trick,
This overrides TrueClass and FalseClass so you can do something as simple as this,
So simple! There’s so many tiny adjustments you can make with this little trick!