A short post on writing more readable code using the unless statement and how to write less code for conditional assignments.
Unless Statement
Using unless instead of if can make code more readable.
The best time to use unless is instead of writing a negative if statement.
When using unless I like to read the code and see how well it flows. For example I would not use unless when testing for nil because if something reads better, also using unless probably isn’t a good idea when there’s multiple conditions.
Shorthand Conditional Assignments
A shorthand way to write a conditional assignment while checking for a nil value is to use ||=
which means ‘or equals’.
In the following example the time key in the options hash is checked and if it is nil then time is set to UTC.
If you want to write a conditional with an if else
statement this can actually be written in a long, medium and a short way!
In the following example if the user is signed in then time is set to the users time zone else its set to UTC.
Thanks for reading :)