Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

next versus if in a .each loop?

Tags:

I have a text processing thing I'm doing in Ruby. Basically, I have to implement a simple state machine (with one character look-behind).

My code at the moment looks like this:

text.each{ |c|   ...   ...   ...   ...   if @state!=:some_state     next   end   #processing stuff for if in :some_state mode   ...   ...   ...   ...   ... } 

Is this proper? Or should it rather be implemented like:

text.each{ |c|   ...   ...   ...   ...   if @state==:some_state     #processing stuff for if in :some_state mode     ...     ...     ...     ...     ...   end } 

Is there a right way or is it just preference? Which one blends more with "the ruby way" of doing things?

like image 400
Earlz Avatar asked Feb 17 '10 04:02

Earlz


People also ask

WHAT ARE FOR NEXT loops used for?

The for... next statement is an iterative, incremental loop statement used to repeat a sequence of statements for a specific number of occurrences. A for... next loop executes a set of statements for successive values of a variable until a limiting value is encountered.

How do you write a condition in a for loop in Python?

The Python for statement iterates over the members of a sequence in order, executing the block each time. Contrast the for statement with the ''while'' loop, used when a condition needs to be checked each iteration or to repeat a block of code forever. For example: For loop from 0 to 2, therefore running 3 times.

How do you skip a loop in VB net?

In VB.NET, the continue statement is used to skip the particular iteration of the loop and continue with the next iteration. Generally, the continue Statement is written inside the body of the For, While and Do While loop with a condition. In the previous section, we learned about the Exit Statement.


2 Answers

Totally agree with @DigitalRoss and I have seen people using next if there is complicated piece of code after some condition is being evaluated i.e.

 next if @state!=:some_state  # some long complicated code 

on the other hand if there is a simple operation that needs to be performed on the basis of some condition then I would prefer

 if @state == :some_state    #call_a_method_to_do_something  end   OR   call_a_method if @state == :some_state 

Having said that, its bad practice to write long complicated code. If your code is clean and well designed then you would never have to use next within your code.

like image 68
nas Avatar answered Oct 06 '22 01:10

nas


I think that the example you gave doesn't really capture situation where doing next makes a difference. Consider the situation where you have multiple "next-points" in your code:

text.each do |c|   next if @state == :state1   ...   next if @state == :state2   ...   next if @state == :state3   ... end 

and compare it with if-variant:

text.each do |c|   unless @state == :state1     ...     unless @state == :state2       ...       unless @state == :state3         ...       end     end   end end 

Although the first could be viewed as spaghetti-style by some purists, IMHO it is more readable than the latter.

like image 36
Mladen Jablanović Avatar answered Oct 05 '22 23:10

Mladen Jablanović