For example, consider the following snippet:
<a href="/path/elsewhere"><%
if @condition
%>True text<%
else
%>False text<%
end
%></a>
It may not look that bad alone, but when you try to save every byte, the result months from now will be a garbled mess that is difficult to comprehend. Instead, ignore those extra bytes and produce the following:
<a href="/path/elsewhere">
<% if @condition %>
True text
<% else %>
False text
<% end %>
</a>
If outputting all that extra useless whitespace makes you feel too icky, Rails has an option for you. If you close your scriptlet tag with a dash, as "-%>", Rails will strip some of the whitespace.
<a href="/path/elsewhere">
<% if @condition -%>
True text
<% else -%>
False text
<% end -%>
</a>
Just remember that someone has to maintain the code, and making code more difficult to comprehend will end up costing you more than the bandwidth you saved.
No comments:
Post a Comment