Write beautiful code with indentation in mind (rails, html)

It is very important to be aware of indentation (paragraphs) when writing code. A clean code not only reduces the number of errors, but also contributes to many things such as the speed of error resolution and the ease of looking back at the code. Here, I will explain the indentation that is important for writing beautiful code. Then, about rails, the points to be careful about are if statements, def, and each statements! !! I will write an if statement first!

#Easy-to-understand code
if @tweet.save
  redirect_to("/")
else
  redirect_to("/new")
end

#Obscure code
if @tweet.save
redirect_to("/")
else
redirect_to("/new")
end

The important point here is that the part surrounded by if and end is indented so that it is very easy to see where the if statement is applied (by opening two characters)! !! !! To say. I want you to write it carefully!

Next, let's look at an actual example of each statement (repetitive processing)!

#Easy-to-understand code
<% @tweets.each do |t| %>
  <%= t.body %>
<% end %>

#Obscure code
<% @tweets.each do |t| %>
<%= t.body %>
<% end %>

This is the same as the above example! !! !! The part surrounded by end is easy to understand where to repeat by dropping two characters or one tab key! !! Now the next example is ... def! !!

#Easy-to-understand code
def index
  @tweets = Tweet.all
  @user = User.find(params[:id])
end

#Obscure code
def index
@tweets = Tweet.all
 @user = User.find(params[:id])
end

It's already persistent, isn't it? Lol But I'll tell you what's important! Be absolutely aware of indentation in the part surrounded by end such as def each if!

Finally, let's learn about html tag (div) indentation! It's good to actually see it!

<!--Easy-to-understand code-->
<div class="allContainer">
  <div class="container">
    <p>Indentation is important</p>
  </div>
</div>

<!--Obscure code-->
<div class="allContainer">
<div class="container">
<p>Indentation is important</p>
</div>
</div>

It's hard to tell how the closing tag (</ div>) is hanging, right? ?? Therefore, mistakes such as making a mistake in the number of divs will occur, and some errors will not be executed as expected.

As above ・ Indentation is insanely important! !! ・ If you can do this, it will be a very beautiful code! !!

So, let's write carefully one by one from now on!

Recommended Posts

Write beautiful code with indentation in mind (rails, html)
Japaneseize using i18n with Rails
Write Java8-like code in Java8
exited with code 1 error resolution with docker-compose up in rails environment
Things to keep in mind when using Sidekiq with Rails
Enable automatic html tag generation in erb when working with VS Code
Generate Dart client code with Rails + apipie
[Rails] Various ways to write in seed files
Static code analysis with Checkstyle in Java + Gradle
[Rails] Organize your code neatly with gem active_decorator
One way to redirect_to with parameters in rails
Implement the Like feature in Ajax with Rails.
How to write test code with Basic authentication
ActiveModel :: MissingAttributeError (can't write unknown attribute 〇〇) in Rails
rails code doesn't respond well in atom erb