How to Organise Your Code - High Level to Low Level

Which one can you make better sense of?

<%= content_for :title, "The Quote App - Project: ##{@quote.id} - #{@quote.project_name}" %>

<% content_for :breadcrumbs do %>
	<%= render partial: 'quotes/tailwind/breadcrumbs', locals: {quote: @quote, client: @client} %>
<% end %>

<div class="mx-4">
	<%= render partial: 'quotes/tailwind/fly_out_menu', locals: {quote: @quote, tenant: @tenant} %>

	<%= render(partial: 'quotes/tailwind/heading', locals: {quote: @quote, client: @client}) %>

	<%= render(partial: 'quotes/tailwind/tabs', locals: {quote: @quote, client: @client, total: @total}) %>
</div>

<%= turbo_frame_tag "line_item_search_results", src: quote_line_item_detailed_views_path(@quote, request.params) do %>
<% end %>

Or this?

<% content_for :title, "The Quote App - Project Info Page - ##{@quote.id}  #{@quote.project_name}" %>
<!-- start bread crumbs -->
<% content_for :breadcrumbs do %>
  <span class="nav-link">
    <ol class="breadcrumb">
      <li class="breadcrumb-item">
        <%= link_to("Quotes", quotes_path) %>
      </li>
      <li class="breadcrumb-item">
        <%= link_to("#{@client.name} quotes", quotes_path(q: {client_name_start: @client.name})) %>
      </li>
      <li class="breadcrumb-item">
        <%= link_to("##{@quote.id} #{@quote.project_name.truncate(40)}", quote_path(@quote)) %>
      </li>
      <li class="breadcrumb-item active" aria-current="page">
        Line Items (Detailed Views)
      </li>
    </ol>
  </span>
<% end %>
<h4 class="mb-5">
  <%= link_to  quote_path(@quote), class: "btn btn-outline-dark" do %>
    <%= octicon "arrow-left", :height => 16, :"aria-label" => "Line Item Link" %>
    <%= "##{@quote.id}"  %>
    <%= @quote.project_name %>
  <% end %>
</h4>
<%= turbo_frame_tag "line_item_search_results" do %>
  <h4>Line Items</h4>
  <hr>
  <%= render 'search_form' %>
  <%= turbo_frame_tag "new_line_item" do %>
  <% if LineItemPolicy.new(line_item: @quote.line_items.new, person: Current.person, quote: @quote, organisation: @tenant).new? %>      
      <% if Current.person.basic? %>
        <div class="d-flex justify-content-end">
          <div class="btn-group">
            <button type="button" class="btn btn-outline-dark dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
              <%= octicon "plus", :height => 16,  :"aria-label" => "back to quote" %>
              New Line Item
            </button>
            <ul class="dropdown-menu">
              <li>
                <%= link_to("Variation", new_quote_line_item_path(@quote, line_item_type: :variation), :class => 'dropdown-item') %>
              </li>
              <li>
                <%= link_to("Design Review", new_quote_line_item_path(@quote, line_item_type: :design_review), :class => 'dropdown-item') %>
              </li>
            </ul>
          </div>
        </div>
      <% else %>
        <div class="d-flex justify-content-end">
          <%= link_to(new_quote_line_item_path(@quote), :class => 'btn btn-outline-dark') do %>
            <%= octicon "plus", :height => 16,  :"aria-label" => "back to quote" %>
            New Line Item
          <% end %>
        </div>
      <% end %>
    <% end %>
  <% end %>
  <br>
  <%= turbo_frame_tag "line_items" do %>
    <%= render partial: "line_items/line_item", collection: @line_items %>
    <%= render 'pagination' %>
  <% end %>
<% end %>

The latter is barely legible.

The former is infinitely better because it essentially component-ised. You have a high level, and can easily drill down into lower levels if you need to. It makes all the difference when you want to quickly understand or edit your views.

Written on January 19, 2025