News for hackers. Stuff that matters.
Re:Yoda Conditions by kentt on Friday February 17, 2017 @12:22PM (#13669952)
One of the worst parts of Ruby. I can see the argument that it could be better to have

  save if valid
rather than

  if valid
    save
  end
but what I see 9 times out of 10 is

  things.do each |thing|
    foo
    bar
    baz
  end if valid
or

really.long.thing.that.i.try.to.parse.in.my.head if acutally_almost_never_happens

which is harder to read since I read top to bottom / left to right, but the flow is bottom to top and right to left

Re:Yoda Conditions by kbenson on Friday February 17, 2017 @02:23PM (#13670924)
This is something Ruby likely got form Perl,but they cribbed it wrong. Perl's post-conditionals only work on single statements, not blocks. You can use them for functional style statements though. I.e.

    # Not allowed
    for ( 1 .. 10 ) {
        say $_;
    } if 1;
    
    # Allowed
    map { say $_ } ( 1 .. 10 ) if 1;
The idea seems to be that a post-conditional should be simple and obvious by the time you've parsed the statement. A block makes it confusing, as does mixing post control structures

Post-loop structures have the same limitation:

    # Not allowed
    { say "foo"; say $_ } for ( 1 .. 10 );
    
    # Allowed
    say $_ for ( 1 .. 10 );
When used correctly, these can become very succinct and clear.

    my %data = get_data_record_hash("foo");
    
    $data{$_} = update_field(data{$_}) for ('field1','field4',other_field');
    
    # Compare to map, which normally you expect to return values
    map { $data{$_} = update_field(data{$_}) } ('field1','field4',other_field');
The single statement limitation keeps you from going wild in ways that are probably not useful to future readers of the code (including you).
Re:Yoda Conditions by Gaelan on Friday February 17, 2017 @12:26PM (#13670001)
You can write gibberish in any language. You shouldn't kill useful features because they are sometimes misused.
Re:Yoda Conditions by olalonde on Friday February 17, 2017 @02:18PM (#13670891)
> You shouldn't kill useful features because they are sometimes misused.

That is highly debatable.

Re:Yoda Conditions by ghurtado on Friday February 17, 2017 @03:02PM (#13671229)
Not even PHP's "register_globals"?
Re:Yoda Conditions by Gaelan on Friday February 17, 2017 @09:31PM (#13673306)
I'd argue that register_globals is never a good idea. Post-if is sometimes a good idea, with one case that is problematic (less readable, not even a negative affect on function)
Re:Yoda Conditions by ghurtado on Sunday February 19, 2017 @11:45AM (#13680515)
I happen to agree, but the statement was about "useful features".