3 ms·
I only know `unless` from Perl, and it can make your code clearer if used sparingly. For example it allows you to express preconditions positively: die "Yo
by gpvos 4y ago
I only know `unless` from Perl, and it can make your code clearer if used sparingly. For example it allows you to express preconditions positively:
die "You may only use port numbers 1024 and higher"
unless $port >= 1024;
- TomSwirly 4y agodie "Port numbers below 1024 are forbidden" if $port < 1024; is hardly unreadable spaghetti code.
- gpvos 4y agoOkay. Now compare: die "You may only use port numbers 1024 and higher" unless $port >= 1024 || is_root(current_uid()); die "Port numbers 1024 are forbidden" if $port < 1024 && !is_root(current_uid());
- hashhar 4y agoMy head hurts! Does unless apply to both branches or just one. The if example is easier for sure.