Is Ruby's best practice of shun operator modifiers after, for example, a long block of code that is passed to an iterator?

something.each do |x|

  #lots of stuff

end if some_condition
+3
source share
6 answers

I think the popular way is to use operator modifiers only if it's single-line . In all other cases, use the usual if style, common in C, Java, etc.

bail_out if reqd_param.nil?

if its_gonna_be_long then
  long_exec stmt1
  long_exec stmt2
  ....
end
+7
source

I personally oppose this for the pure and simple reason that it is too easy to miss. Even with this shortened version, it took me two times to figure out what you have

if some_condition

in the end

+3
source

, , . , :

<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.

, , , , , .

do loop
  # ...
  next if condition
  # ...
end
+1

- , .

- .

+1

, , .

-1

I think the above example is great in some cases when it exists inside several nested blocks. If the code above has 4 depth levels, then you have deleted another level. Therefore, in some cases, the above style can actually increase readability. Please note that we assume that the block contains no more than 20 operators.

-1
source

Source: https://habr.com/ru/post/1698354/


All Articles