Prettier Conditionals

Old codebases can get hairy. Especially if they were passed from a person to a person and people didn't really think about the next guy who can take it over. Or if they were building "MVP"s.

One of the things I see often is loooooooong conditional statements.

Something like this:

if (user && user.role == "admin") || (price > 17 || fuel_stat > 80)  
# rest of the code goes here

This is a long conditional statement. It takes quite a bit of time for a fresh pair of eyes to figure out what's going on. There is just too much state management. or and and statements nested and comparing roles and business logic and everything in one line.

I want to able to scan through conditionals fairly fast without knowing the details. One way to achieve this is with hiding all of that logic in a method.

if eligible_for_upgrade?(user, price)  
# rest of the code goes here

Code above looks a lot cleaner. When a new person comes on board, they don't have to spend any time on figuring out the details of what eligble_for_upgrade?(user, price) is if they don't want. That will in turn result in a code shipped faster, things being tested better and everybody's lives being a bit more joyful 😊

Nikola Novakovic

Software engineer that loves business side of things as well. Also lifts a lot of weights.

Subscribe to get regular updates