Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

CoffeeScript (which this is forked from) has some parts like this that I strongly dislike, such as using the unless keyword after an action. For example:

    doSomeThing() unless person.present
For me that utterly destroys readability - in my mind, when I see a (), that function is being called there and then. The fact that you can invalidate that later in the line confuses me deeply - and it doesn't really provide any benefits above an if statement anyway.

The rest of CoffeeScript is great though, don't get me wrong. (though ES6 JavaScript has taken the best features anyway)



As a Perl programmer, I'm often using contructs like that, and I think they have a place, but you should restrain yourself from using them in places where they can easily be misunderstood.

For example, they work really well in loop control statements:

  for my $i (1 .. 100) {
    next if $i == 10;
    next if skippable($i);
    last if is_what_we_want($i);

    # Do stuff here
  }
Or more complex param validation

  sub foo( $arg1, $arg2 ) {
    return unless $arg1 > 10;
    die "Invalid argument arg2!" unless defined $arg2 and $arg2 =~ /^(?:CAT|DOG|GERBIL)$/;

    # Do something
  }
That said, I don't like it for assignment of in that manner (at least where the assigned value comes out of the if), because the main justification for allowing it (it's a natural extension of how we think) doesn't follow. A ternary operator is better in that instance, IMHO.


That's not specific to 'unless' – it's the general postcondition style that's is derived from Ruby. Indeed, for single-line if statement bodies in Ruby, it's the recommended style.

It fits the style of Ruby to try and cut down on syntax noise where it's not needed. Contrast:

  save! if !record.persisted? && !record.invalid?
  save! unless record.persisted? || record.invalid?
Though I do appreciate it's a bit unusual compared with most other languages – CoffeeScript does owe a lot to Ruby's syntax.


That's not really the part of unless that bothers me - it's the order. Why not do:

    if !record.persisted? && !record.invalid?
        save!
or:

    unless record.persisted? || record.invalid?
        save!


The behavior here, if it comes from Ruby, almost definitely has it's legacy in Perl, so you can look to Perl for justifications. The main justification is likely to be that's a natural construct of how we think and communicate, so why not allow us to express ourselves that way?

Another way to look at it is why do you think the conditional should come before like you've shown? Because C or even older languages started with that? If you didn't have prior experience with that form, would you still think it's inherently better, or would the trade-offs between the two forms seem slightly more balanced?


I guess it's generally just that it's regarded as fairly idiomatic Ruby. bbatsov's popular style guide, for example (https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier):

The same applies to while/until loops, which can lead to quite succinct code.

I've been using it for a long time, so I don't find it confusing at all – but I agree it's pretty rare to find, so I'm not surprised to find out some people would rather not see that!


To play devil's advocate, when using Python's ternary operator like this:

    something() if False else y
The `something` function is never called.


The unless keyword is very useful for when you're testing an exit condition, e.g., return unless...


Well just don't use the unless keyword then. I've used CS for years and I don't use it.


Well sure. But I live in a weird world where I sometimes have to work with code other people wrote, as well as my own.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: