Live data from Hacker News

Read this post ‘unless’ you’re not a Ruby developer

jesseduffield.com

121–130 of 326 posts

Re: Read this post ‘unless’ you’re not a Ruby developer

#121
post #35

I don't have a problem with double negation but I hate ruby for this kind of design - pointless aliases for everything. It's the exact opposite of pythons "There should be one– and preferably only one –obvious way to do it" - they intentionally create solutions that have zero practical benefit - it's just fuels arguments based on preferences and introduces mental overhead due to inconsistency.

It's not pointless, it makes code read better. It's another option to have. It's not meant for every use case. It indicates you don't understand Ruby if you find yourself using an "unless" in a complex boolean operation. It's meant for simple cases like an inline return statement return unless User.exists(id=100)

    return if !User.exists(id=100)
reads just fine and takes less letters. I don't think unless is "bad", it's just unnecessary

> It indicates you don't understand Ruby if you find yourself using an "unless" in a complex boolean operation. It's meant for simple cases like an inline return statement

    unless `git status -s | grep -v 'RAILS_VERSION\\|CHANGELOG\\|Gemfile.lock\\|package.json\\|version.rb\\|tasks/release.rb'`.strip.empty?
      abort "[ABORTING] `git status` reports a dirty tree. Make sure all changes are committed"
    end
is not exactly very readable and this

          unless connection.adapter_name == "Mysql2" && options[:id] == :bigint
            if [:integer, :bigint].include?(options[:id]) && !options.key?(:default)
              options[:default] = nil
            end
          end
isn't glancable either. Just random snippets from Rails code. You can see how author wanted some bonus points and used unless, if, and ! too. Sure it isn't hard to figure out but it makes it needlesly obtuse

Re: Read this post ‘unless’ you’re not a Ruby developer

#122
post #79

Earlier quoted context omitted.

God that's such a landmine when reading code. Seeing a return without an explicit change of scope... Why ? To save one line ? Yep that's why I hate ruby - worked on one mature codebase for a year and after seeing various such gems used across the project - from >10 devs - I'm confident I will never touch the language again.

Those one line returns are particularly handy at the beginning of methods, to validate or process arguments and return immediately for simple cases.

right but

    if condition { return } 
works well enough in other languages and shows actual condition (the important part) to programmer first. if that if and extra brackets is really too long Perl way is also option.

    condition || return

Re: Read this post ‘unless’ you’re not a Ruby developer

#123

I'm not a very experienced Ruby developer but RuboCop does a pretty good job at guiding and teaching you to use `unless` as a guard in method definitions, e.g.: def my_method(required_thing) raise 'required_thing is required' unless required_thing.present? ... end In practice, this hasn't been as issue as a consequence of the tooling and ecosystem around Ruby.

and why

    def my_method(required_thing)
      raise 'required_thing is required' if !required_thing.present?
is a problem ?

Re: Read this post ‘unless’ you’re not a Ruby developer

#124
There are two main reasons unless is great and I miss it now that I primarily program in python.

1. It is a negative, and that is a great thing when there are boolean conditions. Yes, you can wrap the whole thing in brackets, but when doing a visual scan quickly over code, they're easier to miss than the giant `unless` token which guarantees that you won't have an early closing bracket. The more the number of terms go up, the more I have to be fastidious with watching where brackets begin and end.

2. It hints to the expected flow of things. Isn't the first more readable to you?

    return :allow_air_travel unless self.nuclear_war_ongoing?
    
    return :allow_air_travel if ! self.nuclear_war_ongoing?
Or I'll put it another way. If you do a search of your own hacker news comments on BigQuery, will you never find the english word "unless" outside of a Ruby discussion there? You could say "if not" why bother using an extra word? Or what about "not true" do you ever say that when you could just say "false" like Dwight from The Office?

Basically my argument boils down to this:

Bears. Beats. Battlestar Galactica.

Re: Read this post ‘unless’ you’re not a Ruby developer

#125
post #70

I really don’t understand the author’s point about adding extra conditions. They admit that many people will find a single condition with `unless` more readable. They then complain that it becomes unreadable when adding another condition. OK, so swap it out for an `if` at that point. No one is forcing you to keep using `unless` if the requirements change. “You should use a suboptimal solution to cater for unknown fut…

That’s exactly how I use ‘unless’. Simple on condition (no else branch) use cases in one liners, anything more complex, I move to if. Best of both world I’d say!

Yes! And rubocop can encourage the use of 'unless' in only these simple situations where it does enhance legibility.

https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/Unle...

https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Style/Unle...

Re: Read this post ‘unless’ you’re not a Ruby developer

#127
That's a lot of words for a simple matter of cleanliness. Do not use 'unless' for complex expressions, only single conditions. I personally hate grouped negatives like the one shown:

    !(valid_token && !expired)
This expression is also a double (triple?) negative, and a lot harder to parse. The correct approach is to make the `valid_token` flag take `expired` into account, or add a third variable that represents validity, not append `expired` to the unless clause. This keeps everything readable, if the variable names are descriptive enough:

    valid_token = token.valid? && !expired
    return 'invalid' unless valid_token
It's also damning that the author uses `unless ... else` as his starting point for the critique, while he is aware that the style guide says "Do not use unless with else" (briefly acknowledged in the last section).

Re: Read this post ‘unless’ you’re not a Ruby developer

#128
Unless seems to be pretty common in many languages. https://www.indifferentlanguages.com/words/unless In my mind the javascript community would do well to address the idiosyncrasies in its language standard before criticizing other languages for using common human language patterns. If you want to pick on ruby for being weird with conditionals, consider the following.

   if 0 
     puts 'true'
   end
which will print true. I think there is a much greater chance that 0 being true will cause problems with programmers from other languages than using unless which is quite natural to humans. Ruby is first and foremost a language for humanity, and probably dead last a language for ease of implementation.

> As a general rule I think that if a language has some feature for which there is already a commonly understood syntax across other languages, it should just use that syntax. If you’re introducing a complete paradigm shift, then that’s fine, but unless is not that: it’s just a different way to write if ! and people jumping back and forth between ruby and, say, javascript, now have one extra idiosyncracy to keep in mind.

Re: Read this post ‘unless’ you’re not a Ruby developer

#129
I'd consider myself a writer more than a developer, but I've been working in Rails for over 15 years, and one of my absolute favourite things is "unless".

Why? Because it allows you to express yourself more elegantly.

The click-bait title is misleading. It's meant to ridicule "unless", but actually achieves the opposite.

If you were to write the title of the post as code, it would be:

  unless !ruby_dev
    read article
  end
That would be a terrible use of "unless"! That should clearly say "if ruby_dev", not "unless !ruby_dev".

But what if you wanted to write an article meant for anyone other than ruby developers?

Which of the following is better?

  if !ruby_dev
or

  unless ruby_dev
Both work, but I consider the second option more elegant. Just as I wouldn't verbally say "if you're not a ruby developer" rather than "unless you're a ruby developer".

Honestly, I don't see the issue. It's a style matter. Just use it properly. All language can be abused if you try hard enough.

Post reply on HN