1
\$\begingroup\$
<% @descriptions.each_with_index do |description, i| %>
  <% description.tale2.each do |tax_ref| %>            
    <% if condition %>              
      <% if condition %>
        <% if condition %>                  
          <%= $text_first_describe%> <%= $paren_author_yr %>
             <% ref_sp_uniq.each_with_index do |ref, i| %>
               <% if ref == tax_ref.ref_wo_brace%>
                  <% execution %>                      
               <% elsif i == (ref_sp_uniq.size - 1)%>
                  <%# @ref_desc = "#{@ref_desc_numb}. #{tax_ref.ref_wo_brace}" %>
               <% end %>
             <% end %>
          <% if condition %>
                  <% execution %>                      
          <% elsif condition %>
            <% execution %>
          <% elsif taxon_name.emend_author_year %>                    
             <%= print %>
          <% else %>                    
             <%= print %>                    
          <% end %>
        <% end %>
      <% else %>
        <% if condition %>
          <%= print %>
          <% ref_sp_uniq.each_with_index do |ref, i| %>
             <% if condition %>
                <% execution %>                      
             <% elsif condition %>
                  <% execution %>                      
             <% end %>
          <% end %>
          <% if condition %>
            <% execution %>                      
          <% elsif condition %>
            <% execution %>                      
          <% elsif condition %>
            <% execution %>                      
          <% else %>
            <% execution %>                      
          <% end %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>        
<% end %>
\$\endgroup\$

closed as unclear what you're asking by Malachi, Mathieu Guindon, 200_success, Paul, Anirban Nag 'tintinmj' Nov 24 '13 at 11:01

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ try to edit your code with a good completion \$\endgroup\$ – shingara Feb 1 '11 at 10:03
  • 1
    \$\begingroup\$ that depend your condition. Its almost impossible to help your without more information \$\endgroup\$ – shingara Feb 1 '11 at 10:04
  • 12
    \$\begingroup\$ As it stands this question is impossible to answer. Without knowing what conditions are being checked or what code is being executed there is literally nothing we can suggest. Maybe you are repeating code, or maybe several chunks of code could just be run under a single check, but we simply do not know. We need the actual code with conditions and execution before we can do anything. \$\endgroup\$ – doppelgreener Feb 1 '11 at 10:12
  • 5
    \$\begingroup\$ -1: please edit your question, use code formatting (four spaces at the start of each line) and provide some context. \$\endgroup\$ – Eric Bréchemier Feb 1 '11 at 20:04
  • 1
    \$\begingroup\$ See Flattening arrow code. \$\endgroup\$ – Mathieu Guindon Nov 24 '13 at 6:26
6
\$\begingroup\$

In general, you can try inverting the condition on your outermost if block, and work your way in. This will often result in less nesting, eg:

if condition1
  if condition2
    #ifblock
  else
    #elseblock2
  end
else
  #elseblock
end

Inside a loop, as you have your code, it would become this strcture, which is less nested:

if !condition
  #elseblock
  next
end
if !condition2
  #elseblock2
  next
end
#ifblock
\$\endgroup\$
2
\$\begingroup\$

A thing to try: case statement

case tax_ref
when a then execution1
when b then execution2
end

http://www.tutorialspoint.com/ruby/ruby_if_else.htm

\$\endgroup\$
2
\$\begingroup\$

Why can't you use the operators || and && ?

im not at all literate with the syntax but you should be able to convert:

<% if condition %>            
  <% if condition %>
     <% if condition %>
     <% end %>
  <% end %>
<% end %>

into:

<% if condition && condition && condition %>
    <% expression %>
<% end %>

Looking at the docs this logical expressions are available!

also removing the language identifiers you should be able to do:

<%
    if condition && condition && condition
        expression
    end
%>

When it comes down to the else if's then you can increase readability by doing a switch statement:

The main point is to remove the not so required <% and %> in places.

http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#and

\$\endgroup\$
2
\$\begingroup\$

There are several things you should do with this template, and templates that look like this in general:

  1. Break some elements into partials.

  2. Push logic to the model. Instead of

     <% if condition with model %>
       stuff
     <% else %>
       other stuff
     <% end %>
    

    when stuff or other stuff are strings with little or no markup, do

     <%= model.display_for_condition %>
    
  3. Use helpers for cases like 2) where the things to be displayed have some markup:

     <% condition_helper(model.condition?) %>
    
  4. Use presenter objects, especially when dealing with display logic that references more than one model.

  5. Most abstractly, but most importantly for learning to write code with fewer if statements, internalize one of the key distinctions between OO style and procedural style code: with OO you ask objects to do things they know how to do. If you find yourself always asking objects for information and deciding what to do with it, you are using objects as nothing more than structs and writing procedural code.

    Or as my intern described it the other day: (this has become one of my favourite quotes about programming)

if you're itchy it's ok to scratch your own itch, but it would be kind of weird to scratch other people's itches.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.