Hacker News new | past | comments | ask | show | jobs | submit login

I find this:

    error "The eggs are not in their basket"
        unless $eggs_in_basket;
clearer than this:

    error "The eggs are not in their basket"
        if !$eggs_in_basket;
because "unless" is more visible than "!", and because the condition indicates the "normal" case.

I agree that unless/else blocks are quite unhelpful. The double negative doesn't help.

That said I am guilty of writing some unless/else blocks, but the reason is always performance. "unless ($x)" is faster than "if (!$x)". Only one opcode faster, but in something optimised for speed that's a few percent, and therefore a goal, especially if there are many of them. For something like "++$y unless $flag" it's more than a few percent.

As with many other things in Perl, and other languages in the "slow" class (Python, Ruby etc), there are no dataflow optimisations. Things like "my $x; $x = 1" are slower than "my $x = 1;". Yet still there are times when something is worth doing faster, without the big jump of rewriting in a different language.

That's completely different from the "fast" languages, like C, C++, C#, and nowadays JavaScript. I've come to appreciate that I wish all languages did trivial dataflow optimisations and inlining, even if they are in every other respect interpreted languages, because absence of dataflow motivates people to write uglier and more difficult to understand code in tight hotspots, when code clarity would be really valuable.




Yeah, I hear you. I actually use `doit() unless $something;` quite often, but then change it to `doit() if ! ($something || $something_else )` immediately the need arises. That is I always think about it when I"m writing an unless statement.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: