Does Ruby best practice shun statement modifiers after, for example, a long code block that is passed to an iterator?


Does Ruby best practice shun statement modifiers after, for example, a long code block that is passed to an iterator?



something.each do |x|    #lots of stuff  end if some_condition 



What “Pastie” alternatives are there? (installable)

1:



How can I install a local gem?
I think the popular way is to use statement modifiers only if it is a one-liner.


What code highlighting libs are there for Ruby?
In all other cases, use the normal if style prevalent in C, Java etc..
How can I write a hook that gets called when a RubyGem is installed?
bail_out if reqd_param.nil?  if its_gonna_be_long then   long_exec stmt1   long_exec stmt2   .... 


What does __FILE__ mean in Ruby?
end


Redirect the “puts” command output to a log file


FizzBuzz using ternary conditional operator

2:



How do you list the currently available objects in the current scope in ruby?
I'd personally advocate against that for the pure and simple reason that it is too easy to miss.

Even with that shortened version it took me a double-take to realise you had the .
if some_condition 
at the end.


3:


I almost never use the modifier forms of conditionals because I think there is too much potential for reader confusion.

It's like an officer talking to a subordinate:.
 <sergeant> Your orders are to climb that hill and recon the enemy! <private> YES SIR!  *begins running up the hill* <sergeant> ... 

but only if you have binoculars.

The only time I might consider it acceptable is when the thing modified is so small that the conditional can clearly be seen, e.g..
 do loop   # ... 

next if condition # ...

end


4:


Long code block it self is a bad practice, refactor it to more smaller blocks.. Modifier after a long block is a way to hell..


5:


In the organization I work for we have started to flag such constructs for re-write..


6:


I think the above example is perfectly fine in certain cases where it exists inside of several nested blocks.

If the above code is 4 levels deep then you have eliminated another level.

Therefore, in certain cases, the above style can actually increase readability.

Note we are assuming that there are no more than 20 statements within the block..



76 out of 100 based on 46 user ratings 646 reviews