News for hackers. Stuff that matters.
Re:Reverse Engineering One Line of JavaScript by methyl on Friday July 14, 2017 @03:49AM (#14768247)
I can't agree, if you are a Ruby programmer you immediately know what's going on in the first line and it's definitely more readable - you see it at a glance instead of having to scan three lines.
Re:Reverse Engineering One Line of JavaScript by onion2k on Friday July 14, 2017 @06:28AM (#14768762)
if you are a Ruby programmer

cyberferret explicitly said it's a good idea to avoid shorthand methods because new programmers don't understand them, and you're making the argument that they're OK because experienced programmers will understand them. The essence of your argument is that if you've used the language enough to know what the or-equals statement means then you'll know what it means. I'm not convinced that's a good reason.

Re:Reverse Engineering One Line of JavaScript by vortico on Friday July 14, 2017 @06:43AM (#14768812)
For that specific example though, it's so common in the colloquial Ruby language that all normal Ruby programmers should learn and use it. If you're just taking about readability, the time saved skimming during your career is worth the time spent learning the idiom once in your life.
Re:Reverse Engineering One Line of JavaScript by joshumax on Friday July 14, 2017 @07:04AM (#14768885)
Cyberferrit said new programmers to the project, not specifically new to the language or programming itself.

Working on emulation software I commonly run across snippets such as `res = !!iRegisterVal` which results in some C newbies scratching their heads as to why a useless double negation would be there. But for more seasoned developers its intent can pretty easily recognized...

Re:Reverse Engineering One Line of JavaScript by CrystalLangUser on Friday July 14, 2017 @07:10AM (#14768906)
Why is !! useful? Asking as a ruby programmer.
Re:Reverse Engineering One Line of JavaScript by drusepth on Friday July 14, 2017 @07:28AM (#14768979)
Because Ruby doesn't have a to_boolean method. !! is used to convert "truthy" or "falsy" values [0] into true boolean true or false values.

For example,

    def user_signed_in?
      !!current_user
    end
This returns "true" or "false", instead of a (truthy) User object or a (falsy) nil.

[0] https://gist.github.com/jfarmer/2647362

Re:Reverse Engineering One Line of JavaScript by asherkin on Friday July 14, 2017 @07:23AM (#14768961)
It is a shorthand method of casting to a boolean value - C didn't have a true bool type until "recently" (but did have the concept of truthiness, where true is `is not 0`), so many projects and libraries use `#define TRUE 1`, and #define FALSE 0` or some similar variant.

So rather than write `foo ? TRUE : FALSE` or being more explicit `(foo != 0) ? 1 : 0`, you can just do `!!foo`.

Re:Reverse Engineering One Line of JavaScript by sethrin on Friday July 14, 2017 @07:24AM (#14768966)
It's a quick cast to a boolean.
Re:Reverse Engineering One Line of JavaScript by hasa on Friday July 14, 2017 @09:07AM (#14769469)
No. It is not so simple as that. Some developers don't like mindfuck with cryptic lines all the time. But as with placement of curly braces, the coding convention battle rages on. Btw the old symmetric C-style is the only right way ;)
Re:Reverse Engineering One Line of JavaScript by crooked-v on Friday July 14, 2017 @04:16AM (#14768353)
And even if using an if, you can make it smaller with

    foo = 1 if foo.nil?
Re:Reverse Engineering One Line of JavaScript by maze-le on Friday July 14, 2017 @05:04AM (#14768522)
Inline conditionals can be a blessing and a curse. In this case, it is absoluteley alright to use them (I find it also more elegant). But I have also seen things like:

    my_fancy_named_variable = my_array.map{|x| do_processing(x)}.sort_by{|x| x.value} unless some_very_long_conditional_that_breaks_readability
This is not very readable, and I would actually use multiple lines to format a call like this.